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
Non-overriding signal handlers #2767
Closed
alexeevit
wants to merge
14
commits into
puma:master
from
alexeevit:feature/nonReplacingSignalHandlers
Closed
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
66ba480
implement safe signal trap && use it
alexeevit 73d8e32
do not log on rescue
alexeevit f4fda9f
hotfix
alexeevit 9d157fb
hotfix
alexeevit 911cb10
new Puma::Signal solution
alexeevit 06b0d5b
clean
alexeevit 2f3562e
linter
alexeevit cb0c016
Merge branch 'master' into feature/nonReplacingSignalHandlers
nateberkopec 937d46d
merge master
alexeevit 7b45e05
add behavior for text signal handlers
alexeevit 2e61866
hotfix
alexeevit 5383fba
Merge branch 'master' into feature/nonReplacingSignalHandlers
alexeevit 233ac42
Merge branch 'master' into feature/nonReplacingSignalHandlers
alexeevit 4e65f2e
Merge branch 'master' into feature/nonReplacingSignalHandlers
alexeevit File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module Puma | ||
module Signal | ||
module_function | ||
|
||
def prepend_handler(sig, &handler) | ||
name = signame(sig) | ||
custom_signal_handlers[signame] ||= [] | ||
custom_signal_handlers[signame].prepend(handler) | ||
end | ||
nateberkopec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Signal.trap that does not replace | ||
# the existing puma handlers | ||
def trap(sig, &handler) | ||
name = signame(sig) | ||
::Signal.trap(name) do | ||
invoke_custom_signal_handlers(name) | ||
yield handler | ||
end | ||
end | ||
|
||
private | ||
|
||
def signame(sig) | ||
name = sig.is_a?(Integer) ? ::Signal.signame(sig) : String(sig).sub('SIG', '') | ||
raise ArgumentError, "unsupported signal SIG#{name}" unless ::Signal.list[name] | ||
name | ||
end | ||
module_function :signame | ||
|
||
def invoke_custom_signal_handlers(signame) | ||
Array(custom_signal_handlers[signame]).each do |handler| | ||
yield handler | ||
end | ||
end | ||
module_function :invoke_custom_signal_handlers | ||
|
||
def custom_signal_handlers | ||
@custom_signal_handlers ||= {} | ||
end | ||
module_function :custom_signal_handlers | ||
end | ||
end |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This exception handling could also be moved to Puma::Signal module
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, I'll consider it if the solution makes sense at all 😄