/** * 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']); }
/** * 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)); }
/** * 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']; } } $job = Net_Gearman_Job::factory($name, $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); } catch (Net_Gearman_Job_Exception $e) { $job->fail(); $this->fail($handle, $name, $e); } // Force the job's destructor to run $job = null; return true; }
/** * Submit a task to Gearman * * @param object $task Task to submit to Gearman * * @return void * @see Net_Gearman_Task, Net_Gearman_Client::runSet() */ protected function submitTask(Net_Gearman_Task $task) { switch ($task->type) { case Net_Gearman_Task::JOB_LOW: $type = 'submit_job_low'; break; case Net_Gearman_Task::JOB_LOW_BACKGROUND: $type = 'submit_job_low_bg'; break; case Net_Gearman_Task::JOB_HIGH_BACKGROUND: $type = 'submit_job_high_bg'; break; case Net_Gearman_Task::JOB_BACKGROUND: $type = 'submit_job_bg'; break; case Net_Gearman_Task::JOB_HIGH: $type = 'submit_job_high'; break; default: $type = 'submit_job'; break; } // if we don't have a scalar // json encode the data if (!is_scalar($task->arg)) { $arg = @json_encode($task->arg); } else { $arg = $task->arg; } $params = array('func' => $task->func, 'uniq' => $task->uniq, 'arg' => $arg); $s = $this->getConnection($task->uniq); Net_Gearman_Connection::send($s, $type, $params); $s_key = (int) $s; if (!is_array(Net_Gearman_Connection::$waiting[$s_key])) { Net_Gearman_Connection::$waiting[$s_key] = array(); } array_push(Net_Gearman_Connection::$waiting[$s_key], $task); }
/** * 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); } /** * The response can be empty during shut down. We don't need to proceed * in those cases. */ if (!is_array($resp) || empty($resp) || 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']; } } try { $job = Net_Gearman_Job::factory($name, $socket, $handle, $this->initParams[$name]); $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); } catch (Net_Gearman_Job_Exception $e) { // If the factory method call fails, we won't have a job. if (isset($job) && $job instanceof Net_Gearman_Job) { $job->fail(); } $this->fail($handle, $name, $e); throw new Net_Gearman_Job_Exception('failed need restart!'); } // Force the job's destructor to run $job = null; return true; }
/** * Submit a task to Gearman * * @param object $task Task to submit to Gearman * * @return void * @see Net_Gearman_Task, Net_Gearman_Client::runSet() */ protected function submitTask(Net_Gearman_Task $task) { switch ($task->type) { case Net_Gearman_Task::JOB_BACKGROUND: $type = 'submit_job_bg'; break; case Net_Gearman_Task::JOB_HIGH: $type = 'submit_job_high'; break; default: $type = 'submit_job'; break; } $params = array('func' => $task->func, 'uniq' => $task->uniq, 'arg' => json_encode($task->arg)); $s = $this->getConnection(); Net_Gearman_Connection::send($s, $type, $params); if (!is_array(Net_Gearman_Connection::$waiting[$s])) { Net_Gearman_Connection::$waiting[$s] = array(); } array_push(Net_Gearman_Connection::$waiting[$s], $task); }
/** * 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; }