/** * Attempt to run the given job. * * @param Job $job * * @return void */ public function run(Job &$job) { $this->getPHPQ()->getLogger()->info(sprintf('Running job #%d', $job->getId())); JobReflector::setFailed($job, false); JobReflector::setFinished($job, null); JobReflector::setHasResult($job, false); JobReflector::setResult($job, null); try { $job->setUp(); $job->perform($this->getPHPQ()->getContainer()); $job->tearDown(); } catch (\Exception $e) { JobReflector::setFailed($job, true); } $this->getPHPQ()->getDriver()->persistJobState($job); $this->getPHPQ()->getDriver()->detach($job); return; }
/** * Initialise the given job object. * @internal * * @param Job $job * * @return void */ private final function initialiseJobObject(Job &$job) { $created = new DateTimeImmutable(); Reflection\JobReflector::setQueue($job, $this); Reflection\JobReflector::setCreated($job, $created); Reflection\JobReflector::setSchedule($job, $created); Reflection\JobReflector::setTimeout($job, null); Reflection\JobReflector::setVersion($job, 0); return; }
/** * Create a job from te given column set. * * @param $columns * * @return Job * @throws ReflectionException */ private function hydrateJobObject($columns) { // Figure out the class of the job $class = $columns[$this->columns['__CLASS__']]; if (!class_exists($class)) { throw new ReflectionException(sprintf('Job class [%s] does not exist', $class)); } try { $job = new $class(); } catch (\Exception $e) { throw new ReflectionException(sprintf('Could not instantiate object of type [%s]', $class)); } if (!$job instanceof Job) { throw new ReflectionException(sprintf('[%s] is not an instance of %s', $class, Job::class)); } $queue = $this->getPHPQ()->getQueue($columns[$this->columns[JobReflector::PROPERTY_QUEUE]]); $parameters = $columns[$this->columns[JobReflector::PROPERTY_PARAMETERS]]; $result = $columns[$this->columns[JobReflector::PROPERTY_RESULT]]; $timeout = $columns[$this->columns[JobReflector::PROPERTY_TIMEOUT]]; $lastAttempt = $columns[$this->columns[JobReflector::PROPERTY_LAST_ATTEMPT]]; JobReflector::setId($job, $columns[$this->columns[JobReflector::PROPERTY_ID]]); JobReflector::setQueue($job, $queue); JobReflector::setCreated($job, new DateTimeImmutable($columns[$this->columns[JobReflector::PROPERTY_CREATED]])); JobReflector::setSchedule($job, new DateTimeImmutable($columns[$this->columns[JobReflector::PROPERTY_SCHEDULE]])); JobReflector::setFailed($job, $columns[$this->columns[JobReflector::PROPERTY_FAILED]]); JobReflector::setFinished($job, $columns[$this->columns[JobReflector::PROPERTY_FINISHED]]); JobReflector::setResult($job, $result ? json_decode($result, true) : null); JobReflector::setProgress($job, $columns[$this->columns[JobReflector::PROPERTY_PROGRESS]]); JobReflector::setLastAttempt($job, $lastAttempt ? new DateTimeImmutable($lastAttempt) : null); JobReflector::setTimeout($job, $timeout ? new DateTimeImmutable($timeout) : null); JobReflector::setRetryCount($job, $columns[$this->columns[JobReflector::PROPERTY_RETRY_COUNT]]); JobReflector::setParameters($job, $parameters ? json_decode($parameters, true) : array()); // Track the state of this job $this->jobs[spl_object_hash($job)] = clone $job; return $job; }
/** * @inheritdoc */ public function reserveJob($queues = null, $blocking = true, $timeout = 0) { if ($queues) { $queues = array_map(function ($queue) { return $this->getQueueKeyPrefix() . $queue; }, $queues); } else { $queues = $this->getQueueKeyNames(); } if ($blocking) { $output = $this->client->blpop($queues, $timeout); if (!$output) { return null; } list($key, $json) = $output; $job = JobReflector::fromJSON($json); JobReflector::setQueue($job, $this->getQueueForKey($key)); return $job; } else { foreach ($queues as $key) { $json = $this->client->lpop($queues); if ($json) { $job = JobReflector::fromJSON($json); JobReflector::setQueue($job, $this->getQueueForKey($key)); return $job; } } return null; } }