@@ -249,6 +249,72 @@ handle all signals e.g. to do some tasks before terminating the command.
249249 :method: `Symfony\\ Component\\ Console\\ SignalRegistry\\ SignalMap::getSignalName `
250250 method.
251251
252+ The ``ConsoleEvents::ALARM `` Event
253+ ----------------------------------
254+
255+ **Typical Purposes **: To perform periodic tasks during long-running commands
256+ (e.g. database connection keepalive, extending lock TTLs, heartbeat checks).
257+
258+ When a command calls
259+ :method: `Symfony\\ Component\\ Console\\ Application::setAlarmInterval `, the
260+ application schedules a recurring ``SIGALRM `` signal at the given interval (in
261+ seconds). Each time the signal fires, a ``ConsoleAlarmEvent `` is dispatched,
262+ allowing listeners to run periodic logic without blocking the command::
263+
264+ // src/Command/LongRunningCommand.php
265+ namespace App\Command;
266+
267+ use Symfony\Component\Console\Attribute\AsCommand;
268+ use Symfony\Component\Console\Command\Command;
269+ use Symfony\Component\Console\Input\InputInterface;
270+ use Symfony\Component\Console\Output\OutputInterface;
271+
272+ #[AsCommand(name: 'app:long-running')]
273+ class LongRunningCommand extends Command
274+ {
275+ protected function initialize(InputInterface $input, OutputInterface $output): void
276+ {
277+ // trigger an alarm every 10 seconds
278+ $this->getApplication()->setAlarmInterval(10);
279+ }
280+
281+ protected function execute(InputInterface $input, OutputInterface $output): int
282+ {
283+ // long-running processing...
284+
285+ return Command::SUCCESS;
286+ }
287+ }
288+
289+ You can then listen to the
290+ :class: `Symfony\\ Component\\ Console\\ Event\\ ConsoleAlarmEvent ` to perform
291+ actions on each alarm::
292+
293+ // src/EventListener/ConsoleAlarmListener.php
294+ namespace App\EventListener;
295+
296+ use Symfony\Component\Console\Event\ConsoleAlarmEvent;
297+ use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
298+
299+ #[AsEventListener]
300+ class ConsoleAlarmListener
301+ {
302+ public function __invoke(ConsoleAlarmEvent $event): void
303+ {
304+ // e.g. ping the database to keep the connection alive
305+ }
306+ }
307+
308+ .. note ::
309+
310+ The alarm feature requires the ``pcntl `` PHP extension and is not available
311+ on Windows.
312+
313+ .. versionadded :: 7.2
314+
315+ The ``ConsoleAlarmEvent `` and ``setAlarmInterval() `` were introduced in
316+ Symfony 7.2.
317+
252318.. _`reserved exit codes` : https://www.tldp.org/LDP/abs/html/exitcodes.html
253319.. _`Signals` : https://en.wikipedia.org/wiki/Signal_(IPC)
254320.. _`constants of the PCNTL PHP extension` : https://www.php.net/manual/en/pcntl.constants.php
0 commit comments