0

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.

1
  • exec() a separate script, or a cron job that checks the status of all unfinished jobs. a php script like the first option would have to set the timeout limit on itself to 0 though. Commented May 15, 2011 at 7:11

3 Answers 3

1

Can your server not fire a request once the VM has been created?

Eg.

  1. PHP script requests the server via your API to create a new VM.
  2. PHP script records start time and exits. VM in queue on server waits to be created.
  3. Server finally creates VM and calls an update tables php script.

That way you have no polling, no cron scripts, no background threads. etc. But only if you're system can work this way. Otherwise I'd look at setting up a cron script as mentioned by @dqhendricks or if possible a background script as @Savas Alp mentioned.

Sign up to request clarification or add additional context in comments.

Comments

0

If your hosting allows, create a PHP CLI program and execute it in the background like the following.

<?php
while (true)
{
  sleep(10);

  // Do the checks etc.
}
?>

And run it like the following command:

php background.php & // Assuming you're using Linux

If your hosting does not allow running background jobs, you must utilize every opportunity to do this check; like doing it at the beginning of every PHP page request. To help facilitate this, after creating a virtual machine, the resulting page may refresh itself at every 10 seconds!

Comments

0

As variant, you can use Tasks module, and there is sample of task code:

class VMCheck extends \Tasks\Task
{
    protected $vm_name;

    public function add($vm_name)
    {
        $this->getStorage()->store(__CLASS__, $vm_name, true);
    }

    public function execute()
    {
        do
        {
            $check = CheckAPI_call($vm_name); //your checking code here
            sleep(10);
        }
        while (empty($check));
    }

    public function restore($data)
    {
        $this->vm_name = $data;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.