/** * {@inheritDoc} */ public function watchOnly($tube) { if ($this->dispatcher) { $this->dispatcher->dispatch(CommandEvent::WATCH_ONLY, new CommandEvent($this, ['tube' => $tube])); } $this->pheanstalk->watchOnly($tube); return $this; }
/** * 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>'); }