/** * 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; }
/** * 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; }