Files
remote_ip/app/common/Command/Watch.php

65 lines
2.1 KiB
PHP
Raw Normal View History

2023-06-16 21:44:35 -04:00
<?php
namespace ProVM\Command;
use DateTimeImmutable;
use DateInterval;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
2023-06-18 23:26:44 -04:00
use Symfony\Component\Console\Style\StyleInterface;
2023-06-18 19:20:06 -04:00
use Symfony\Component\Console\Style\SymfonyStyle;
2023-06-16 22:35:41 -04:00
use function Safe\shell_exec;
2023-06-16 21:44:35 -04:00
#[AsCommand(
name: 'watch',
hidden: false
)]
class Watch extends Command
{
2023-06-18 19:20:06 -04:00
public function __construct(protected string $command, protected string $period, protected LoggerInterface $logger, string $name = 'watch')
2023-06-16 21:44:35 -04:00
{
parent::__construct($name);
}
public function execute(InputInterface $input, OutputInterface $output): int
{
2023-06-18 19:20:06 -04:00
$io = new SymfonyStyle($input, $output);
$io->title('Watch');
2023-06-16 21:44:35 -04:00
$period = new DateInterval($this->period);
2023-06-18 23:26:44 -04:00
$current = $this->updateTime($period);
2023-06-18 19:20:06 -04:00
$io->info('Starting');
2023-06-18 23:26:44 -04:00
$io->info("Start: {$current->format('Y-m-d H:i:s')}");
2023-06-16 21:44:35 -04:00
while(true) {
2023-06-18 23:26:44 -04:00
$io->info('Running Update');
$this->runUpdate();
$current = $this->updateTime($period);
$wait = $current->add($period);
$io->text("Waiting until {$wait->format('Y-m-d H:i:s')}");
2023-06-18 19:20:06 -04:00
time_sleep_until($wait->getTimestamp());
2023-06-16 21:44:35 -04:00
}
return Command::SUCCESS;
}
2023-06-18 23:26:44 -04:00
protected function updateTime(DateInterval $interval): DateTimeImmutable
{
$t0 = new DateTimeImmutable((new DateTimeImmutable())->format('Y-m-d 00:00:00'));
$tf = new DateTimeImmutable((new DateTimeImmutable())->add(new DateInterval('P1D'))->format('Y-m-d 00:00:00'));
$now = new DateTimeImmutable();
for ($t = $t0; $t < $tf; $t = $t->add($interval)) {
$t1 = $t->add($interval);
if ($t < $now and $now < $t1) {
return $t;
}
}
return $now;
}
protected function runUpdate(StyleInterface $io): void
2023-06-16 21:44:35 -04:00
{
2023-06-18 19:20:06 -04:00
$command = "{$this->command} update";
$this->logger->info("Running '{$command}'");
2023-06-18 23:26:44 -04:00
$result = shell_exec($command);
$io->note($result);
2023-06-16 21:44:35 -04:00
}
}