Skip to content

Commit

Permalink
WorkerCommand: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Jan 13, 2024
1 parent a67ba35 commit dc3197d
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

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;
use Symfony\Component\Console\Input\InputInterface;
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;

/**
Expand All @@ -25,7 +23,7 @@
final class WorkerCommand extends Command
{

private ClockInterface $clock;
private Clock $clock;

private ?int $testRuns = null;

Expand All @@ -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
Expand Down Expand Up @@ -79,16 +77,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('<info>Running scheduled tasks every minute.</info>');

$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();

Expand All @@ -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()) {
Expand Down

0 comments on commit dc3197d

Please sign in to comment.