Created
June 16, 2012 03:53
-
-
Save lox/2939834 to your computer and use it in GitHub Desktop.
Failing zmq worker script
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
<?php | |
require(dirname(__FILE__).'/../lib/zmq/Zmsg.php'); | |
$context = new ZMQContext(); | |
$worker = $context->getSocket(ZMQ::SOCKET_DEALER); | |
$worker->setSockOpt(ZMQ::SOCKOPT_IDENTITY, trim(`hostname`)."#worker".getmypid()); | |
$worker->connect($endpoint); | |
// tell the broker we are ready for a test | |
$worker->send("READY"); | |
// wait till we get a test from the broker | |
$zmsg = new Zmsg($worker); | |
$zmsg->recv(); | |
// message is in JSON, decode | |
$body = $zmsg->body(); | |
$req = json_decode($body); | |
// tell the broker we have queued the test | |
$zmsg->body_set("QUEUED $body"); | |
$zmsg->send(false); | |
// fork a child to execute cmd, in case it crashes | |
$pid = pcntl_fork(); | |
if ($pid == -1) | |
{ | |
die('could not fork'); | |
} | |
else if ($pid) | |
{ | |
// we are the parent | |
pcntl_wait($status); | |
$exitcode = pcntl_wexitstatus($status); | |
} | |
else | |
{ | |
// we are the child | |
passthru($req->cmd); | |
exit(0); | |
} | |
$r = array( | |
'cmd'=>$req->cmd, | |
'exitcode'=>$exitcode, | |
); | |
$zmsg->body_set('RESULT '.json_encode($r)); | |
$zmsg->send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment