class Controller
Coordinates process-wide signal handlers for multiple consumers.
Nested
Definitions
def initialize
Initialize the controller.
Implementation
def initialize
@mutex = ::Thread::Mutex.new
@states = {}
@dispatch = {}.freeze
end
def install(handlers)
Install signal handlers.
Signature
-
parameter
handlersHandlers The handlers to install.
-
yields
{|handlers| ...} The block to run while the handlers are installed.
-
returns
Registration The active registration.
Implementation
def install(handlers)
installed_handlers = handlers.to_h.freeze
registration = Registration.new(self, installed_handlers)
@mutex.synchronize do
installed_handlers.each do |signal, handler|
add(signal, registration, handler)
end
update_dispatch
end
if block_given?
begin
return yield handlers
ensure
registration.close
end
else
return registration
end
end
def dispatch(signal)
Dispatch a signal to all currently active handlers.
Signature
-
parameter
signalString The signal name to dispatch.
Implementation
def dispatch(signal)
number = ::Signal.list.fetch(signal)
@dispatch[signal]&.each do |handler, context|
handler.call(number, context)
end
end
def remove(registration, handlers)
Remove a set of installed handlers.
Signature
-
parameter
registrationRegistration The registration that owns the handlers.
-
parameter
handlersHash(String, Proc | Nil) The handlers to remove.
Implementation
def remove(registration, handlers)
@mutex.synchronize do
handlers.each_key do |signal|
remove_signal(signal, registration)
end
update_dispatch
end
end
def reset!
Reset all installed signal handlers to their previous signal traps.
Implementation
def reset!
@mutex.synchronize do
@states.each do |signal, state|
::Signal.trap(signal, state.previous)
end
@states.clear
update_dispatch
end
end