Jump to content

How To Setup PHP Workers


JustinK101

Recommended Posts

Howdy, we are writing a PHP script which creates virtual machines via a RESTful API call. That part is quite easy. Once that request to create the VM is sent to the server, the API request returns with essentially "Machine queued to be created...". When we create a virtual machine, we insert a record into a MySQL database basically with VM label, and DATE-CREATED-STARTED. That record also has a field DATE-CREATED-FINISHED which is NULL.

 

LABEL          DATE-CREATED-STARTED      DATE-CREATED-FINISHED

test-vm-1      2011-05-14 12:00:00            NULL

 

So here is our problem. How do we basically spin/spawn off a PHP worker, on the initial request, that checks the status of the queued virtual machine every 10 seconds, and when the virtual machine is up and running, updates DATE-CREATED-FINISHED. Keep in mind, the initial API request immediately returns "Machine queue to be created." and then exits. The PHP worker needs to be doing the 10 second check in the background.

 

Thanks.

Link to comment
Share on other sites

I wouldn't overcomplicate it.  Have a job that queries all the rows that have a null date-created-finished value order by date-created-started.  Loop through those checking the status of each, and if any are finished then update the status. 

 

You can put this job in cron and run it every 10 seconds, however, you need some sort of semaphore method in case something hangs up and the job doesn't finish in under 10 seconds so you don't get a race condition. The easiest way would be to have a simple table with one row in it that you either insert/delete or update its value.  The job should check for this row and simply exit if the "in process" row exists.

Link to comment
Share on other sites

Gearman is great, but usually people use it because they have different architectures they are trying to stitch together.  If you are dead set on doing the multiple process design, you don't need it -- just write your script loop and put a sleep call at the bottom of the loop.  Your main script can call your polling script with exec and specifying the '&' to tell linux to background it.  Works really well.

Link to comment
Share on other sites

  • 2 weeks later...

I wouldn't overcomplicate it.  Have a job that queries all the rows that have a null date-created-finished value order by date-created-started.  Loop through those checking the status of each, and if any are finished then update the status. 

 

You can put this job in cron and run it every 10 seconds, however, you need some sort of semaphore method in case something hangs up and the job doesn't finish in under 10 seconds so you don't get a race condition. The easiest way would be to have a simple table with one row in it that you either insert/delete or update its value.  The job should check for this row and simply exit if the "in process" row exists.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.