-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Conversation
This file contains 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
driesvints
reviewed
Aug 31, 2022
nunomaduro
changed the title
[9.x] Adds Signal Traps to Artisan
[9.x] Introducing Signal Traps
Aug 31, 2022
Wirone
reviewed
Aug 31, 2022
nunomaduro
commented
Aug 31, 2022
nunomaduro
changed the title
[9.x] Introducing Signal Traps
[9.x] Introducing Signal Traps 🚦
Sep 1, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Documentation: laravel/docs#8178.
Many operating systems allow signals to be sent to running processes. As an example, when typing
Control + C
on MacOS, aSIGINT
will initiate an Artisan command shutdown.Artisan commands shutdowns may lead to unexpected state, as the user may have defined an Artisan command that takes more than expected to run, and ending the command without any sort of clean up may leave temporary files, temporary resources, and more.
To mitigate this issue, this pull request introduces Signal Traps on Laravel, a new concept that allows you to catch process signals and execute code when they occur:
With this technique, Laravel developers can perform any clean-up tasks / or potentially revert operations when an Artisan command is ended unexpectedly. The given API, sits on top of a new
trap
method, on the baseCommand
class of Laravel.Now, because is very common wanting to subscribe to multiple signals at the same time, you may pass an
array
of signals in those cases:In addition,
traps
getuntrap
once the command is "done". This is useful, and intentionally behavior, as commands may get called within commands, or within "queue:jobs":Finally, two important things that not be trivial at first glance:
trap
method, you intentionally override PHP's native behavior for that signal. For example, if you define atrap
forSIGINT
, PHP won't end the process as it would do natively, therefore is up to the user to end the process gracefully or not.pcntl
extension - in fact, trying to useSIGINT
on an Windows environment, results on the following error:Package developers should use
extension_loaded('pcntl')
before even calling traps, as their users may use Windows.For future development considerations: