Module: Pgbus::Process::SignalHandler
- Included in:
- Outbox::Poller, Consumer, Dispatcher, Supervisor, Worker, Recurring::Scheduler
- Defined in:
- lib/pgbus/process/signal_handler.rb
Class Method Summary collapse
Instance Method Summary collapse
- #graceful_shutdown ⇒ Object
-
#handled_signals ⇒ Object
The signals setup_signals has installed handlers for (and will restore on restore_signals).
- #immediate_shutdown ⇒ Object
-
#interruptible_sleep(seconds) ⇒ Object
Sleep that can be interrupted by signals.
- #process_signals ⇒ Object
- #restore_signals ⇒ Object
- #setup_signals ⇒ Object
Class Method Details
.included(base) ⇒ Object
6 7 8 |
# File 'lib/pgbus/process/signal_handler.rb', line 6 def self.included(base) base.attr_reader :signal_queue end |
Instance Method Details
#graceful_shutdown ⇒ Object
60 61 62 |
# File 'lib/pgbus/process/signal_handler.rb', line 60 def graceful_shutdown raise NotImplementedError end |
#handled_signals ⇒ Object
The signals setup_signals has installed handlers for (and will restore on restore_signals). Empty until setup_signals runs.
12 13 14 |
# File 'lib/pgbus/process/signal_handler.rb', line 12 def handled_signals (@previous_handlers || {}).keys end |
#immediate_shutdown ⇒ Object
64 65 66 |
# File 'lib/pgbus/process/signal_handler.rb', line 64 def immediate_shutdown raise NotImplementedError end |
#interruptible_sleep(seconds) ⇒ Object
Sleep that can be interrupted by signals. Use this instead of Kernel#sleep in any loop that needs to respond to INT/TERM/QUIT promptly.
55 56 57 58 |
# File 'lib/pgbus/process/signal_handler.rb', line 55 def interruptible_sleep(seconds) @self_pipe_r.wait_readable(seconds) drain_self_pipe end |
#process_signals ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/pgbus/process/signal_handler.rb', line 38 def process_signals while (sig = begin @signal_queue.pop(true) rescue StandardError nil end) case sig when "INT", "TERM" graceful_shutdown when "QUIT" immediate_shutdown end end end |
#restore_signals ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/pgbus/process/signal_handler.rb', line 30 def restore_signals @previous_handlers&.each do |sig, handler| trap(sig, handler || "DEFAULT") end @self_pipe_r&.close unless @self_pipe_r&.closed? @self_pipe_w&.close unless @self_pipe_w&.closed? end |
#setup_signals ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/pgbus/process/signal_handler.rb', line 16 def setup_signals @signal_queue = Queue.new @previous_handlers = {} @self_pipe_r, @self_pipe_w = IO.pipe %w[INT TERM QUIT].each do |sig| @previous_handlers[sig] = trap(sig) do @signal_queue << sig # Wake any IO.select / interruptible_sleep call @self_pipe_w.write_nonblock(".", exception: false) end end end |