Skip to content

Instantly share code, notes, and snippets.

@lox
Created June 16, 2012 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lox/2939834 to your computer and use it in GitHub Desktop.
Save lox/2939834 to your computer and use it in GitHub Desktop.
Failing zmq worker script
<?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