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