Skip to content

[Console] Add signal event #15741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions components/console/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,25 @@ Listeners receive a
The exit code received in this case is the exception code.

.. _`reserved exit codes`: https://www.tldp.org/LDP/abs/html/exitcodes.html

The ``ConsoleEvents::SIGNAL`` Event
-----------------------------------

.. versionadded:: 5.2

The ``ConsoleSignalEvent`` class was introduced in Symfony 5.2.

**Typical Purposes**: To handle some signals for all application commands while being executed.

Listeners receive a
:class:`Symfony\\Component\\Console\\Event\\ConsoleSignalEvent` event::

use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleSignalEvent;

$dispatcher->addListener(ConsoleEvents::SIGNAL, function (ConsoleSignalEvent $event) {
// https://www.php.net/manual/en/pcntl.constants.php
$signal = $event->getHandlingSignal();

// ...
});
42 changes: 42 additions & 0 deletions console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,48 @@ call ``setAutoExit(false)`` on it to get the command result in ``CommandTester``
:class:`Symfony\\Component\\Console\\Application <Symfony\\Component\\Console\\Application>`
and extend the normal ``\PHPUnit\Framework\TestCase``.

Command Reacting to Signals
---------------------------

.. versionadded:: 5.2

The :class:`Symfony\\Component\\Console\\Command\\SignalableCommandInterface` interface
for reacting to signals was introduced in Symfony 5.2.

You can make your commands react to signals by implementing the ``SignalableCommandInterface`` interface::

// ...
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\SignalableCommandInterface;

class CreateUserCommand extends Command implements SignalableCommandInterface
{
// ...

public function getSubscribedSignals(): array
{
// constants defined by PCNTL extension
// https://www.php.net/manual/en/pcntl.constants.php
return [SIGINT, SIGTERM];
}

public function handleSignal(int $signal): void
{
if (SIGINT === $signal) {
// handle SIGINT signal
}

// handle SIGTERM signal
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
// ...

return Command::SUCCESS;
}
}

Logging Command Errors
----------------------

Expand Down