Mmoreram\GearmanBundle\Service\GearmanExecute::callJob PHP Method

callJob() private method

Given a worker, execute GearmanWorker function defined by job.
private callJob ( array $worker, array $options = [], GearmanWorker $gearmanWorker = null ) : GearmanExecute
$worker array Worker definition
$options array Array of options passed to the callback
$gearmanWorker GearmanWorker Worker instance to use
return GearmanExecute self Object
    private function callJob(array $worker, array $options = array(), \GearmanWorker $gearmanWorker = null)
    {
        if (is_null($gearmanWorker)) {
            $gearmanWorker = new \GearmanWorker();
        }
        if (isset($worker['job'])) {
            $jobs = array($worker['job']);
            $iterations = $worker['job']['iterations'];
            $minimumExecutionTime = $worker['job']['minimumExecutionTime'];
            $timeout = $worker['job']['timeout'];
            $successes = $this->addServers($gearmanWorker, $worker['job']['servers']);
        } else {
            $jobs = $worker['jobs'];
            $iterations = $worker['iterations'];
            $minimumExecutionTime = $worker['minimumExecutionTime'];
            $timeout = $worker['timeout'];
            $successes = $this->addServers($gearmanWorker, $worker['servers']);
        }
        $options = $this->executeOptionsResolver->resolve($options);
        $iterations = $options['iterations'] ?: $iterations;
        $minimumExecutionTime = $options['minimum_execution_time'] ?: $minimumExecutionTime;
        $timeout = $options['timeout'] ?: $timeout;
        if (count($successes) < 1) {
            if ($minimumExecutionTime > 0) {
                sleep($minimumExecutionTime);
            }
            throw new ServerConnectionException('Worker was unable to connect to any server.');
        }
        $objInstance = $this->createJob($worker);
        /**
         * Start the timer before running the worker.
         */
        $time = time();
        $this->runJob($gearmanWorker, $objInstance, $jobs, $iterations, $timeout);
        /**
         * If there is a minimum expected duration, wait out the remaining period if there is any.
         */
        if ($minimumExecutionTime > 0) {
            $now = time();
            $remaining = $minimumExecutionTime - ($now - $time);
            if ($remaining > 0) {
                sleep($remaining);
            }
        }
        return $this;
    }