private function runJob(\GearmanWorker $gearmanWorker, $objInstance, array $jobs, $iterations, $timeout = null)
{
/**
* Set the output of this instance, this should allow workers to use the console output.
*/
if ($objInstance instanceof GearmanOutputAwareInterface) {
$objInstance->setOutput($this->output ?: new NullOutput());
}
/**
* Every job defined in worker is added into GearmanWorker
*/
foreach ($jobs as $job) {
$gearmanWorker->addFunction($job['realCallableName'], array($this, 'handleJob'), array('job_object_instance' => $objInstance, 'job_method' => $job['methodName'], 'jobs' => $jobs));
}
/**
* If iterations value is 0, is like worker will never die
*/
$alive = 0 === $iterations;
if ($timeout > 0) {
$gearmanWorker->setTimeout($timeout * 1000);
}
/**
* Executes GearmanWorker with all jobs defined
*/
while (false === $this->stopWorkSignalReceived && $gearmanWorker->work()) {
$iterations--;
$event = new GearmanWorkExecutedEvent($jobs, $iterations, $gearmanWorker->returnCode());
$this->eventDispatcher->dispatch(GearmanEvents::GEARMAN_WORK_EXECUTED, $event);
if ($gearmanWorker->returnCode() != GEARMAN_SUCCESS) {
break;
}
/**
* Only finishes its execution if alive is false and iterations
* arrives to 0
*/
if (!$alive && $iterations <= 0) {
break;
}
}
}