Esempio n. 1
0
 /**
  * Schedule the given job for future execution in this queue.
  *
  * @param Job                                 $job
  * @param DateTimeImmutable $schedule
  *
  * @return $this
  */
 public function schedule(Job &$job, DateTimeImmutable $schedule)
 {
     $this->getPHPQ()->getLogger()->info('Scheduling job');
     $this->initialiseJobObject($job);
     Reflection\JobReflector::setSchedule($job, $schedule);
     $id = $this->phpq->getDriver()->addJobToQueue($this, $job);
     Reflection\JobReflector::setId($job, $id);
     $this->getPHPQ()->getLogger()->debug(sprintf('Scheduled job #%d', $job->getId()));
     return $this;
 }
Esempio n. 2
0
 /**
  * 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;
 }