[9.x] Introducing Signal Traps 馃殾 - #43933
Merged
Merged
Conversation
driesvints
reviewed
Aug 31, 2022
Wirone
reviewed
Aug 31, 2022
nunomaduro
commented
Aug 31, 2022
nunomaduro
marked this pull request as ready for review
September 1, 2022 10:51
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
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 + Con MacOS, aSIGINTwill 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
trapmethod, on the baseCommandclass of Laravel.Now, because is very common wanting to subscribe to multiple signals at the same time, you may pass an
arrayof signals in those cases:In addition,
trapsgetuntraponce 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:
trapmethod, you intentionally override PHP's native behavior for that signal. For example, if you define atrapforSIGINT, PHP won't end the process as it would do natively, therefore is up to the user to end the process gracefully or not.pcntlextension - in fact, trying to useSIGINTon 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: