/dev/null &'; exec($cmd); return; } /** * @static * @param string $url Url of job * @param null|int $port [OPTIONAL] * @return bool|int Returns true on success or error code on fail */ public static function start($url, $port = null) { if (null === $port) { $port = Qs_Job::getPort(); } $ip = Qs_Job::getIp(); $timeout = Qs_Job::getTimeout(); $errorNumber = ''; $errorString = ''; $handle = fsockopen('tcp://' . $ip, $port, $errorNumber, $errorString, $timeout); if (!$handle) { $message = 'Error ' . $errorNumber . ': ' . $errorString . 'Connection failed: ' . 'tcp://' . $ip . ':' . $port; trigger_error($message, E_USER_WARNING); return Qs_Job::ERROR_CONNECTION_FAILED; } // TODO: check if written length less than string length if (false === fwrite($handle, 'START ' . $url)) { $message = 'Error ' . $errorNumber . ': ' . $errorString . 'Can not write to socket: ' . 'tcp://' . $ip . ':' . $port; trigger_error($message, E_USER_WARNING); return Qs_Job::ERROR_WRITE_SOCKET; } // TODO: check if server sent 'ACCEPTED' status fclose($handle); return true; } /** * @static * @return int */ public static function getIp() { if (null === Qs_Job::$_ip) { Qs_Job::$_ip = Qs_Job::_getConfig('ip'); } return Qs_Job::$_ip; } /** * Sets ip address of the script that handles the jobs * @static * @param string $ip * @return void */ public static function setIp($ip) { Qs_Job::$_ip = $ip; } public static function getPort() { if (null === Qs_Job::$_port) { Qs_Job::$_port = Qs_Job::_getConfig('port'); } return Qs_Job::$_port; } /** * Sets port of the script that handles the jobs * @static * @param int $port * @return void */ public static function setPort($port) { Qs_Job::$_port = $port; } public static function getTimeout() { if (null === Qs_Job::$_timeout) { Qs_Job::$_timeout = Qs_Job::_getConfig('timeout'); } return Qs_Job::$_timeout; } /** * Sets connection timeout in seconds * @param int $timeout Connection timeout in seconds * @return void */ public function setTimeout($timeout) { Qs_Job::$_timeout = $timeout; } public static function getErrorMessage($errorNumber) { switch ($errorNumber) { case Qs_Job::ERROR_CONNECTION_FAILED: $errorMessage = 'Can not connect to the job processing script'; break; case Qs_Job::ERROR_WRITE_SOCKET: $errorMessage = 'Can not write to the socket'; break; default: $errorMessage = 'Unknown error'; } return $errorMessage; } protected static function _getConfig($field, $default = null) { if (null === Qs_Job::$_config) { Qs_Job::$_config = Qs_Config::get('job', 'qs')->toArray(); } if (false === $field) { return Qs_Job::$_config; } if (array_key_exists($field, (array) Qs_Job::$_config)) { return Qs_Job::$_config[$field]; } return $default; } }