8000 [Console] Add signal event by marie · Pull Request #33729 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Add signal event #33729

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
Jul 31, 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
use Symfony\Component\Config\Resource\SelfCheckingResourceChecker;
use Symfony\Component\Config\ResourceCheckerConfigCacheFactory;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleSignalEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\DependencyInjection\Config\ContainerParametersResourceChecker;
use Symfony\Component\DependencyInjection\EnvVarProcessor;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleSignalEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Exception\ExceptionInterface;
Expand All @@ -39,6 +40,7 @@
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SignalRegistry\SignalRegistry;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\ErrorHandler\ErrorHandler;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -76,6 +78,7 @@ class Application implements ResetInterface
private $defaultCommand;
private $singleCommand = false;
private $initialized;
private $signalRegistry;

public function __construct(string $name = 'UNKNOWN', string $version = 'UNKNOWN')
{
Expand All @@ -98,6 +101,11 @@ public function setCommandLoader(CommandLoaderInterface $commandLoader)
$this->commandLoader = $commandLoader;
}

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

/**
* Runs the current application.
*
Expand Down Expand Up @@ -260,6 +268,17 @@ 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) {
$this->dispatcher->dispatch($event, ConsoleEvents::SIGNAL);
};

$this->signalRegistry->register($handlingSignal, $onSignalHandler);
}
}

$this->runningCommand = $command;
$exitCode = $this->doRunCommand($command, $input, $output);
$this->runningCommand = null;
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Console/ConsoleEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleSignalEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;

/**
Expand All @@ -31,6 +32,14 @@ final class ConsoleEvents
*/
const COMMAND = 'console.command';

/**
* The SIGNAL event allows you to perform some actions
* after the command execution was interrupted.
*
* @Event("Symfony\Component\Console\Event\ConsoleSignalEvent")
*/
const SIGNAL = 'console.signal';

/**
* The TERMINATE event allows you to attach listeners after a command is
* executed by the console.
Expand All @@ -57,6 +66,7 @@ final class ConsoleEvents
const ALIASES = [
ConsoleCommandEvent::class => self::COMMAND,
ConsoleErrorEvent::class => self::ERROR,
ConsoleSignalEvent::class => 'console.signal',
ConsoleTerminateEvent::class => self::TERMINATE,
];
}
35 changes: 35 additions & 0 deletions src/Symfony/Component/Console/Event/ConsoleSignalEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\Event;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @author marie <marie@users.noreply.github.com>
*/
final class ConsoleSignalEvent extends ConsoleEvent
{
private $handlingSignal;

public function __construct(Command $command, InputInterface $input, OutputInterface $output, int $handlingSignal)
{
parent::__construct($command, $input, $output);
$this->handlingSignal = $handlingSignal;
}

public function getHandlingSignal(): int
{
return $this->handlingSignal;
}
}
60 changes: 60 additions & 0 deletions src/Symfony/Component/Console/SignalRegistry/SignalRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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\SignalRegistry;

final class SignalRegistry
{
private $registeredSignals = [];

private $handlingSignals = [];

public function __construct()
{
pcntl_async_signals(true);
}

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

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

$this->registeredSignals[$signal][] = $signalHandler;
pcntl_signal($signal, [$this, 'handle']);
}

/**
* @internal
*/
public function handle(int $signal): void
{
foreach ($this->registeredSignals[$signal] as $signalHandler) {
$signalHandler($signal);
}
}

public function addHandlingSignals(int ...$signals): void
{
foreach ($signals as $signal) {
$this->handlingSignals[$signal] = true;
}
}

public function getHandlingSignals(): array
{
return array_keys($this->handlingSignals);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?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\Tests\SignalRegistry;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\SignalRegistry\SignalRegistry;

/**
* @requires extension pcntl
*/
class SignalRegistryTest extends TestCase
{
public function tearDown(): void
{
pcntl_async_signals(false);
pcntl_signal(SIGUSR1, SIG_DFL);
pcntl_signal(SIGUSR2, SIG_DFL);
}

public function testOneCallbackForASignal_signalIsHandled()
{
$signalRegistry = new SignalRegistry();

$isHandled = false;
$signalRegistry->register(SIGUSR1, function () use (&$isHandled) {
$isHandled = true;
});

posix_kill(posix_getpid(), SIGUSR1);

$this->assertTrue($isHandled);
}

public function testTwoCallbacksForASignal_bothCallbacksAreCalled()
{
$signalRegistry = new SignalRegistry();

$isHandled1 = false;
$signalRegistry->register(SIGUSR1, function () use (&$isHandled1) {
$isHandled1 = true;
});

$isHandled2 = false;
$signalRegistry->register(SIGUSR1, function () use (&$isHandled2) {
$isHandled2 = true;
});

posix_kill(posix_getpid(), SIGUSR1);

$this->assertTrue($isHandled1);
$this->assertTrue($isHandled2);
}

public function testTwoSignals_signalsAreHandled()
{
$signalRegistry = new SignalRegistry();

$isHandled1 = false;
$isHandled2 = false;

$signalRegistry->register(SIGUSR1, function () use (&$isHandled1) {
$isHandled1 = true;
});

posix_kill(posix_getpid(), SIGUSR1);

$this->assertTrue($isHandled1);
$this->assertFalse($isHandled2);

$signalRegistry->register(SIGUSR2, function () use (&$isHandled2) {
$isHandled2 = true;
});

posix_kill(posix_getpid(), SIGUSR2);

$this->assertTrue($isHandled2);
}

public function testTwoCallbacksForASignal_previousAndRegisteredCallbacksWereCalled()
{
$signalRegistry = new SignalRegistry();

$isHandled1 = false;
pcntl_signal(SIGUSR1, function () use (&$isHandled1) {
$isHandled1 = true;
});

$isHandled2 = false;
$signalRegistry->register(SIGUSR1, function () use (&$isHandled2) {
$isHandled2 = true;
});

posix_kill(posix_getpid(), SIGUSR1);

$this->assertTrue($isHandled1);
$this->assertTrue($isHandled2);
}

public function testTwoCallbacksForASignal_previousCallbackFromAnotherRegistry()
{
$signalRegistry1 = new SignalRegistry();

$isHandled1 = false;
$signalRegistry1->register(SIGUSR1, function () use (&$isHandled1) {
$isHandled1 = true;
});

$signalRegistry2 = new SignalRegistry();

$isHandled2 = false;
$signalRegistry2->register(SIGUSR1, function () use (&$isHandled2) {
$isHandled2 = true;
});

posix_kill(posix_getpid(), SIGUSR1);

$this->assertTrue($isHandled1);
$this->assertTrue($isHandled2);
}
}
0