JustinK101 Posted May 15, 2011 Share Posted May 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236448-how-to-setup-php-workers/ Share on other sites More sharing options...
gizmola Posted May 15, 2011 Share Posted May 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236448-how-to-setup-php-workers/#findComment-1215604 Share on other sites More sharing options...
JustinK101 Posted May 15, 2011 Author Share Posted May 15, 2011 Thanks for the reply, but need a scalable solution, and a cron job, just doesn't feel right. We Googled around and found Gearman. Thoughts? http://docs.php.net/manual/en/gearman.examples-reverse-bg.php Quote Link to comment https://forums.phpfreaks.com/topic/236448-how-to-setup-php-workers/#findComment-1215606 Share on other sites More sharing options...
gizmola Posted May 15, 2011 Share Posted May 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236448-how-to-setup-php-workers/#findComment-1215609 Share on other sites More sharing options...
gagansh Posted May 26, 2011 Share Posted May 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236448-how-to-setup-php-workers/#findComment-1220470 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.