Loop frequency
This commit is contained in:
@ -5,4 +5,5 @@ return [
|
||||
DateTimeZone::class => function (ContainerInterface $container) {
|
||||
return new DateTimeZone($container->get('TZ') ?? 'America/Santiago');
|
||||
},
|
||||
'loopFrequency' => 60
|
||||
];
|
||||
|
@ -16,6 +16,7 @@ return [
|
||||
return new Incoviba\Command\BaseLoop(
|
||||
$container->get('LoopLogger'),
|
||||
$container->get(Incoviba\Service\Schedule::class),
|
||||
$container->get('loopFrequency'),
|
||||
$container->get(DateTimeZone::class),
|
||||
);
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ use Incoviba\Service\Schedule;
|
||||
class BaseLoop extends Console\Command\Command
|
||||
{
|
||||
public function __construct(protected LoggerInterface $logger, protected Schedule $scheduleService,
|
||||
protected int $timeout = 5 * 60,
|
||||
protected DateTimeZone $timezone,
|
||||
?string $name = null)
|
||||
{
|
||||
@ -26,7 +27,7 @@ class BaseLoop extends Console\Command\Command
|
||||
{
|
||||
$this->write($output, 'Running loop...');
|
||||
|
||||
$this->waitNextMinute($output);
|
||||
$this->waitNextTimeout($output);
|
||||
|
||||
$this->write($output, 'Starting loop...');
|
||||
while (true) {
|
||||
@ -35,19 +36,19 @@ class BaseLoop extends Console\Command\Command
|
||||
foreach ($commands as $command) {
|
||||
$this->runCommand($input, $output, $command);
|
||||
}
|
||||
$this->waitNextMinute($output, $start);
|
||||
$this->waitNextTimeout($output, $start);
|
||||
}
|
||||
return self::SUCCESS;
|
||||
}
|
||||
protected function waitNextMinute(Console\Output\OutputInterface $output, ?DateTimeInterface $start = null): void
|
||||
protected function waitNextTimeout(Console\Output\OutputInterface $output, ?DateTimeInterface $start = null): void
|
||||
{
|
||||
// wait for next minute
|
||||
if ($start === null) {
|
||||
$start = new DateTimeImmutable('now', $this->timezone);
|
||||
}
|
||||
$nextMinute = new DateTimeImmutable($start->format('Y-m-d H:i:00'), $this->timezone);
|
||||
$nextMinute = $nextMinute->add(new \DateInterval('PT1M'));
|
||||
$diff = $nextMinute->getTimestamp() - $start->getTimestamp();
|
||||
$nextTimeout = new DateTimeImmutable($start->format('Y-m-d H:i:00'), $this->timezone);
|
||||
$nextTimeout = $nextTimeout->add(new \DateInterval("PT{$this->timeout}S"));
|
||||
$diff = $nextTimeout->getTimestamp() - $start->getTimestamp();
|
||||
if ($diff > 0) {
|
||||
$this->logger->debug("Waiting {$diff} seconds...");
|
||||
sleep($diff);
|
||||
|
Reference in New Issue
Block a user