Question

The code:

$gmc = new GearmanClient();
$gmc->addServer();
$gmc->setCompleteCallback(function(GearmanTask $task){
    echo 'Complete: Task ' . $task->unique() . PHP_EOL;
});

for ($i=0; $i<10; $i++) {
    $gmc->addTask('queryShard', json_encode($taskData), null, 'Job:' . $i);
}

if (!$gmc->runTasks()) {
    echo "ERROR " . $gmc->error() . "\n";
}

I can see when each of the fired tasks are done. However, is there a way to fire an event when all launched tasks succeed (please don't offer solutions that rely on manual task registry)?

Also, can anyone advise on how to manage a group of tasks as a single set?

Thanks!

Was it helpful?

Solution

runTasks will block execution of the calling thread until all added foreground tasks complete. See fiskfisk answer for more details. However the fact that the all tasks completed does not imply that all tasks successfully completed.

Gearman application framework intentionally does not make assumptions on (un)successful competition of the tasks. There are many Gearman client implementations and limitless number of use cases. It is on the developer to provide the most suitable solution for the use case in question.

Most commonly I would use the SplObjectStorage class along with implementing created, fail, warning, exception and complete callbacks to track tasks completion on the client side. There might be complex use cases when additional tracking on the workers side might be needed.

OTHER TIPS

You can use the following:

$taskset = $gmc->new_task_set();
while(...) {
    $taskset->addTask(...);
}
$taskset->wait();

to wait until all jobs has been finished.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top