/** * 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; }
/** * 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; }