/** * 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']); }
/** * Run a set of tasks * * @param object $set A set of tasks to run * @param int $timeout Time in seconds for the socket timeout. Max is 10 seconds * * @return void * @see Net_Gearman_Set, Net_Gearman_Task */ public function runSet(Net_Gearman_Set $set, $timeout = null) { $this->set = $set; $totalTasks = $this->set->tasksCount; $taskKeys = array_keys($this->set->tasks); $t = 0; if ($timeout !== null) { $socket_timeout = min(10, (int) $timeout); } else { $socket_timeout = 10; } while (!$this->set->finished()) { if ($timeout !== null) { if (empty($start)) { $start = microtime(true); } else { $now = microtime(true); if ($now - $start >= $timeout) { break; } } } if ($t < $totalTasks) { $k = $taskKeys[$t]; $this->submitTask($this->set->tasks[$k]); if ($this->set->tasks[$k]->type == Net_Gearman_Task::JOB_BACKGROUND || $this->set->tasks[$k]->type == Net_Gearman_Task::JOB_HIGH_BACKGROUND || $this->set->tasks[$k]->type == Net_Gearman_Task::JOB_LOW_BACKGROUND) { $this->set->tasks[$k]->finished = true; $this->set->tasksCount--; } $t++; } $write = null; $except = null; $read = $this->conn; @socket_select($read, $write, $except, $socket_timeout); foreach ($read as $socket) { $err = socket_last_error($socket); // Error 11 is EAGAIN and is normal in non-blocking mode if ($err && $err != 11) { $msg = socket_strerror($err); socket_getpeername($socket, $remote_address, $remote_port); socket_getsockname($socket, $local_address, $local_port); trigger_error("socket_select failed: ({$err}) {$msg}; server: {$remote_address}:{$remote_port}", E_USER_WARNING); } socket_clear_error($socket); $resp = Net_Gearman_Connection::read($socket); if (count($resp)) { $this->handleResponse($resp, $socket, $this->set); } } } }
/** * Run a set of tasks * * @param object $set A set of tasks to run * * @return void * @see Net_Gearman_Set, Net_Gearman_Task */ public function runSet(Net_Gearman_Set $set) { $totalTasks = $set->tasksCount; $taskKeys = array_keys($set->tasks); $t = 0; while (!$set->finished()) { if ($t < $totalTasks) { $k = $taskKeys[$t]; $this->submitTask($set->tasks[$k]); if ($set->tasks[$k]->type == Net_Gearman_Task::JOB_BACKGROUND) { $set->tasks[$k]->finished = true; $set->tasksCount--; } $t++; } $write = null; $except = null; $read = $this->conn; socket_select($read, $write, $except, 10); foreach ($read as $socket) { $resp = Net_Gearman_Connection::read($socket); if (count($resp)) { $this->handleResponse($resp, $socket, $set); } } } }
/** * Blocking socket read * * @param resource $socket The socket to read from * @param float $timeout The timeout for the read * * @throws Net_Gearman_Exception on timeouts * @return array */ public static function blockingRead($socket, $timeout = 3) { static $cmds = array(); $tv_sec = floor($timeout % 1000); $tv_usec = $timeout * 1000; $start = microtime(true); while (count($cmds) == 0) { if (microtime(true) - $start > $timeout) { throw new Net_Gearman_Exception('Blocking read timed out'); } $write = null; $except = null; $read = array($socket); $ts_start = microtime(true); @socket_select($read, $write, $except, $tv_sec, $tv_usec); $elapsed = (microtime(true) - $ts_start) * 1000; if (rand(1, 1000) == 1) { socket_getpeername($socket, $addr); } foreach ($read as $s) { $cmds[] = Net_Gearman_Connection::read($s); } } return array_shift($cmds); }
/** * Run a set of tasks * * @param object $set A set of tasks to run * @param int $timeout Time in seconds for the socket timeout. Max is 10 seconds * * @return void * @see Net_Gearman_Set, Net_Gearman_Task */ public function runSet(Net_Gearman_Set $set, $timeout = null) { $totalTasks = $set->tasksCount; $taskKeys = array_keys($set->tasks); $t = 0; if ($timeout !== null) { $socket_timeout = min(10, (int) $timeout); } else { $socket_timeout = 10; } while (!$set->finished()) { if ($timeout !== null) { if (empty($start)) { $start = microtime(true); } else { $now = microtime(true); if ($now - $start >= $timeout) { break; } } } if ($t < $totalTasks) { $k = $taskKeys[$t]; $this->submitTask($set->tasks[$k]); if ($set->tasks[$k]->type == Net_Gearman_Task::JOB_BACKGROUND || $set->tasks[$k]->type == Net_Gearman_Task::JOB_HIGH_BACKGROUND || $set->tasks[$k]->type == Net_Gearman_Task::JOB_LOW_BACKGROUND) { $set->tasks[$k]->finished = true; $set->tasksCount--; } $t++; } $write = null; $except = null; $read = $this->conn; socket_select($read, $write, $except, $socket_timeout); foreach ($read as $socket) { $resp = Net_Gearman_Connection::read($socket); if (count($resp)) { $this->handleResponse($resp, $socket, $set); } } } }