Skip to content

cloudfoundry-samples/cf-ex-stand-alone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CloudFoundry PHP Example Application: stand alone & tasks

This is an example application which can be run on CloudFoundry using the PHP Build Pack.

This is an example stand alone application and example of using CF tasks. It is simply a PHP script that runs outside of PHP-FPM as a stand alone process. It also includes a simple task that can be run with cf run-task.

Usage

  1. Clone the app (i.e. this repo).
git clone https://github.com/cloudfoundry-samples/cf-ex-stand-alone.git
cd cf-ex-stand-alone
  1. Edit the manifest.yml file. Also, notice how the manifest file includes the no-route: true and health-check-type: process attributes. This is important because it instructs CloudFoundry that this application will not handle web requests, which means it will not expect the application to listen on $PORT.

  2. Push it to CloudFoundry.

cf push

Run cf logs cf-ex-stand-alone. This should start to tail the logs and you should see the application generate a message every five seconds. The application message will look like this.

2017-03-14T16:24:09.57-0400 [APP/PROC/WEB/0]OUT 20:24:09 cf-ex-stand-alone | Current Time From App [08:24:09]
  1. Run the task (optional).
cf run-task cf-ex-stand-alone 'php task.php' --name 'php-task'

Run cf logs cf-ex-stand-alone. This should start to tail the logs and you should see both the application and task generate a message every five seconds. The task message will look like this.

2017-03-14T16:24:11.53-0400 [APP/TASK/3839de9e/0]OUT Current Time From Task [16] [08:24:11]

The task should exit after 20 iterations. You can run cf tasks to confirm it exited successfully.

How It Works

When you push the application here's what happens.

  1. The local bits are pushed to your target. This is small, three files around 1k.
  2. The server downloads the PHP Build Pack and runs it. This installs just PHP.
  3. Because there is no web server, the build pack knows that the application is a stand alone app and it looks for a main file to execute. In this case, it finds app.php and so it runs it with the command $HOME/php/bin/php -c "$HOME/php/etc" app.php.
  4. At this point, the build pack is done and CF runs our droplet executing the command from the previous step. It is critical that this application runs forever. If the application exits, CloudFoundry will think it has crashed and restart it.
  5. If you run the task, the task will spin up in a separate container. CF will execute the command indicated (i.e. php task.php) using the same droplet as the application. The task will run and eventually exit, it should exit 0 to indicate success. You can run cf tasks cf-ex-stand-alone to confirm it was successful.

About

CloudFoundry PHP example application: stand alone app

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages