/** * Begin executing one or more job * * @param Resqee_Job|array $jobs A Resqee_Job or an array of Resqee_Job objects * * @return array The IDs of the jobs we exectued */ private function execute($jobs) { if (!is_array($jobs)) { $jobs = array($jobs); } $socketIndex = count($this->sockets); foreach ($jobs as $jobId => $job) { $this->jobToSocketMapping[$jobId] = $socketIndex; } $postData = Resqee::KEY_POST_JOB_CLASS_PARAM . '=' . get_class($this) . '&' . Resqee::KEY_POST_JOB_PARAM . '=' . urlencode(serialize($jobs)); $forceServer = $jobServer = null; while ($jobServer == null) { try { $jobServer = $this->getJobServer($forceServer); $forceServer = true; $this->sockets[$socketIndex] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); } catch (Resqee_Exception $e) { throw $e; } // failed creating a socket. disable the server if ($this->sockets[$socketIndex] === false) { Resqee_Config_Jobs::disableServer($jobServer); socket_close($this->sockets[$socketIndex]); $this->sockets[$socketIndex] = $jobServer = null; continue; } $res = @socket_connect($this->sockets[$socketIndex], $jobServer['host'], $jobServer['port']); // failed connecting to the socket if ($res === false) { Resqee_Config_Jobs::disableServer($jobServer); socket_close($this->sockets[$socketIndex]); $this->sockets[$socketIndex] = $jobServer = null; continue; } } socket_set_block($this->sockets[$socketIndex]); socket_set_option($this->sockets[$socketIndex], SOL_TCP, 1, 1); $headers = array("POST /job HTTP/1.1", "Host: {$jobServer['host']}", 'User-Agent: Resqee Client', 'Connection: Close', 'Content-Length: ' . strlen($postData), "Content-Type: application/x-www-form-urlencoded", "Connection: close"); socket_write($this->sockets[$socketIndex], implode("\r\n", $headers)); socket_write($this->sockets[$socketIndex], "\r\n\r\n"); socket_write($this->sockets[$socketIndex], $postData); return array_keys($jobs); }
/** * Get an instance * * @return Resqee_Config_Jobs */ public static function getInstance() { if (self::$instance == null) { self::$instance = new Resqee_Config_Jobs(); } return self::$instance; }