Created
September 3, 2011 21:05
-
-
Save hjr3/1191796 to your computer and use it in GitHub Desktop.
Gearman worker addTasks example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// macro.php | |
<?php | |
$gmw = new GearmanWorker(); | |
$gmw->addServer(); | |
$gmw->addFunction('macro', function(GearmanJob $job) { | |
$gmc = new GearmanClient; | |
$gmc->addServer(); | |
$gmc->setCompleteCallback(function(GearmanTask $task) { | |
$h = $task->jobHandle(); | |
echo "Job '{$h}' complete", PHP_EOL; | |
}); | |
$gmc->setFailCallback(function(GearmanTask $task) { | |
$h = $task->jobHandle(); | |
echo "Job '{$h}' failed", PHP_EOL; | |
}); | |
$gmc->addTask('micro', 'pass'); | |
$gmc->addTask('micro', 'pass'); | |
$gmc->addTask('micro', 'pass'); | |
$gmc->addTask('micro', 'fail'); | |
$gmc->addTask('micro', 'fail'); | |
$gmc->addTask('micro', 'fail'); | |
$gmc->runTasks(); | |
}); | |
while ($gmw->work()); | |
?> | |
// micro.php | |
<?php | |
$gmw = new GearmanWorker; | |
$gmw->addServer(); | |
$gmw->addFunction('micro', function(GearmanJob $job) { | |
if ($job->workload() == 'fail') { | |
$job->sendFail(); | |
} else { | |
$job->sendComplete('complete'); | |
} | |
}); | |
while ($gmw->work()); | |
?> | |
// cli | |
gearman -f macro -s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So the companion client to the "echo" worker above would have to register a setDataCallback method to receive the object sent through the sendData call, right?
Similarly, the worker should call sendComplete if the client is expecting something via setCompleteCallback.
I don't do many callbacks with Gearman, but the manual really failed to make the connection for me. Thanks!