Running PHP workers on AppFog Running PHP workers on AppFog php php

Running PHP workers on AppFog


After a lot of tinkering myself, I've found a way!

In your php script, you should set timeout limit to 0, and have a infinite loop, like that:

<?php    set_time_limit(0);    while (true) {        print "blah\n";        sleep(120);    }

This code will print out "blah" every 2 minutes.

To deploy this to AppFog, you must use the af console command. The big thing here is to say no when it asks if that's a PHP app.

The steps

  1. af push on the directory
  2. say no if it guesses the language of your app
  3. Select Standalone as the app type.
  4. Now you select PHP
  5. Enter php index.php or whatever name you gave to your application main file.

It's all shown below:

D:\Users\Leonel\dev\app>af pushWould you like to deploy from the current directory? [Yn]:Application Name: APPDetected a PHP Application, is this correct? [Yn]: n[...]6: Standalone[...]Select Application Type: 6Selected Standalone Application[...]5: php[...]Select Runtime: 5Selected phpStart Command: php index.php1: AWS US East - Virginia[...]Select Infrastructure: 1Application Deployed URL [None]:Memory reservation (128M, 256M, 512M, 1G, 2G) [128M]:How many instances? [1]:Bind existing services to 'APP'? [yN]:Create services to bind to 'APP'? [yN]:Would you like to save this configuration? [yN]:Creating Application: OKUploading Application:  Checking for available resources: OK  Packing application: OK  Uploading (0K): OKPush Status: OKStaging Application 'APP': OKStarting Application 'APP': OKD:\Users\Leonel\dev\APP>af logs APP====> /logs/stdout.log <====blahblah

Some Notes

  • You need to create a separate app to perform the background tasks, though this app can be binded to the same services (e.g databases) of the other apps or this app can curl to your other app, for example. Just make sure it's on the same availability zone.
  • af logs APP will give you the output of the worker, so you can debug and check if everything is ok.

That's it, hope it helps.


Great solution. I'm unable comment due to insufficient reputation, so I'm modifying the original answer.

To enable the PHP pseudo crontab to start at a specified second, use an enforcement loop at the start of the script, as follows:

<?php    set_time_limit(0);    // begin process at zero (00) second mark    $cnt=0;    while (true) {      usleep(250000); // avoid excess looping      if ( date('s',time()) == '00' ) {        break;      }      if ( $cnt++ > 240 ) {        break; // something has gone wrong...      }    }    var_dump(date('s',time()));exit; // test/validate    while (true) {        print "blah\n";        sleep(120);    }