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