Skip to content
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

[9.x] Introducing Signal Traps 🚦 #43933

Merged
merged 24 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 8 additions & 3 deletions src/Illuminate/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Command extends SymfonyCommand
use Concerns\CallsCommands,
Concerns\HasParameters,
Concerns\InteractsWithIO,
Concerns\InteractsWithSignals,
Macroable;

/**
Expand Down Expand Up @@ -120,9 +121,13 @@ public function run(InputInterface $input, OutputInterface $output): int

$this->components = $this->laravel->make(Factory::class, ['output' => $this->output]);

return parent::run(
$this->input = $input, $this->output
);
try {
return parent::run(
$this->input = $input, $this->output
);
} finally {
$this->untrap();
}
}

/**
Expand Down
49 changes: 49 additions & 0 deletions src/Illuminate/Console/Concerns/InteractsWithSignals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Illuminate\Console\Concerns;

use Illuminate\Console\Signals;
use Illuminate\Support\Arr;

trait InteractsWithSignals
{
/**
* The signals instance, if any.
*
* @var \Illuminate\Console\Signals|null
*/
protected $signals;

/**
* Sets a trap to be run when the given signal(s) occurs.
*
* @param iterable<array-key, int>|int $signals
* @param callable $callback
* @return void
*/
public function trap($signals, $callback)
{
Signals::whenAvailable(function () use ($signals, $callback) {
$this->signals ??= new Signals(
$this->getApplication()->getSignalRegistry(),
);

collect(Arr::wrap($signals))
->each(fn ($signal) => $this->signals->register($signal, $callback));
});
}

/**
* Untraps traps set by the command class.
*
* @return void
*/
public function untrap()
{
if (! is_null($this->signals)) {
$this->signals->unregister();

$this->signals = null;
}
}
}
132 changes: 132 additions & 0 deletions src/Illuminate/Console/Signals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace Illuminate\Console;

use Symfony\Component\Console\SignalRegistry\SignalRegistry;

class Signals
{
/**
* The signal registry instance.
*
* @var \Symfony\Component\Console\SignalRegistry\SignalRegistry
*/
protected $registry;

/**
* The signal registry's previous list of handlers.
*
* @param array<int, array<int, callable>>|null
*/
protected $previousHandlers;

/**
* Create a new Signals instance.
*
* @param \Symfony\Component\Console\SignalRegistry\SignalRegistry $registry
* @return void
*/
public function __construct($registry)
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved
{
$this->registry = $registry;

$this->previousHandlers = $this->getHandlers();
}

/**
* Registers a new signal handler.
*
* @param int $signal
* @param callable $callback
* @return void
*/
public function register($signal, $callback)
{
$this->previousHandlers[$signal] ??= $this->initializeSignal($signal);

with($this->getHandlers(), function ($handlers) use ($signal) {
$handlers[$signal] ??= $this->initializeSignal($signal);

$this->setHandlers($handlers);
});

$this->registry->register($signal, $callback);

with($this->getHandlers(), function ($handlers) use ($signal) {
$lastHandlerInserted = array_pop($handlers[$signal]);
array_unshift($handlers[$signal], $lastHandlerInserted);

$this->setHandlers($handlers);
});
}

/**
* Unregister the current signals instance, and reverts
* the signal's registry handlers state.
*
* @return array<int, array<int, callable>>
*/
public function unregister()
{
$this->setHandlers($this->previousHandlers);
}

/**
* Executes the given callback if "signals" are supported.
*
* @param callable $callback
* @return void
*/
public static function whenAvailable($callback)
{
if (
app()->runningInConsole()
&& ! app()->runningUnitTests()
&& extension_loaded('pcntl')
&& defined('SIGINT')
&& SignalRegistry::isSupported()) {
$callback();
}
}

/**
* Set the registry's handlers.
*
* @param array<int, array<int, callable>> $handlers
* @return void
*/
protected function setHandlers($handlers)
{
(fn () => $this->signalHandlers = $handlers)
->call($this->registry);
}

/**
* Get the registry's handlers.
*
* @return array<int, array<int, callable>>
*/
protected function getHandlers()
{
return (fn () => $this->signalHandlers)
->call($this->registry);
}

/**
* Gets the signal's default callback on the array format.
*
* @return [callable]
*/
protected function initializeSignal($signal)
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved
{
$existingHandler = pcntl_signal_get_handler($signal);

return [is_callable($existingHandler)
? $existingHandler
: function ($signal) {
if (! in_array($signal, [SIGUSR1, SIGUSR2])) {
exit(0);
}
}, ];
}
}