public static function addJob($workers = array(), $payload = array(), $queue = 'Generic') { $queue = \PHPQueue\Base::getQueue($queue); $jobData = array_merge(array('worker' => $workers), $payload); $queue->addJob($jobData); return true; }
/** * @param \PHPQueue\Job $jobObject */ public function runJob($jobObject) { parent::runJob($jobObject); $jobQueue = \PHPQueue\Base::getQueue('Generic'); $jobQueue->resultLog->addInfo('Working job: ', $jobObject->data); $this->result_data = array_merge($jobObject->data, array('status' => 'success')); }
protected function beforeLoop() { if (empty($this->queue_name)) { throw new \PHPQueue\Exception('Queue name is invalid'); } $this->queue = \PHPQueue\Base::getQueue($this->queue_name); }
public function work() { $newJob = null; $queue = \PHPQueue\Base::getQueue($this->queue_name); try { $newJob = \PHPQueue\Base::getJob($queue); fwrite(STDOUT, "===========================================================\n"); fwrite(STDOUT, "Next Job:\n"); var_dump($newJob); } catch (Exception $ex) { fwrite(STDOUT, "Error: " . $ex->getMessage() . "\n"); } if (empty($newJob)) { fwrite(STDOUT, "Notice: No Job found.\n"); return; } try { if (empty($newJob->worker)) { throw new \PHPQueue\Exception("No worker declared."); } if (is_string($newJob->worker)) { $result_data = $this->processWorker($newJob->worker, $newJob); } else { if (is_array($newJob->worker)) { foreach ($newJob->worker as $worker_name) { $result_data = $this->processWorker($worker_name, $newJob); $newJob->data = $result_data; } } } fwrite(STDOUT, "Updating job... \n"); return \PHPQueue\Base::updateJob($queue, $newJob->job_id, $result_data); } catch (Exception $ex) { fwrite(STDOUT, sprintf("\nError occured: %s\n", $ex->getMessage())); $queue->releaseJob($newJob->job_id); throw $ex; } }
/** * Trigger a worker for a queue. Next item in the queue will be retrieved and worked with the appropriate worker. * @param string $queueName * @return stdClass */ protected function work($queueName = null) { $queue = \PHPQueue\Base::getQueue($queueName); try { $newJob = \PHPQueue\Base::getJob($queue); } catch (Exception $ex) { return $this->failed(405, $ex->getMessage()); } if (empty($newJob)) { return $this->failed(404, "No Job in queue."); } try { if (empty($newJob->worker)) { throw new \PHPQueue\Exception("No worker declared."); } if (is_string($newJob->worker)) { $result_data = $this->processWorker($newJob->worker, $newJob); } else { if (is_array($newJob->worker)) { foreach ($newJob->worker as $worker_name) { $result_data = $this->processWorker($worker_name, $newJob); $newJob->data = $result_data; } } } \PHPQueue\Base::updateJob($queue, $newJob->job_id, $result_data); return $this->successful(); } catch (Exception $ex) { $queue->releaseJob($newJob->job_id); return $this->failed($ex->getCode(), $ex->getMessage()); } }