Files
remote_ip/app/common/Command/Watch.php
2023-06-18 19:20:06 -04:00

51 lines
1.5 KiB
PHP

<?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;
use Symfony\Component\Console\Style\SymfonyStyle;
use function Safe\shell_exec;
#[AsCommand(
name: 'watch',
hidden: false
)]
class Watch extends Command
{
public function __construct(protected string $command, protected string $period, protected LoggerInterface $logger, string $name = 'watch')
{
parent::__construct($name);
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->title('Watch');
$period = new DateInterval($this->period);
$current = new DateTimeImmutable();
$io->info('Starting');
while(true) {
$now = new DateTimeImmutable();
if ($now->diff($current) === $period) {
$io->info('Running Update');
$this->runUpdate();
$current = $now;
}
$wait = (new DateTimeImmutable((new DateTimeImmutable())->format('Y-m-d H:i:0')))->add(new DateInterval('PT1M'));
time_sleep_until($wait->getTimestamp());
}
return Command::SUCCESS;
}
protected function runUpdate(): void
{
$command = "{$this->command} update";
$this->logger->info("Running '{$command}'");
shell_exec($command);
}
}