Skip to content

[Console] Rework the signal integration #37827

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

Merged
merged 1 commit into from
Aug 13, 2020
Merged
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
39 changes: 31 additions & 8 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\Command\ListCommand;
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
Expand Down Expand Up @@ -79,13 +80,18 @@ class Application implements ResetInterface
private $singleCommand = false;
private $initialized;
private $signalRegistry;
private $signalsToDispatchEvent = [];

public function __construct(string $name = 'UNKNOWN', string $version = 'UNKNOWN')
{
$this->name = $name;
$this->version = $version;
$this->terminal = new Terminal();
$this->defaultCommand = 'list';
$this->signalRegistry = new SignalRegistry();
if (\defined('SIGINT')) {
$this->signalsToDispatchEvent = [SIGINT, SIGTERM, SIGUSR1, SIGUSR2];
}
}

/**
Expand All @@ -101,9 +107,14 @@ public function setCommandLoader(CommandLoaderInterface $commandLoader)
$this->commandLoader = $commandLoader;
}

public function setSignalRegistry(SignalRegistry $signalRegistry)
public function getSignalRegistry(): SignalRegistry
{
$this->signalRegistry = $signalRegistry;
return $this->signalRegistry;
}

public function setSignalsToDispatchEvent(int ...$signalsToDispatchEvent)
{
$this->signalsToDispatchEvent = $signalsToDispatchEvent;
}

/**
Expand Down Expand Up @@ -268,14 +279,20 @@ public function doRun(InputInterface $input, OutputInterface $output)
$command = $this->find($alternative);
}

if ($this->signalRegistry) {
foreach ($this->signalRegistry->getHandlingSignals() as $handlingSignal) {
$event = new ConsoleSignalEvent($command, $input, $output, $handlingSignal);
$onSignalHandler = function () use ($event) {
if ($this->dispatcher) {
foreach ($this->signalsToDispatchEvent as $signal) {
$event = new ConsoleSignalEvent($command, $input, $output, $signal);

$this->signalRegistry->register($signal, function ($signal, $hasNext) use ($event) {
$this->dispatcher->dispatch($event, ConsoleEvents::SIGNAL);
};

$this->signalRegistry->register($handlingSignal, $onSignalHandler);
// No more handlers, we try to simulate PHP default behavior
if (!$hasNext) {
if (!\in_array($signal, [SIGUSR1, SIGUSR2], true)) {
exit(0);
}
}
});
}
}

Expand Down Expand Up @@ -926,6 +943,12 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
}
}

if ($command instanceof SignalableCommandInterface) {
foreach ($command->getSubscribedSignals() as $signal) {
$this->signalRegistry->register($signal, [$command, 'handleSignal']);
}
}

if (null === $this->dispatcher) {
return $command->run($input, $output);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ CHANGELOG
* Added `SingleCommandApplication::setAutoExit()` to allow testing via `CommandTester`
* added support for multiline responses to questions through `Question::setMultiline()`
and `Question::isMultiline()`
* Added `SignalRegistry` class to stack signals handlers
* Added support for signals:
* Added `Application::getSignalRegistry()` and `Application::setSignalsToDispatchEvent()` methods
* Added `SignalableCommandInterface` interface

5.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Command;

/**
* Interface for command reacting to signal.
*
* @author Grégoire Pineau <lyrixx@lyrix.info>
*/
interface SignalableCommandInterface
{
/**
* Returns the list of signals to subscribe.
*/
public function getSubscribedSignals(): array;

/**
* The method will be called when the application is signaled.
*/
public function handleSignal(int $signal): void;
}
32 changes: 12 additions & 20 deletions src/Symfony/Component/Console/SignalRegistry/SignalRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@

final class SignalRegistry
{
private $registeredSignals = [];

private $handlingSignals = [];
private $signalHandlers = [];

public function __construct()
{
pcntl_async_signals(true);
if (\function_exists('pcntl_async_signals')) {
pcntl_async_signals(true);
}
}

public function register(int $signal, callable $signalHandler): void
{
if (!isset($this->registeredSignals[$signal])) {
if (!isset($this->signalHandlers[$signal])) {
$previousCallback = pcntl_signal_get_handler($signal);

if (\is_callable($previousCallback)) {
$this->registeredSignals[$signal][] = $previousCallback;
$this->signalHandlers[$signal][] = $previousCallback;
}
}

$this->registeredSignals[$signal][] = $signalHandler;
$this->signalHandlers[$signal][] = $signalHandler;

pcntl_signal($signal, [$this, 'handle']);
}

Expand All @@ -41,20 +42,11 @@ public function register(int $signal, callable $signalHandler): void
*/
public function handle(int $signal): void
{
foreach ($this->registeredSignals[$signal] as $signalHandler) {
$signalHandler($signal);
}
}
$count = \count($this->signalHandlers[$signal]);

public function addHandlingSignals(int ...$signals): void
{
foreach ($signals as $signal) {
$this->handlingSignals[$signal] = true;
foreach ($this->signalHandlers[$signal] as $i => $signalHandler) {
$hasNext = $i !== $count - 1;
$signalHandler($signal, $hasNext);
}
}

public function getHandlingSignals(): array
{
return array_keys($this->handlingSignals);
}
}