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
- #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
54 55 56 |
# File 'lib/pgbus/process/signal_handler.rb', line 54 def graceful_shutdown raise NotImplementedError end |
#immediate_shutdown ⇒ Object
58 59 60 |
# File 'lib/pgbus/process/signal_handler.rb', line 58 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.
49 50 51 52 |
# File 'lib/pgbus/process/signal_handler.rb', line 49 def interruptible_sleep(seconds) @self_pipe_r.wait_readable(seconds) drain_self_pipe end |
#process_signals ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pgbus/process/signal_handler.rb', line 32 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
24 25 26 27 28 29 30 |
# File 'lib/pgbus/process/signal_handler.rb', line 24 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
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/pgbus/process/signal_handler.rb', line 10 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 |