示例#1
0
 /**
  * 001-echo_req.phpt
  *
  * @return void
  */
 public function testSend()
 {
     $connection = Net_Gearman_Connection::connect();
     Net_Gearman_Connection::send($connection, 'echo_req', array('text' => 'foobar'));
     do {
         $ret = Net_Gearman_Connection::read($connection);
     } while (is_array($ret) && !count($ret));
     Net_Gearman_Connection::close($connection);
     $this->assertType('array', $ret);
     $this->assertEquals('echo_res', $ret['function']);
     $this->assertEquals(17, $ret['type']);
     $this->assertType('array', $ret['data']);
     $this->assertEquals('foobar', $ret['data']['text']);
 }
示例#2
0
 /**
  * Mark your job as failing
  *
  * If your job fails for some reason (e.g. a query fails) you need to run
  * this function and exit from your run() method. This will tell Gearman
  * (and the client by proxy) that the job has failed.
  *
  * @return void
  * @see Net_Gearman_Connection::send()
  */
 public function fail()
 {
     Net_Gearman_Connection::send($this->conn, 'work_fail', array('handle' => $this->handle));
 }
示例#3
0
 /**
  * Stop working
  *
  * @return void
  */
 public function endWork()
 {
     foreach ($this->conn as $conn) {
         Net_Gearman_Connection::close($conn);
     }
 }
示例#4
0
 /**
  * Send a command
  *
  * @param string $cmd The command to send
  *
  * @return void
  * @throws Net_Gearman_Exception
  */
 protected function sendCommand($cmd)
 {
     if ($this->shutdown) {
         throw new Net_Gearman_Exception('This server has been shut down');
     }
     fwrite($this->conn, $cmd . "\r\n", Net_Gearman_Connection::stringLength($cmd . "\r\n"));
 }
示例#5
0
 /**
  * Disconnect from Gearman
  *
  * @return      void
  */
 public function disconnect()
 {
     if (!is_array($this->conn) || !count($this->conn)) {
         return;
     }
     foreach ($this->conn as $conn) {
         Net_Gearman_Connection::close($conn);
     }
 }
示例#6
0
 /**
  * Multibyte substr() implementation
  *
  * @param string  $str    The string to substr()
  * @param integer $start  The first position used
  * @param integer $length The maximum length of the returned string
  *
  * @return string Portion of $str specified by $start and $length
  * @see Net_Gearman_Connection::$multiByteSupport
  * @link http://us3.php.net/mb_substr
  * @link http://us3.php.net/substr
  */
 public static function subString($str, $start, $length)
 {
     if (is_null(self::$multiByteSupport)) {
         self::$multiByteSupport = intval(ini_get('mbstring.func_overload'));
     }
     if (self::$multiByteSupport & 2) {
         return mb_substr($str, $start, $length, '8bit');
     } else {
         return substr($str, $start, $length);
     }
 }
示例#7
0
 /**
  * Listen on the socket for work
  *
  * Sends the 'grab_job' command and then listens for either the 'noop' or
  * the 'no_job' command to come back. If the 'job_assign' comes down the
  * pipe then we run that job. 
  *
  * @param resource $socket The socket to work on 
  * 
  * @return boolean Returns true if work was done, false if not
  * @throws Net_Gearman_Exception
  * @see Net_Gearman_Connection::send()
  */
 protected function doWork($socket)
 {
     Net_Gearman_Connection::send($socket, 'grab_job');
     $resp = array('function' => 'noop');
     while (count($resp) && $resp['function'] == 'noop') {
         $resp = Net_Gearman_Connection::blockingRead($socket);
     }
     if (in_array($resp['function'], array('noop', 'no_job'))) {
         return false;
     }
     if ($resp['function'] != 'job_assign') {
         throw new Net_Gearman_Exception('Holy Cow! What are you doing?!');
     }
     $name = $resp['data']['func'];
     $handle = $resp['data']['handle'];
     $arg = array();
     if (isset($resp['data']['arg']) && Net_Gearman_Connection::stringLength($resp['data']['arg'])) {
         $arg = json_decode($resp['data']['arg'], true);
         if ($arg === null) {
             $arg = $resp['data']['arg'];
         }
     }
     // =======================================================================
     // this is the part of the code I changed, everything else was copy/paste
     // =======================================================================
     ///$job = Net_Gearman_Job::factory($name, $socket, $handle);
     $job = new NetGearmanInterfaceJob($this->callback_map, $socket, $handle);
     try {
         $this->start($handle, $name, $arg);
         $res = $job->run($arg);
         if (!is_array($res)) {
             $res = array('result' => $res);
         }
         $job->complete($res);
         $this->complete($handle, $name, $res);
         $this->consumed = true;
     } catch (NetGearmanInterfaceJobException $e) {
         $job->fail();
         $this->fail($handle, $name, $e);
         // Force the job's destructor to run
         $job = null;
         // re-throw the original exception...
         throw $e->e;
     } catch (Net_Gearman_Job_Exception $e) {
         $job->fail();
         $this->fail($handle, $name, $e);
     }
     // =======================================================================
     // Force the job's destructor to run
     $job = null;
     return true;
 }