From 1f284929958fd074dd71d7be1f217d1b2b0facbb Mon Sep 17 00:00:00 2001 From: Sebastian Krebs Date: Tue, 14 Jan 2014 20:35:25 +0100 Subject: [PATCH] CLI-option to set custom time to sleep when queue ran out of jobs --- Command/RunCommand.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Command/RunCommand.php b/Command/RunCommand.php index 4f613ea6..593d8d9e 100644 --- a/Command/RunCommand.php +++ b/Command/RunCommand.php @@ -61,6 +61,7 @@ protected function configure() ->setDescription('Runs jobs from the queue.') ->addOption('max-runtime', 'r', InputOption::VALUE_REQUIRED, 'The maximum runtime in seconds.', 900) ->addOption('max-concurrent-jobs', 'j', InputOption::VALUE_REQUIRED, 'The maximum number of concurrent jobs.', 4) + ->addOption('idle-time', null, InputOption::VALUE_REQUIRED, 'Time to sleep when the queue ran out of jobs.', 2) ; } @@ -78,6 +79,11 @@ protected function execute(InputInterface $input, OutputInterface $output) throw new InvalidArgumentException('The maximum number of jobs per queue must be greater than zero.'); } + $idleTime = (integer) $input->getOption('idle-time'); + if ($idleTime <= 0) { + throw new InvalidArgumentException('Time to sleep when idling must be greater than zero.'); + } + $this->env = $input->getOption('env'); $this->verbose = $input->getOption('verbose'); $this->output = $output; @@ -90,13 +96,14 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->runJobs( $startTime, $maxRuntime, + $idleTime, $maxJobs, $this->getContainer()->getParameter('jms_job_queue.queue_options_defaults'), $this->getContainer()->getParameter('jms_job_queue.queue_options') ); } - private function runJobs($startTime, $maxRuntime, $maxJobs, array $queueOptionsDefaults, array $queueOptions) + private function runJobs($startTime, $maxRuntime, $idleTime, $maxJobs, array $queueOptionsDefaults, array $queueOptions) { while (time() - $startTime < $maxRuntime) { $this->checkRunningJobs(); @@ -109,7 +116,7 @@ private function runJobs($startTime, $maxRuntime, $maxJobs, array $queueOptionsD ); if (null === $pendingJob) { - sleep(2); + sleep($idleTime); continue 2; // Check if the maximum runtime has been exceeded. }