-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.rb
42 lines (36 loc) · 1.03 KB
/
signal.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
# 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