Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

whats the difference between a task and do

Tags:

php

gearman

When looking inside the php gearman docs I see that there are task and do, both of them have background and non background and they also all have high and low and normal.

Can anyone clarify these? I'm just really confused about the difference.

like image 308
WojonsTech Avatar asked Oct 02 '12 11:10

WojonsTech


1 Answers

There are two differences: running order and purpose.

Running order - when you run some tasks by do and by runTasks then do has higher priority than tasks. So the running order will by:

  1. all do with hight priority (GearmanClient::doHigh() or
    GearmanClient::doHighBackground())
  2. all task with hight priority (GearmanClient::addTaskHigh() or GearmanClient::addTaskHighBackground())
  3. all do with normal priority
  4. all task with normal priority
  5. all do with low priority
  6. all task with low priority

Purpose:

Task - use this for short tasks, when you do not care when it finish or how is progress

Do - use this for complex job or when you need to check progress. There is GearmanJob::sendStatus() for this purpose:

worker.php

$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction("sleep13", array('MyWorker', 'sleep13'));
while ($worker->work());

class MyWorker {
    public function sleep13($job) {
        $data = unserialize($job->workload());
        echo 'start ' . $data['id']  . PHP_EOL;

        for($i = 0; $i < 13; $i++) {
            sleep(1);
            $job->sendStatus($i, 13);
        }
        echo 'done ' . $data['id']  . PHP_EOL;
    }
}

client.php

$client = new GearmanClient();
$client->addServer();

// Run task
$job_handle = $client->doBackground("sleep13", serialize(array('id' => 'normal-1')));

// Check progress
$done = false;
do {
   usleep(300);
   $stat = $client->jobStatus($job_handle);
   if (!$stat[0]) // the job is known so it is not done
      $done = true;
   echo "Running: " . ($stat[1] ? "true" : "false") . ", numerator: " . $stat[2] . ", denomintor: " . $stat[3] . "\n";
} while(!$done);

echo "done!\n";

$job_handle is string so you can store it somewhere and then check it anytime.

like image 96
Štefan Kušnír Avatar answered Sep 20 '22 20:09

Štefan Kušnír