Skip to content

Commit

Permalink
Add time limit option
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsykun committed Jan 18, 2020
1 parent 842c9d0 commit c8d6b54
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Command/CronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected function configure(): void
->addOption('command', null, InputOption::VALUE_OPTIONAL, 'Run only selected command')
->addOption('demand', null, InputOption::VALUE_NONE, 'Start cron scheduler every one minute without exit')
->addOption('group', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Run schedules for specific groups.')
->addOption('time-limit', null, InputOption::VALUE_OPTIONAL, 'Run cron scheduler during this time (sec.)')
->setDescription('Runs currently schedule cron');
}

Expand All @@ -51,11 +52,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('demand')) {
$output->writeln('Run scheduler without exit');
while ($now = time()) {
$startTime = time();
$timeLimit = $input->getOption('time-limit');

while ($now = time() and (null === $timeLimit || $now - $startTime < $timeLimit)) {
sleep(60 - ($now % 60));
$startTime = microtime(true);
$runAt = microtime(true);
$this->scheduler($input, $output);
$output->writeln(sprintf('All schedule tasks completed in %.3f seconds', microtime(true) - $startTime), OutputInterface::VERBOSITY_VERBOSE);
$output->writeln(sprintf('All schedule tasks completed in %.3f seconds', microtime(true) - $runAt), OutputInterface::VERBOSITY_VERBOSE);
}
} else {
$this->scheduler($input, $output);
Expand Down

0 comments on commit c8d6b54

Please sign in to comment.