Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running PHP workers on AppFog

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.

like image 305
Leonel Avatar asked Nov 27 '12 23:11

Leonel


1 Answers

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.

like image 97
Leonel Avatar answered Sep 18 '22 18:09

Leonel