protected function setup(): void
    {
        if (null === $this->worker) {
            $worker = new \GearmanWorker();
            $worker->setTimeout($this->timeout);

            if (empty($this->servers)) {
                try {
                    // by default we add the local machine
                    if (!$worker->addServer()) {
                        throw new \GearmanException(
                            'GearmanWorker::addServer returned false.',
                            GEARMAN_COULD_NOT_CONNECT
                        );
                    }
                } catch (\Throwable $e) {
                    throw new \GearmanException(
                        'Unable to add local server 127.0.0.1:4730.  ' . $e->getMessage(),
                        GEARMAN_COULD_NOT_CONNECT
                    );
                }
            } else {
                shuffle($this->servers);
                $added = 0;
                foreach ($this->servers as $server) {
                    $host = isset($server['host']) ? $server['host'] : '127.0.0.1';
                    $port = (int)isset($server['port']) ? $server['port'] : 4730;
                    try {
                        if ($worker->addServer($host, $port)) {
                            $added++;
                        }
                    } catch (\Throwable $e) {
                        // do nothing, yet.
                    }
                }

                if (0 === $added) {
                    throw new \GearmanException(
                        sprintf('Unable to add any of these servers: %s', json_encode($this->servers)),
                        GEARMAN_COULD_NOT_CONNECT
                    );
                }
            }

            $worker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
            $worker->addOptions(GEARMAN_WORKER_GRAB_UNIQ);
            if (null !== $this->workerId && is_callable([$worker, 'setId'])) {
                $worker->setId($this->workerId);
            }
            $this->worker = $worker;

            shuffle($this->channels);
            foreach ($this->channels as $channel) {
                $this->worker->addFunction($channel, [$this, 'handleJob']);
            }
        }
    }
protected function setup(): void
    {
        if (null === $this->worker) {
            $worker = new \GearmanWorker();
            $worker->setTimeout($this->timeout);

            if (empty($this->servers)) {
                try {
                    // by default we add the local machine
                    if (!$worker->addServer()) {
                        throw new \GearmanException(
                            'GearmanWorker::addServer returned false.',
                            GEARMAN_COULD_NOT_CONNECT
                        );
                    }
                } catch (\Throwable $e) {
                    throw new \GearmanException(
                        'Unable to add local server 127.0.0.1:4730.  ' . $e->getMessage(),
                        GEARMAN_COULD_NOT_CONNECT
                    );
                }
            } else {
                shuffle($this->servers);
                $added = 0;
                foreach ($this->servers as $server) {
                    $host = isset($server['host']) ? $server['host'] : '127.0.0.1';
                    $port = (int)isset($server['port']) ? $server['port'] : 4730;
                    try {
                        if ($worker->addServer($host, $port)) {
                            $added++;
                        }
                    } catch (\Throwable $e) {
                        // do nothing, yet.
                    }
                }

                if (0 === $added) {
                    throw new \GearmanException(
                        sprintf('Unable to add any of these servers: %s', json_encode($this->servers)),
                        GEARMAN_COULD_NOT_CONNECT
                    );
                }
            }

            $worker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
            $worker->addOptions(GEARMAN_WORKER_GRAB_UNIQ);
            if (null !== $this->workerId && is_callable([$worker, 'setId'])) {
                $worker->setId($this->workerId);
            }
            $this->worker = $worker;

            shuffle($this->channels);
            foreach ($this->channels as $channel) {
                $this->worker->addFunction($channel, [$this, 'handleJob']);
            }
        }
    }
protected function setup ( ) : void { if ( null === $ this -> worker ) { $ worker = new \ GearmanWorker ( ) ; $ worker -> setTimeout ( $ this -> timeout ) ; if ( empty ( $ this -> servers ) ) { try { // by default we add the local machine if ( ! $ worker -> addServer ( ) ) { throw new \ GearmanException ( 'GearmanWorker::addServer returned false.' , GEARMAN_COULD_NOT_CONNECT ) ; } } catch ( \ Throwable $ e ) { throw new \ GearmanException ( 'Unable to add local server 127.0.0.1:4730.  ' . $ e -> getMessage ( ) , GEARMAN_COULD_NOT_CONNECT ) ; } } else { shuffle ( $ this -> servers ) ; $ added = 0 ; foreach ( $ this -> servers as $ server ) { $ host = isset ( $ server [ 'host' ] ) ? $ server [ 'host' ] : '127.0.0.1' ; $ port = ( int ) isset ( $ server [ 'port' ] ) ? $ server [ 'port' ] : 4730 ; try { if ( $ worker -> addServer ( $ host , $ port ) ) { $ added ++ ; } } catch ( \ Throwable $ e ) { // do nothing, yet. } } if ( 0 === $ added ) { throw new \ GearmanException ( sprintf ( 'Unable to add any of these servers: %s' , json_encode ( $ this -> servers ) ) , GEARMAN_COULD_NOT_CONNECT ) ; } } $ worker -> addOptions ( GEARMAN_WORKER_NON_BLOCKING ) ; $ worker -> addOptions ( GEARMAN_WORKER_GRAB_UNIQ ) ; if ( null !== $ this -> workerId && is_callable ( [ $ worker , 'setId' ] ) ) { $ worker -> setId ( $ this -> workerId ) ; } $ this -> worker = $ worker ; shuffle ( $ this -> channels ) ; foreach ( $ this -> channels as $ channel ) { $ this -> worker -> addFunction ( $ channel , [ $ this , 'handleJob' ] ) ; } } }
Creates a gearman worker, sets the timeout and adds the servers.
At least one server must connect successfully otherwise an exception is thrown.

@throws \GearmanException
Creates a gearman worker sets the timeout and adds the servers . At least one server must connect successfully otherwise an exception is thrown .