Skip to content

Commit

Permalink
some tweaks
Browse files Browse the repository at this point in the history
- makes scheduling more precise
- allows a JobScheduler service to handle multiple commands
  • Loading branch information
schmittjoh committed Feb 29, 2016
1 parent 0a0951d commit e7a1e4a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
11 changes: 6 additions & 5 deletions Command/ScheduleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function configure()
->setName('jms-job-queue:schedule')
->setDescription('Schedules jobs at defined intervals')
->addOption('max-runtime', null, InputOption::VALUE_REQUIRED, 'The maximum runtime of this command.', 3600)
->addOption('min-job-interval', null, InputOption::VALUE_REQUIRED, 'The minimum time between schedules jobs in seconds.', 60)
->addOption('min-job-interval', null, InputOption::VALUE_REQUIRED, 'The minimum time between schedules jobs in seconds.', 5)
;
}

Expand Down Expand Up @@ -54,19 +54,20 @@ protected function execute(InputInterface $input, OutputInterface $output)

$jobsLastRunAt = $this->populateJobsLastRunAt($registry->getManagerForClass(CronJob::class), $jobSchedulers);

$startedAt = $lastRunAt = time();
$startedAt = time();
while (true) {
$lastRunAt = $now = time();
$lastRunAt = microtime(true);
$now = time();
if ($now - $startedAt > $maxRuntime) {
$output->writeln('Max. runtime reached, exiting...');
break;
}

$this->scheduleJobs($output, $registry, $jobSchedulers, $jobsLastRunAt);

$timeToWait = time() - $lastRunAt + $minJobInterval;
$timeToWait = microtime(true) - $lastRunAt + $minJobInterval;
if ($timeToWait > 0) {
sleep((integer)$timeToWait);
usleep($timeToWait * 1E6);
}
}

Expand Down
10 changes: 6 additions & 4 deletions DependencyInjection/CompilerPass/JobSchedulersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public function process(ContainerBuilder $container)
{
$schedulers = array();
foreach ($container->findTaggedServiceIds('jms_job_queue.scheduler') as $id => $attributes) {
if ( ! isset($attributes[0]['command'])) {
throw new \RuntimeException(sprintf('The tag "jms_job_queue.schedulers" of service "%s" must have a "command" attribute.', $id));
}
foreach ($attributes as $attributeData) {
if (!isset($attributeData['command'])) {
throw new \RuntimeException(sprintf('The tag "jms_job_queue.schedulers" of service "%s" must have a "command" attribute.', $id));
}

$schedulers[$attributes[0]['command']] = new Reference($id);
$schedulers[$attributeData['command']] = new Reference($id);
}
}

$container->getDefinition('jms_job_queue.scheduler_registry')
Expand Down

0 comments on commit e7a1e4a

Please sign in to comment.