-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[9.x] Introducing Signal Traps 🚦 #43933
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
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
7c68c8f
Adds signal traps
nunomaduro 6c8e8d7
Apply fixes from StyleCI
StyleCIBot 4b61c28
Improves code logic
nunomaduro 9066928
Improves return type
nunomaduro ad3f304
Don't run traps outside of console or in unit tests
nunomaduro 9080c41
Updates description
nunomaduro c6b356b
Adds tests
nunomaduro 0b4f53c
Removes non needed annotation
nunomaduro dc2ea4d
Improves test
nunomaduro 91e641d
Avoids missing constant on Windows
nunomaduro e735acd
Improves exit "as default" behaviour
nunomaduro 5e1b7e8
Adjusts docs
nunomaduro 53cf0ff
Only use signals if `posix` and `pcntl` are available
nunomaduro 49d1ab7
Fixes missing parentheses
nunomaduro c46b3e5
Apply fixes from StyleCI
StyleCIBot bc99750
No longers kill the process
nunomaduro ecfa022
Unsets null values
nunomaduro 0c49e64
Adjusts PHP annotations
nunomaduro 61326d9
move to service provider
taylorotwell e1a6eea
move method
taylorotwell e88a73b
Apply fixes from StyleCI
StyleCIBot 79ef572
Update InteractsWithSignals.php
taylorotwell f0bed13
Update Signals.php
taylorotwell 69ea261
Update Signals.php
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
$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); | ||
} | ||
}, ]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.