public function testJobStats()
 {
     $job = new Job(1234, 'data');
     $stats = ['foo' => 'bar'];
     $this->pheanstalk->expects($this->once())->method('statsJob')->with($job)->will($this->returnValue($stats));
     $this->assertEquals($stats, $this->manager->getJobStats($job));
 }
 /**
  * @param OutputInterface $output
  * @param string          $action
  * @param int             $limit
  */
 protected function inspect(OutputInterface $output, $action, $limit = null)
 {
     $output->writeln(sprintf('Inspecting jobs for the <info>%s</info> action', $action));
     $this->manager->watchOnly([$action]);
     $jobs = [];
     while ($job = $this->manager->get(1)) {
         $output->writeln(sprintf('<info>%d</info>: <comment>%s</comment>', $job->getId(), $job->getData()));
         $jobs[] = $job;
         if (null !== $limit && sizeof($jobs) >= $limit) {
             break;
         }
     }
     $output->writeln('Releasing the jobs back to the queue, <error>don\'t cancel this action!</error>');
     foreach ($jobs as $job) {
         $stats = $this->manager->getJobStats($job);
         $this->manager->getPheanstalk()->release($job, $stats['pri']);
     }
 }
 /**
  * @inheritdoc
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->output = $output;
     $dispatcher = $this->manager->getDispatcher();
     $maxMemory = intval($input->getOption('max-memory')) * 1024 * 1024;
     $maxTime = intval($input->getOption('max-time'));
     $maxJobs = intval($input->getOption('limit'));
     $batchSize = intval($input->getOption('batch-size'));
     $minDuration = intval($input->getOption('min-duration'));
     $logger = $this->manager->getLogger();
     $this->attachConsoleHandler($logger, $output);
     // configure pheanstalk to watch the right tubes
     $this->watchActions($input->getOption('action'), $input->getOption('exclude'));
     $start = time();
     $jobsCompleted = 0;
     // wait for job, timeout after 1 minute
     $timeout = 60;
     $this->output(sprintf('Waiting at most <info>%d seconds</info> for a reserved job...', $timeout));
     $exit = 0;
     while ($job = $this->manager->get($timeout)) {
         $stats = $this->manager->getJobStats($job);
         $timeStart = microtime(true) * 1000;
         $memStart = memory_get_usage(true);
         try {
             $this->output(sprintf('Working job <info>%d</info> for action <comment>%s</comment> with payload <info>%s</info>', $job->getId(), $stats['tube'], $job->getData()));
             $result = $this->manager->executeJob($job);
         } catch (AbortException $e) {
             $message = 'Worker aborted ' . ($e->getReason() ? 'with reason: ' . $e->getReason() : 'without a given reason');
             $this->output($message);
             $exit = 1;
             break;
         }
         $duration = microtime(true) * 1000 - $timeStart;
         $usage = memory_get_usage(true) - $memStart;
         $message = sprintf('Completed job <info>%d</info> in <comment>%dms</comment> using <comment>%s</comment> with result: <info>%s</info>', $job->getId(), $duration, $this->formatBytes($usage), json_encode($result, JSON_UNESCAPED_SLASHES));
         $this->output($message);
         ++$jobsCompleted;
         // intermediate flush
         if ($jobsCompleted % $batchSize === 0) {
             $this->output('Batch complete', OutputInterface::VERBOSITY_VERBOSE);
             $dispatcher->dispatch(WorkerEvents::FLUSH);
         }
         if ($jobsCompleted >= $maxJobs) {
             $this->output(sprintf('Maximum number of jobs completed (%d)', $maxJobs), OutputInterface::VERBOSITY_VERBOSE);
             break;
         }
         if ($maxMemory > 0 && memory_get_usage(true) > $maxMemory) {
             $this->output(sprintf('Memory peak of %dMB reached (peak: %sMB)', $maxMemory / 1024 / 1024, memory_get_usage(true) / 1024 / 1024), OutputInterface::VERBOSITY_VERBOSE);
             break;
         }
         if ($maxTime > 0 && time() - $start > $maxTime) {
             $this->output(sprintf('Maximum execution time of %ds reached', $maxTime), OutputInterface::VERBOSITY_VERBOSE);
             break;
         }
     }
     // flush remaining
     $dispatcher->dispatch(WorkerEvents::FLUSH);
     // make sure worker doesn't quit to quickly, or supervisor will mark it
     // as a failed restart, and put the worker in FATAL state.
     $duration = time() - $start;
     if ($duration < $minDuration) {
         $this->output(sprintf('Sleeping until worker has run for at least %s seconds', $minDuration));
         sleep($minDuration - $duration);
     }
     $this->output('Shutting down worker');
     return $exit;
 }