Skip to content

Commit

Permalink
Merge pull request #52 from KingCrunch/custom-idle-time
Browse files Browse the repository at this point in the history
CLI-option to set custom time to sleep when queue ran out of jobs
  • Loading branch information
schmittjoh committed Mar 6, 2014
2 parents c19f5ba + 1f28492 commit 77b67f0
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;
}

Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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.
}

Expand Down

0 comments on commit 77b67f0

Please # to comment.