We have added GearmanWorker::echo()
call to check if the Gearman server is available.
Look at the code after changes
$worker = new GearmanWorker();
$worker->addServer('host', 123);
$worker->addFunction('name', array($this, 'function'));
$worker->setTimeout(5000);
while ($worker->work() || GEARMAN_TIMEOUT == $worker->returnCode()) {
if (GEARMAN_TIMEOUT == $worker->returnCode()) {
$this->handleTimeout();
$echo = @$worker->echo(1);
if (!$echo) {
echo 'Failed to connect to Gearman Gerver.'. PHP_EOL;
break;
}
} elseif (GEARMAN_SUCCESS != $worker->returnCode()) {
$this->handleFail();
break;
}
}
Please note the line $echo = @$worker->echo(1);
and checks bellow. If server is unavailable then echo()
output PHP warning and returns false
.
GearmanException
is never thrown in our setup.