public function run() { /** @var \Octo\System\Store\JobStore $jobStore */ $jobStore = Store::get('Job'); /** @var \Octo\System\Store\ScheduledJobStore $scheduleStore */ $scheduleStore = Store::get('ScheduledJob'); // Clean up existing Scheduler jobs from the database: $jobs = $jobStore->getByType('Octo.System.Scheduler'); foreach ($jobs as $job) { if ($job->getId() != $this->job->getId()) { Manager::delete($job); } } // Create the next Scheduler job: Manager::create($this->job, Job::PRIORITY_HIGH, 5); // Schedule other jobs: $jobs = $scheduleStore->getJobsToSchedule(); foreach ($jobs as $item) { $job = new Job(); $job->setType($item->getType()); $data = json_decode($item->getData(), true); if (!empty($data) && is_array($data)) { $job->setData($data); } $job = Manager::create($job); $item->setCurrentJob($job); $scheduleStore->save($item); } return true; }
public static function delete(Job $job) { if ($job->getQueueId()) { $pheanstalk = new Pheanstalk(Config::getInstance()->get('Octo.worker.host')); try { $beanstalkJob = $pheanstalk->peek($job->getQueueId()); $pheanstalk->delete($beanstalkJob); } catch (\Exception $ex) { // Job is not in beanstalk. } } self::getStore()->delete($job); }
/** * Start the worker. */ public function run() { // Set up pheanstalk: $this->pheanstalk->watch($this->queue); $this->pheanstalk->ignore('default'); while ($this->run) { $beanstalkJob = $this->pheanstalk->reserve(900); $jobData = json_decode($beanstalkJob->getData(), true); // Delete invalid jobs: if (empty($jobData) || !is_array($jobData) || empty($jobData['type'])) { $this->pheanstalk->delete($beanstalkJob); continue; } $job = new Job($jobData); // Delete jobs we don't have handlers for: if (!array_key_exists($job->getType(), $this->handlers)) { $this->pheanstalk->delete($beanstalkJob); Manager::update($job, Job::STATUS_FAILURE, 'No handler exists for this job type.'); continue; } // Try and load the job handler: $handlerClass = $this->handlers[$job->getType()]['handler']; try { $handler = new $handlerClass($job, $this); if (!$handler instanceof Handler) { throw new \Exception('Job handler does not extend \\Octo\\Job\\Handler.'); } Manager::update($job, Job::STATUS_RUNNING); // Try and run the job: $success = $handler->run(); } catch (\Exception $ex) { $this->pheanstalk->delete($beanstalkJob); Manager::update($job, Job::STATUS_FAILURE, $ex->getMessage()); continue; } Manager::update($job, $success ? Job::STATUS_SUCCESS : Job::STATUS_FAILURE, $handler->getMessage()); // Remove the job when we're done: $this->pheanstalk->delete($beanstalkJob); } }
/** * Set CurrentJob - Accepts a Job model. * * @param $value \Octo\System\Model\Job */ public function setCurrentJobObject(\Octo\System\Model\Job $value) { return $this->setCurrentJobId($value->getId()); }