Service/GearmanExecute.php
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) {

            /**
             * worker needs to have it's context into separated memory space;
             * if it's passed as a value, then garbage collector remove the target
             * what causes a segfault
             */
            $this->workersBucket[$job['realCallableName']] = [
                'job_object_instance' => $objInstance,
                'job_method' => $job['methodName'],
                'jobs' => $jobs,
            ];
            $gearmanWorker->addFunction(
                $job['realCallableName'],
                array($this, 'handleJob')
            );
        }

        /**
         * 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;
            }
        }
    }
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) {

            /**
             * worker needs to have it's context into separated memory space;
             * if it's passed as a value, then garbage collector remove the target
             * what causes a segfault
             */
            $this->workersBucket[$job['realCallableName']] = [
                'job_object_instance' => $objInstance,
                'job_method' => $job['methodName'],
                'jobs' => $jobs,
            ];
            $gearmanWorker->addFunction(
                $job['realCallableName'],
                array($this, 'handleJob')
            );
        }

        /**
         * 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;
            }
        }
    }
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 ) { /**
             * worker needs to have it's context into separated memory space;
             * if it's passed as a value, then garbage collector remove the target
             * what causes a segfault
             */ $ this -> workersBucket [ $ job [ 'realCallableName' ] ] = [ 'job_object_instance' => $ objInstance , 'job_method' => $ job [ 'methodName' ] , 'jobs' => $ jobs , ] ; $ gearmanWorker -> addFunction ( $ job [ 'realCallableName' ] , array ( $ this , 'handleJob' ) ) ; } /**
         * 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 ; } } }
Given a GearmanWorker and an instance of Job, run it

@param \GearmanWorker $gearmanWorker Gearman Worker
@param Object         $objInstance   Job instance
@param array          $jobs          Array of jobs to subscribe
@param integer        $iterations    Number of iterations
@param integer        $timeout       Timeout

@return GearmanExecute self Object
Given a GearmanWorker and an instance of Job run it