Ejemplo n.º 1
0
 public function run()
 {
     $endTime = time() + $this->lifetime;
     $this->logger->info(sprintf('Started listening with a lifetime of %d seconds.', $this->lifetime));
     while (time() < $endTime) {
         try {
             /** @var \Pheanstalk\Job $job */
             $job = $this->pheanstalk->reserve(1);
         } catch (Exception $exception) {
         }
         if ($job) {
             $this->logger->info(sprintf('Reserved job #%d: %s', $job->getId(), $job->getData()));
             try {
                 $data = json_decode($job->getData(), true);
                 /** @var WorkerInterface $worker */
                 $worker = $this->workerManager->get($data['worker'], $data['params']);
                 $worker->run(new Context($this, $this->logger, $data['params']));
                 $this->logger->info(sprintf('Finished job #%d', $job->getId()));
                 $this->pheanstalk->delete($job);
             } catch (Exception $exception) {
                 $this->logger->emerg('Failed to execute job #' . $job->getId(), ['exception' => $exception]);
                 $this->pheanstalk->bury($job);
             }
         }
         usleep($this->interval);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function bury($job, $priority = self::DEFAULT_PRIORITY)
 {
     if ($this->dispatcher) {
         $this->dispatcher->dispatch(CommandEvent::BURY, new CommandEvent($this, ['job' => $job, 'priority' => $priority]));
     }
     $this->pheanstalk->bury($job, $priority);
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function fail($queueName, Envelope $env)
 {
     try {
         $this->conn->bury($this->assurePheanstalkEnvelope($env)->getJob(), $this->options['fail-priority']);
     } catch (\Pheanstalk\Exception $e) {
         throw PheanstalkError::fromException($e);
     }
 }
 /**
  * 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>');
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function fail($queueName, Envelope $env)
 {
     if (!$env instanceof PheanstalkEnvelope) {
         throw new InvalidEnvelope(sprintf('%s requires that envelopes be instances of "%s", got "%s"', __CLASS__, PheanstalkEnvelope::class, get_class($env)));
     }
     try {
         $this->conn->bury($env->getJob(), $this->options['fail-priority']);
     } catch (\Pheanstalk\Exception $e) {
         throw PheanstalkError::fromException($e);
     }
 }
 /**
  * Mark a job as failed
  *
  * @access public
  * @param  Job $job
  * @return $this
  */
 public function failed(Job $job)
 {
     $beanstalkJob = new BeanstalkJob($job->getId(), $job->serialize());
     $this->beanstalk->bury($beanstalkJob);
     return $this;
 }
Ejemplo n.º 7
0
 /**
  * Puts a job into a 'buried' state, revived only by 'kick' command.
  *
  * @param Job $job
  */
 public function bury(Job $job)
 {
     $this->pheanstalk->bury($job);
     $this->logJob($job->getId(), 'Job buried');
 }
Ejemplo n.º 8
0
 /**
  * Bury the job
  *
  * @param ManagerInterface $manager
  *
  * @return boolean Always true
  */
 public function bury(ManagerInterface $manager)
 {
     $this->pheanstalk->bury($manager->getPheanstalkJob());
     return true;
 }
Ejemplo n.º 9
0
 public function buryJob($queueName, JobInterface $job)
 {
     $this->pheanstalk->bury(new Job($job->getData()['_beanstalk_id'], []));
 }