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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With