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