GearmanClient::addServer

(PECL gearman >= 0.5.0)

GearmanClient::addServerAdd a job server to the client

Descrizione

public GearmanClient::addServer(string $host = null, int $port = 0, bool $setupExceptionHandler = true): bool

Adds a job server to a list of servers that can be used to run a task. No socket I/O happens here; the server is simply added to the list.

Elenco dei parametri

host

L'hostname del job server.

port

La porta del job server.

Valori restituiti

Restituisce true in caso di successo, false in caso di fallimento.

Esempi

Example #1 Adding two job servers

<?php

# Create our client object.
$gmclient= new GearmanClient();

# Add two job servers, the first on the default 4730 port
$gmclient->addServer("10.0.0.1");
$gmclient->addServer("10.0.0.2", 7003);

?>

Vedere anche:

add a note add a note

User Contributed Notes 8 notes

up
2
info at phpgangsta dot de
11 years ago
Since a few versions the port parameter is not optional anymore. I have version 1.1.1 of pecl/gearman compiled with libgearman 1.1.5, and I'm getting the following error:

send_packet(GEARMAN_COULD_NOT_CONNECT) Failed to send server-options packet -> libgearman/connection.cc:430

This happens if you don't provide a port.

Just set the second parameter to 4730 and it is working again.
up
2
brainreflex at gmail dot com
8 years ago
Amit, kosta250

I found a workaround to avoid the dead servers and continue with rest alive.

<?php

$servers
= array(
  array(
'host' => '127.0.0.1', 'port' => '4730'),
  array(
'host' => '127.0.0.1', 'port' => '4731'),
  array(
'host' => '127.0.0.1', 'port' => '4732'),
  array(
'host' => '127.0.0.2', 'port' => '4730')
);

$client= new \GearmanClient();

foreach(
$servers as $server) {
 
$c = new \GearmanClient();
 
$c->addServer($server['host'], $server['port']);

  if (@
$c->ping('ping')) {
   
$client->addServer($server['host'], $server['port']);
  }
}

?>
up
1
jqv8gg681 at relay dot firefox dot com
2 years ago
Combining recommendations from brainreflex and iloveapplepie, as well as hours of testing. This avoids some bugs in cross versions of gearman across platforms.
It steps though the server list and fails if all are inaccessible.
I also use the same override as "class SmartGearmanClient extends GearmanClient" for the clients.

<?php // WORKER
class SmartGearmanWorker extends GearmanWorker
{
    public
$server = false;
    public function
connect($servers = array( array('host' => '127.0.0.1', 'port' => '4730') ))
    {
       
$connected = false;
        foreach (
$servers as $server) {
           
$c = new GearmanClient();
           
$c->addServer($server['host'], $server['port'], false);
   
            if (@
$c->ping('ping')) {             
               
$this->addServer($server['host'], $server['port']);
               
$connected=true;
               
$this->server = array(
                  
'host'=>$server['host'],
                  
'port'=>$server['port']
                );
                break;
// Remove this to use last working server in list
           
}
        }
        return
$connected;
    }
}

echo
"Starting\n";

# Add some servers
$servers = array(
    array(
'host' => '127.0.0.1', 'port' => '4730'),
    array(
'host' => '127.0.0.1', 'port' => '4731'),
    array(
'host' => '127.0.0.1', 'port' => '4732'),
    array(
'host' => '127.0.0.2', 'port' => '4730'),
    array(
'host' => '192.168.204.10', 'port' => '4730')
);

# Create our worker object.
$gmw= new SmartGearmanWorker();

if (
$gmw->connect($servers)) {
   
# Register function "reverseString" with the server.
   
$gmw->addFunction("reverseString", "reverseString_fn");

    print
"Connected to {$gmw->server['host']}:{$gmw->server['port']}...\n";
    print
"Waiting for jobs...\n";

    while (
$gmw->work()) {
        if (
$gmw->returnCode() != GEARMAN_SUCCESS) {
            echo
"return_code: " . $gmw->returnCode() . "\n";
            break;
        }
    }

    echo
"DONE\n";
} else {
    echo
"Unable to connect to any gearman-job-servers.".PHP_EOL;
};

function
reverseString_fn($job)
{
    echo
"Received job: " . $job->handle() . "\n";

   
$workload = $job->workload();
   
$workload_size = $job->workloadSize();

    echo
"Workload: $workload ($workload_size)\n";

   
# Lets send some progress information
   
for ($x= 0; $x < $workload_size; $x++) {
        echo
"Sending status: " . ($x + 1) . "/$workload_size complete\n";
       
$job->sendStatus($x+1, $workload_size);
       
$job->sendData(substr($workload, $x, 1));   
       
sleep(1);
    }

   
$result= strrev($workload);
    echo
"Result: $result\n";

   
# Return what we want to send back to the client.
   
return $result;
}
up
0
iloveapplepie
2 years ago
Sometimes you want the job server to start even if not all servers are available.  The reason is that if at a later point the server comes back gearman will use it, even if if failed during the add server operation.

class SmartGearmanWorker extends GearmanWorker
{
    function addServer($host = '127.0.0.1', $port = 4730):bool
    {
        try {
            parent::addServer($host, $port);
            echo "\nSERVER $host WORKED";
        } catch (GearmanException $e) {
            echo "\nSERVER $host FAILED";
        }
        return true;
    }
}

class SmartGearmanClient extends GearmanClient
{
    function addServer($host = '127.0.0.1', $port = 4730,  bool $setupExceptionHandler = false):bool
    {
        try {
            parent::addServer($host, $port, $setupExceptionHandler);
            echo "\nSERVER $host WORKED";
    } catch (GearmanException $e) {
               
            echo "\nSERVER $host FAILED"; //this does not get called even if the server is down
        }
        return true;
    }
}
up
0
kosta250 at gmail dot com
8 years ago
Adding to Amit's comments,  I found that if the first server in the list of servers is down, then there seems to be no way to handle such a condition.
up
-1
michael at butlerpc dot net
5 years ago
Prior to version 2.0.5, addServer DOES perform socket I/O indirectly because it calls set_server_option internally for an exception handler. This means if the server is unreachable you will get a GearmanException thrown at this point, and you may want to catch and handle it in your application.

<?php

$client
->addServer('127.0.0.1', 4321); // does attempt a socket connection!

?>

Starting with version 2.0.5 of the extension, a third boolean argument (after $port) may be passed false in order to prevent this from happening.

<?php

$client
->addServer('127.0.0.1', 4321, false); // no socket i/o happens here

?>
up
-1
Amit
11 years ago
The addServer and addServers are unforgiving when any of the addresses are down. I tried using exceptions, but it does not quite work. Can you provide a working example to handle a list of servers where one or more is not running.

Thanks
To Top