13

I searched and searched and searched but didn't find any resources on that.

Is there any way to run a worker in a PHP app on AppFog?

I've only found instructions for running Ruby, Node.js and Python workers, based on frameworks of that languages.

2 Answers 2

30

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 push
Would you like to deploy from the current directory? [Yn]:
Application Name: APP
Detected a PHP Application, is this correct? [Yn]: n
[...]
6: Standalone
[...]
Select Application Type: 6
Selected Standalone Application
[...]
5: php
[...]
Select Runtime: 5
Selected php
Start Command: php index.php
1: AWS US East - Virginia
[...]
Select Infrastructure: 1
Application 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: OK
Uploading Application:
  Checking for available resources: OK
  Packing application: OK
  Uploading (0K): OK
Push Status: OK
Staging Application 'APP': OK
Starting Application 'APP': OK

D:\Users\Leonel\dev\APP>af logs APP
====> /logs/stdout.log <====

blah
blah

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.

2
  • 2
    +1 great answer. You might want to wrap it in a try catch and log the errors too. Commented Nov 28, 2012 at 0:56
  • can we 'exec' scripts on AppFog in order to simulate parallel processes?
    – Ajibola
    Commented Oct 1, 2013 at 18:43
1

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);
    }

Not the answer you're looking for? Browse other questions tagged or ask your own question.