From dc3197dc9efb3150d93c513d052f529a8b457156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Barto=C5=A1?= Date: Sat, 13 Jan 2024 20:50:26 +0100 Subject: [PATCH] WorkerCommand: refactoring --- src/Command/WorkerCommand.php | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/Command/WorkerCommand.php b/src/Command/WorkerCommand.php index 29a264c..00c74bd 100644 --- a/src/Command/WorkerCommand.php +++ b/src/Command/WorkerCommand.php @@ -4,6 +4,8 @@ use Closure; use DateTimeImmutable; +use Orisai\Clock\Adapter\ClockAdapterFactory; +use Orisai\Clock\Clock; use Orisai\Clock\SystemClock; use Psr\Clock\ClockInterface; use Symfony\Component\Console\Command\Command; @@ -11,12 +13,8 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\Process; -use function array_map; use function assert; -use function escapeshellarg; -use function implode; use function ltrim; -use function usleep; use const PHP_BINARY; /** @@ -25,7 +23,7 @@ final class WorkerCommand extends Command { - private ClockInterface $clock; + private Clock $clock; private ?int $testRuns = null; @@ -39,7 +37,7 @@ final class WorkerCommand extends Command public function __construct(?ClockInterface $clock = null) { parent::__construct(); - $this->clock = $clock ?? new SystemClock(); + $this->clock = ClockAdapterFactory::create($clock ?? new SystemClock()); } public function setExecutable(string $script, string $command = 'scheduler:run'): void @@ -79,16 +77,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int { $output->writeln('Running scheduled tasks every minute.'); - $command = implode(' ', array_map(static fn (string $arg) => escapeshellarg($arg), [ - PHP_BINARY, - $input->getOption('script') ?? $this->script, - $input->getOption('command') ?? $this->command, - ])); - $lastExecutionStartedAt = $this->nullSeconds($this->clock->now()->modify('-1 minute')); $executions = []; while (true) { - usleep(100_000); + $this->clock->sleep(0, 100); $currentTime = $this->clock->now(); @@ -97,7 +89,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int && $this->nullSeconds($currentTime) != $lastExecutionStartedAt && $this->testRuns !== 0 ) { - $executions[] = $execution = Process::fromShellCommandline($command); + $executions[] = $execution = new Process([ + PHP_BINARY, + $input->getOption('script') ?? $this->script, + $input->getOption('command') ?? $this->command, + ]); // @codeCoverageIgnoreStart if (Process::isTtySupported()) {