/**
  * When no server is supplied, it should connect to localhost:4730.
  *
  * @return void
  */
 public function testDefaultConnect()
 {
     $connection = Net_Gearman_Connection::connect();
     $this->assertType('resource', $connection);
     $this->assertEquals('socket', strtolower(get_resource_type($connection)));
     $this->assertTrue(Net_Gearman_Connection::isConnected($connection));
     Net_Gearman_Connection::close($connection);
 }
Exemple #2
0
 /**
  * Constructor
  *
  * @param array   $servers An array of servers or a single server
  * @param integer $timeout Timeout in microseconds
  * 
  * @return void
  * @throws Net_Gearman_Exception
  * @see Net_Gearman_Connection
  */
 public function __construct($servers, $timeout = 1000)
 {
     if (!is_array($servers) && strlen($servers)) {
         $servers = array($servers);
     } elseif (is_array($servers) && !count($servers)) {
         throw new Net_Gearman_Exception('Invalid servers specified');
     }
     $this->servers = $servers;
     foreach ($this->servers as $key => $server) {
         $conn = Net_Gearman_Connection::connect($server, $timeout);
         if (!Net_Gearman_Connection::isConnected($conn)) {
             unset($this->servers[$key]);
             continue;
         }
         $this->conn[] = $conn;
     }
     $this->timeout = $timeout;
 }
Exemple #3
0
 /**
  * Get a connection to a Gearman server
  *
  * @param string $uniq The unique id of the job
  *
  * @return resource A connection to a Gearman server
  */
 protected function getConnection($uniq = null)
 {
     $conn = null;
     $start = microtime(true);
     $elapsed = 0;
     $servers = $this->servers;
     while ($conn === null && $elapsed < $this->timeout && count($servers)) {
         if (count($servers) === 1) {
             $key = key($servers);
         } elseif ($uniq === null) {
             $key = array_rand($servers);
         } else {
             $key = ord(substr(md5($uniq), -1)) % count($servers);
         }
         $server = $servers[$key];
         if (empty($this->conn[$server])) {
             $conn = null;
             $start = microtime(true);
             try {
                 $conn = Net_Gearman_Connection::connect($server, $this->timeout);
             } catch (Net_Gearman_Exception $e) {
                 $conn = null;
             }
             if (!Net_Gearman_Connection::isConnected($conn)) {
                 $conn = null;
                 unset($servers[$key]);
                 // we need to rekey the array
                 $servers = array_values($servers);
             } else {
                 $this->conn[$server] = $conn;
                 break;
             }
         } else {
             $conn = $this->conn[$server];
         }
         $elapsed = microtime(true) - $start;
     }
     if (empty($conn)) {
         throw new Net_Gearman_Exception("Failed to connect to any Gearman servers. Attempted " . implode(",", $this->servers));
     }
     return $conn;
 }
 /**
  * Constructor
  *
  * @param array   $servers An array of servers or a single server
  * @param integer $timeout Timeout in microseconds
  *
  * @return void
  * @throws Net_Gearman_Exception
  * @see Net_Gearman_Connection
  */
 public function __construct($servers = null, $timeout = 1000)
 {
     if (is_null($servers)) {
         $servers = array("localhost");
     } elseif (!is_array($servers) && strlen($servers)) {
         $servers = array($servers);
     } elseif (is_array($servers) && !count($servers)) {
         throw new Net_Gearman_Exception('Invalid servers specified');
     }
     $this->servers = $servers;
     foreach ($this->servers as $key => $server) {
         $server = trim($server);
         if (empty($server)) {
             throw new Net_Gearman_Exception('Invalid servers specified');
         }
         try {
             $conn = Net_Gearman_Connection::connect($server, $timeout);
         } catch (Net_Gearman_Exception $e) {
             unset($this->servers[$key]);
             continue;
         }
         if (!Net_Gearman_Connection::isConnected($conn)) {
             unset($this->servers[$key]);
             continue;
         }
         $this->conn[] = $conn;
         $this->serverConnections[(int) $conn] = $server;
     }
     if (empty($this->conn)) {
         throw new Net_Gearman_Exception("Couldn't connect to any available servers");
     }
     $this->timeout = $timeout;
 }