示例#1
0
 /**
  * @param \DelayedJobs\DelayedJob\Job $job
  * @return \DelayedJobs\DelayedJob\Job|bool
  */
 public function enqueue(Job $job)
 {
     $jobId = time() + mt_rand(0, time());
     $job->setId($jobId);
     static::$_jobs[$jobId] = $job;
     return $job;
 }
示例#2
0
 /**
  * @param \DelayedJobs\DelayedJob\Job $job Job to publish
  * @return void
  */
 public function publishJob(Job $job)
 {
     $delay = $job->getRunAt()->isFuture() ? Time::now()->diffInSeconds($job->getRunAt(), false) * 1000 : 0;
     //Invert the priority because Rabbit does things differently
     $jobPriority = $this->_manager->config('maximum.priority') - $job->getPriority();
     $jobData = ['priority' => $jobPriority > 0 ? $jobPriority : 0, 'delay' => $delay, 'payload' => ['id' => $job->getId()]];
     $this->getDriver()->publishJob($jobData);
 }
示例#3
0
 public function loadJob(Job $job)
 {
     $jobData = $this->_table()->find()->where(['id' => $job->getId()])->hydrate(false)->first();
     if ($jobData === null) {
         throw new JobNotFoundException(sprintf('Job with id "%s" does not exist in the datastore.', $job->getId()));
     }
     return $job->setData($jobData);
 }
示例#4
0
 protected function _failedJob(Job $job, $exc)
 {
     if ($this->param('debug')) {
         throw $exc;
     }
     $this->out(sprintf('<error> - Execution failed</error> :: <info>%s</info>', $exc->getMessage()), 1, Shell::VERBOSE);
     $this->out($exc->getTraceAsString(), 1, Shell::VERBOSE);
     $this->djLog(__('Failed {0} because {1}', $job->getId(), $exc->getMessage()));
 }
示例#5
0
 /**
  * @param \DelayedJobs\DelayedJob\Job $job The job that is being run.
  * @param \Cake\Console\Shell|null $shell An instance of the shell that the job is run in
  * @return bool
  */
 public function __invoke(Job $job, Shell $shell = null)
 {
     sleep(2);
     $time = (new Time())->i18nFormat();
     if ($job->getPayload('type') === 'success') {
         return 'Successful test at ' . $time;
     } else {
         throw new \Exception('Failing test at ' . $time . ' because ' . $job->getPayload()['type']);
     }
 }
示例#6
0
 /**
  * Checks if there already is a job with the same worker waiting
  *
  * @param \DelayedJobs\DelayedJob\Job $job Job to check
  * @return bool
  */
 public function isSimilarJob(Job $job)
 {
     $quoting = $this->connection()->driver()->autoQuoting();
     $this->connection()->driver()->autoQuoting(true);
     $conditions = ['worker' => $job->getWorker(), 'status IN' => [Job::STATUS_BUSY, Job::STATUS_NEW, Job::STATUS_FAILED, Job::STATUS_UNKNOWN]];
     if (!empty($job->getId())) {
         $conditions['id !='] = $job->getId();
     }
     $exists = $this->exists($conditions);
     $this->connection()->driver()->autoQuoting($quoting);
     return $exists;
 }
示例#7
0
 /**
  * @param \DelayedJobs\DelayedJob\Job $job Job being pushed to broker
  * @return bool|mixed
  */
 protected function _pushToBroker(Job $job)
 {
     if ($job->getId() === null) {
         throw new EnqueueException('Job has not been persisted.');
     }
     try {
         $event = $this->dispatchEvent('DelayedJobs.beforeJobQueue', [$job]);
         if ($event->isStopped()) {
             return $event->result;
         }
         $this->getMessageBroker()->publishJob($job);
         $this->dispatchEvent('DelayedJobs.afterJobQueue', [$job]);
         return true;
     } catch (\Exception $e) {
         Log::emergency(__('RabbitMQ server is down. Response was: {0} with exception {1}. Job #{2} has not been queued.', $e->getMessage(), get_class($e), $job->getId()));
         throw new EnqueueException('Could not push job to broker.');
     }
 }
示例#8
0
 public function afterExecute(Event $event, Job $job, $result, $duration)
 {
     $this->_lastJob = $job->getId();
     $this->_jobCount++;
     $this->out('');
     if ($result instanceof \Throwable) {
         $this->out(sprintf('<error> - Execution failed</error> :: <info>%s</info>', $result->getMessage()), 1, Shell::VERBOSE);
         $this->out($result->getTraceAsString(), 1, Shell::VERBOSE);
     } else {
         $this->out(sprintf('<success> - Execution successful</success> :: <info>%s</info>', $result), 1, Shell::VERBOSE);
     }
     $this->out(sprintf(' - Took: %.2f seconds', $duration / 1000), 1, Shell::VERBOSE);
     pcntl_signal_dispatch();
     $this->_timeLastJob = microtime(true);
     $this->_checkSuicideStatus();
 }
示例#9
0
 public function nack(Job $job, $requeue = true)
 {
     $message = $job->getBrokerMessage();
     if ($message === null) {
         return;
     }
     $message->delivery_info['channel']->basic_nack($message->delivery_info['delivery_tag'], false, $requeue);
 }