public function __construct() { parent::__construct(); $config_class = \PHPQueue\Base::$config_class; $config = $config_class::getConfig($this->queue_type); $this->dataSource = \PHPQueue\Base::backendFactory($config['backend'], $config); $this->resultLog = \PHPQueue\Logger::createLogger('MainLogger', \PHPQueue\Logger::INFO, sprintf('%s/QueueLog-%s-%s.log', $config_class::getLogRoot(), $this->queue_type, date('Ymd'))); }
public function workJob() { $sleepTime = self::RUN_USLEEP; $newJob = null; try { $newJob = Base::getJob($this->queue); } catch (\Exception $ex) { $this->logger->addError($ex->getMessage()); $sleepTime = self::RUN_USLEEP * 5; } if (empty($newJob)) { $this->logger->addNotice("No Job found."); $sleepTime = self::RUN_USLEEP * 10; } else { try { if (empty($newJob->worker)) { throw new Exception("No worker declared."); } if (is_string($newJob->worker)) { $result_data = $this->processWorker($newJob->worker, $newJob); } elseif (is_array($newJob->worker)) { $this->logger->addInfo(sprintf("Running chained new job (%s) with workers", $newJob->job_id), $newJob->worker); foreach ($newJob->worker as $worker_name) { $result_data = $this->processWorker($worker_name, $newJob); $newJob->data = $result_data; } } return Base::updateJob($this->queue, $newJob->job_id, $result_data); } catch (\Exception $ex) { $this->logger->addError($ex->getMessage()); $this->logger->addInfo(sprintf('Releasing job (%s).', $newJob->job_id)); $this->queue->releaseJob($newJob->job_id); $sleepTime = self::RUN_USLEEP * 5; } } $this->logger->addInfo('Sleeping ' . ceil($sleepTime / 1000000) . ' seconds.'); usleep($sleepTime); }
/** * @param \PHPQueue\JobQueue $queue * @param string $jobId * @param mixed $resultData * @return bool|void * @throws \Exception */ public static function updateJob(JobQueue $queue, $jobId = null, $resultData = null) { $status = false; try { $queue->beforeUpdate(); $queue->updateJob($jobId, $resultData); $status = $queue->clearJob($jobId); $queue->afterUpdate(); } catch (\Exception $ex) { $queue->onError($ex); $queue->releaseJob($jobId); throw $ex; } return $status; }