/**
  * {@inheritDoc}
  */
 public function release($job, $priority = self::DEFAULT_PRIORITY, $delay = self::DEFAULT_DELAY)
 {
     if ($this->dispatcher) {
         $this->dispatcher->dispatch(CommandEvent::RELEASE, new CommandEvent($this, ['job' => $job, 'priority' => $priority, 'delay' => $delay]));
     }
     $this->pheanstalk->release($job, $priority, $delay);
     return $this;
 }
 /**
  * Reschedules a job.
  *
  * @param Job       $job
  * @param \DateTime $date
  * @param integer   $priority
  *
  * @throws \InvalidArgumentException When `$date` is in the past
  */
 public function reschedule(Job $job, \DateTime $date, $priority = PheanstalkInterface::DEFAULT_PRIORITY)
 {
     if ($date < new \DateTime()) {
         throw new \InvalidArgumentException(sprintf('You cannot reschedule a job in the past (got %s, and the current date is %s)', $date->format(DATE_ISO8601), date(DATE_ISO8601)));
     }
     $this->pheanstalk->release($job, $priority, $date->getTimestamp() - time());
     $this->logJob($job->getId(), sprintf('Rescheduled job for %s', $date->format('Y-m-d H:i:s')));
 }
 /**
  * Watch for jobs on the given tube
  *
  * @return void
  */
 public function watchForJobs()
 {
     // Time the worker will retire
     $retireTime = time() + $this->ttl;
     if (null !== $this->tube) {
         $this->pheanstalk->watchOnly($this->tube);
     }
     // Watch the Queue
     while (!$this->isTerminated()) {
         $job = $this->pheanstalk->reserve(5);
         if ($job) {
             // Let everyone know we just grabbed a job off the queue
             $this->output->writeln('<comment>Found Job ID: ' . $job->getId() . '</comment>');
             // Check the data is valid for us to process a job
             if (!$this->isValid($job)) {
                 $this->output->writeln('<comment>Invalid Job, skipping.</comment>');
                 $outcome = self::ACTION_BURY;
             } else {
                 // Output to let anyone watching know that we're starting a worker
                 $this->output->writeln('<comment>' . $this->getStartMessage($job) . '</comment>');
                 try {
                     // Process the job
                     $outcome = $this->processJob($job);
                 } catch (\Exception $e) {
                     // Output error
                     $this->output->writeln('<error>Fatal Error: ' . $e->getMessage() . '</error>');
                     // Bury the job
                     $this->pheanstalk->bury($job);
                     // Break out of while loop
                     break;
                 }
                 // Let the folks know we've completed it
                 $this->output->writeln('<comment>Job Processed.</comment>');
             }
             switch ($outcome) {
                 case self::ACTION_DELETE:
                     // Remove the job from the queue
                     $this->pheanstalk->delete($job);
                     break;
                 case self::ACTION_BURY:
                     // Remove the job from the queue
                     $this->pheanstalk->bury($job);
                     break;
                 case self::ACTION_RELEASE:
                     // Remove the job from the queue
                     $this->pheanstalk->release($job);
                     break;
             }
             $this->output->writeln('<info>Waiting for next job...</info>');
         }
         // Check if it's time to retire the worker
         if (0 !== $this->ttl && time() > $retireTime) {
             $this->retire();
         }
     }
     $this->output->writeln('<info>Exiting.</info>');
 }
 /**
  * {@inheritdoc}
  *
  * @param integer|null $priority
  */
 public function release(ManagerInterface $manager, $delay = 0, $priority = PheanstalkInterface::DEFAULT_PRIORITY)
 {
     $this->pheanstalk->release($manager->getPheanstalkJob(), $priority, $delay);
     return true;
 }