Lines 100.00% 9 / 9
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
WorkerRestartCommand 100.00% 9 / 9 100.00% 3 / 3 4 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 configure 100.00% 2 / 2 100.00% 1 / 1 1
 execute 100.00% 6 / 6 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Console;
6
7use Amp\ByteStream\StreamException;
8use Amp\CancelledException;
9use Stewart\Contracts\Exception\StewartException;
10use Stewart\Runtime\Config\ConfigLoader;
11use Stewart\Runtime\Control\Client\ControlClient;
12use Stewart\Runtime\Control\Client\ControlTargetResolver;
13use Symfony\Component\Console\Attribute\AsCommand;
14use Symfony\Component\Console\Command\Command;
15use Symfony\Component\Console\Input\InputArgument;
16use Symfony\Component\Console\Input\InputInterface;
17use Symfony\Component\Console\Output\OutputInterface;
18
19#[AsCommand(name: self::NAME, description: 'Restart one worker with a fresh crash budget, also after it was quarantined')]
20final class WorkerRestartCommand extends ControlCommand
21{
22    public const string NAME = 'worker:restart';
23
24    public function __construct(
25        ConfigLoader $config,
26        ControlTargetResolver $targets,
27        private readonly ControlClient $client,
28    ) {
29        parent::__construct($config, $targets);
30    }
31
32    protected function configure(): void
33    {
34        parent::configure();
35        $this->addArgument('worker', InputArgument::REQUIRED, 'The worker number, as shown by stewart status');
36    }
37
38    protected function execute(InputInterface $input, OutputInterface $output): int
39    {
40        try {
41            $result = $this->client->restartWorker($this->resolveControlTarget($input), $this->readStringArgument($input, 'worker'), $this->parseTimeoutOption($input));
42        } catch (StewartException|StreamException|CancelledException $e) {
43            $output->writeln('<error>' . $e->getMessage() . '</error>');
44
45            return Command::FAILURE;
46        }
47
48        $this->writeCommandResult($output, $result);
49
50        return Command::SUCCESS;
51    }
52}