private function getClient(): \GearmanClient
    {
        if (null === $this->client) {
            $client = new \GearmanClient();
            $client->setTimeout($this->timeout);

            if (empty($this->servers)) {
                try {
                    // by default we add the local machine
                    if (!$client->addServer()) {
                        throw new \GearmanException(
                            'GearmanClient::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 ($client->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
                    );
                }
            }

            $this->client = $client;
        }

        return $this->client;
    }
private function getClient(): \GearmanClient
    {
        if (null === $this->client) {
            $client = new \GearmanClient();
            $client->setTimeout($this->timeout);

            if (empty($this->servers)) {
                try {
                    // by default we add the local machine
                    if (!$client->addServer()) {
                        throw new \GearmanException(
                            'GearmanClient::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 ($client->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
                    );
                }
            }

            $this->client = $client;
        }

        return $this->client;
    }
private function getClient ( ) : \ GearmanClient { if ( null === $ this -> client ) { $ client = new \ GearmanClient ( ) ; $ client -> setTimeout ( $ this -> timeout ) ; if ( empty ( $ this -> servers ) ) { try { // by default we add the local machine if ( ! $ client -> addServer ( ) ) { throw new \ GearmanException ( 'GearmanClient::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 ( $ client -> 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 ) ; } } $ this -> client = $ client ; } return $ this -> client ; }
Creates a gearman client, sets the timeout and adds the servers.
At least one server must connect successfully otherwise an exception is thrown.

Gearman's failure to add a server produces an exception with a message of
'Failed to set exception option' which isn't entirely clear but it means you
won't be sending any messages.  :)

@return \GearmanClient

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