Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function dequeue(SerializerInterface $serializer)
 {
     $job = $this->client->reserve();
     if ($job) {
         return new BeanstalkdJob($this->client, $job, $serializer);
     }
     return null;
 }
 /**
  * {@inheritDoc}
  */
 public function reserve($timeout = null)
 {
     if ($this->dispatcher) {
         $this->dispatcher->dispatch(CommandEvent::RESERVE, new CommandEvent($this, ['timeout' => $timeout]));
     }
     return $this->pheanstalk->reserve($timeout);
 }
 /**
  * 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>');
 }
Exemple #4
0
 protected function dequeue()
 {
     $job = $this->pheanstalk->reserve(1);
     $data = json_decode($job->getData(), true);
     return $data;
 }
 /**
  * @param int $timeout
  *
  * @return Job|bool A job if there is one, false otherwise
  */
 public function get($timeout = null)
 {
     return $this->pheanstalk->reserve($timeout);
 }