|
9 | 9 | use Symfony\Component\Console\Input\InputInterface; |
10 | 10 | use Symfony\Component\Console\Output\OutputInterface; |
11 | 11 |
|
12 | | -abstract class AbstractTerminableCommand extends Command implements SignalableCommandInterface |
13 | | -{ |
14 | | - private const REQUEST_TO_TERMINATE = 143; |
| 12 | +if (! class_exists(AbstractTerminableCommand::class)) { |
| 13 | + abstract class AbstractTerminableCommand extends Command implements SignalableCommandInterface |
| 14 | + { |
| 15 | + private const REQUEST_TO_TERMINATE = 143; |
15 | 16 |
|
16 | | - /** @var int */ |
17 | | - private $sleepDuration; |
| 17 | + /** @var int */ |
| 18 | + private $sleepDuration; |
18 | 19 |
|
19 | | - /** @var bool */ |
20 | | - private $signalShutdownRequested; |
| 20 | + /** @var bool */ |
| 21 | + private $signalShutdownRequested; |
21 | 22 |
|
22 | | - public function __construct(?string $name = null) |
23 | | - { |
24 | | - $this->sleepDuration = 0; |
25 | | - $this->signalShutdownRequested = false; |
| 23 | + public function __construct(?string $name = null) |
| 24 | + { |
| 25 | + $this->sleepDuration = 0; |
| 26 | + $this->signalShutdownRequested = false; |
26 | 27 |
|
27 | | - parent::__construct($name); |
28 | | - } |
| 28 | + parent::__construct($name); |
| 29 | + } |
29 | 30 |
|
30 | | - final protected function execute(InputInterface $input, OutputInterface $output): int |
31 | | - { |
32 | | - $output->writeln('Starting ' . ($this->getName() ?? static::class), OutputInterface::VERBOSITY_VERBOSE); |
| 31 | + final protected function execute(InputInterface $input, OutputInterface $output): int |
| 32 | + { |
| 33 | + $output->writeln('Starting ' . ($this->getName() ?? static::class), OutputInterface::VERBOSITY_VERBOSE); |
33 | 34 |
|
34 | | - if ($this->signalShutdownRequested) { |
35 | | - $output->writeln('Signal received, skipping execution', OutputInterface::VERBOSITY_NORMAL); |
| 35 | + if ($this->signalShutdownRequested) { |
| 36 | + $output->writeln('Signal received, skipping execution', OutputInterface::VERBOSITY_NORMAL); |
36 | 37 |
|
37 | | - return self::REQUEST_TO_TERMINATE; |
38 | | - } |
| 38 | + return self::REQUEST_TO_TERMINATE; |
| 39 | + } |
39 | 40 |
|
40 | | - $exitCode = $this->commandBody($input, $output); |
| 41 | + $exitCode = $this->commandBody($input, $output); |
41 | 42 |
|
42 | | - $this->sleep($output); |
| 43 | + $this->sleep($output); |
43 | 44 |
|
44 | | - /** @psalm-suppress DocblockTypeContradiction */ |
45 | | - if ($this->signalShutdownRequested) { |
46 | | - $output->writeln('Signal received, terminating with exit code ' . self::REQUEST_TO_TERMINATE, OutputInterface::VERBOSITY_NORMAL); |
| 45 | + /** @psalm-suppress DocblockTypeContradiction */ |
| 46 | + if ($this->signalShutdownRequested) { |
| 47 | + $output->writeln('Signal received, terminating with exit code ' . self::REQUEST_TO_TERMINATE, OutputInterface::VERBOSITY_NORMAL); |
47 | 48 |
|
48 | | - return self::REQUEST_TO_TERMINATE; |
| 49 | + return self::REQUEST_TO_TERMINATE; |
| 50 | + } |
| 51 | + |
| 52 | + return $exitCode; |
49 | 53 | } |
50 | 54 |
|
51 | | - return $exitCode; |
52 | | - } |
| 55 | + abstract protected function commandBody(InputInterface $input, OutputInterface $output): int; |
53 | 56 |
|
54 | | - abstract protected function commandBody(InputInterface $input, OutputInterface $output): int; |
| 57 | + public function handleSignal(int $signal, $previousExitCode = 0): false |
| 58 | + { |
| 59 | + switch ($signal) { |
| 60 | + // Shutdown signals |
| 61 | + case SIGTERM: |
| 62 | + case SIGINT: |
| 63 | + $this->signalShutdownRequested = true; |
| 64 | + } |
55 | 65 |
|
56 | | - public function handleSignal(int $signal, $previousExitCode = 0): false |
57 | | - { |
58 | | - switch ($signal) { |
59 | | - // Shutdown signals |
60 | | - case SIGTERM: |
61 | | - case SIGINT: |
62 | | - $this->signalShutdownRequested = true; |
| 66 | + return false; |
63 | 67 | } |
64 | 68 |
|
65 | | - return false; |
66 | | - } |
| 69 | + /** |
| 70 | + * @return list<int> |
| 71 | + */ |
| 72 | + public function getSubscribedSignals(): array |
| 73 | + { |
| 74 | + return [ |
| 75 | + SIGTERM, |
| 76 | + SIGINT, |
| 77 | + ]; |
| 78 | + } |
67 | 79 |
|
68 | | - /** |
69 | | - * @return list<int> |
70 | | - */ |
71 | | - public function getSubscribedSignals(): array |
72 | | - { |
73 | | - return [ |
74 | | - SIGTERM, |
75 | | - SIGINT, |
76 | | - ]; |
77 | | - } |
| 80 | + protected function getSleepDuration(): int |
| 81 | + { |
| 82 | + return $this->sleepDuration; |
| 83 | + } |
78 | 84 |
|
79 | | - protected function getSleepDuration(): int |
80 | | - { |
81 | | - return $this->sleepDuration; |
82 | | - } |
| 85 | + protected function setSleepDuration(int $sleepDuration): void |
| 86 | + { |
| 87 | + if ($sleepDuration < 0) { |
| 88 | + throw new \InvalidArgumentException('Invalid timeout provided to ' . __METHOD__); |
| 89 | + } |
83 | 90 |
|
84 | | - protected function setSleepDuration(int $sleepDuration): void |
85 | | - { |
86 | | - if ($sleepDuration < 0) { |
87 | | - throw new \InvalidArgumentException('Invalid timeout provided to ' . __METHOD__); |
| 91 | + $this->sleepDuration = $sleepDuration; |
88 | 92 | } |
89 | 93 |
|
90 | | - $this->sleepDuration = $sleepDuration; |
91 | | - } |
| 94 | + private function sleep(OutputInterface $output): void |
| 95 | + { |
| 96 | + if (0 === $this->sleepDuration) { |
| 97 | + return; |
| 98 | + } |
92 | 99 |
|
93 | | - private function sleep(OutputInterface $output): void |
94 | | - { |
95 | | - if (0 === $this->sleepDuration) { |
96 | | - return; |
97 | | - } |
| 100 | + $sleepCountDown = $this->sleepDuration; |
98 | 101 |
|
99 | | - $sleepCountDown = $this->sleepDuration; |
| 102 | + while (! $this->signalShutdownRequested && --$sleepCountDown) { |
| 103 | + sleep(1); |
| 104 | + } |
100 | 105 |
|
101 | | - while (! $this->signalShutdownRequested && --$sleepCountDown) { |
102 | | - sleep(1); |
| 106 | + $output->writeln( |
| 107 | + sprintf('Slept %d second(s)', $this->sleepDuration - $sleepCountDown), |
| 108 | + OutputInterface::VERBOSITY_DEBUG |
| 109 | + ); |
103 | 110 | } |
104 | | - |
105 | | - $output->writeln( |
106 | | - sprintf('Slept %d second(s)', $this->sleepDuration - $sleepCountDown), |
107 | | - OutputInterface::VERBOSITY_DEBUG |
108 | | - ); |
109 | 111 | } |
110 | 112 | } |
0 commit comments