Class: Hutch::Waiter

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/hutch/waiter.rb

Overview

Signal-handling class.

Currently, the signal USR2 performs a thread dump, while QUIT, TERM and INT all perform a graceful shutdown.

Defined Under Namespace

Classes: ContinueProcessingSignals

Constant Summary collapse

SHUTDOWN_SIGNALS =
supported_signals_of(%w(QUIT TERM INT)).freeze
USER_SIGNALS =

We have chosen a JRuby-supported signal

supported_signals_of(%w(USR2)).freeze
REGISTERED_SIGNALS =
(SHUTDOWN_SIGNALS + USER_SIGNALS).freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger, logger=, setup_logger

Class Method Details

.supported_signals_of(list) ⇒ Object



14
15
16
17
18
# File 'lib/hutch/waiter.rb', line 14

def self.supported_signals_of(list)
  list.keep_if { |s| Signal.list.keys.include?(s) }.tap do |result|
    result.delete('QUIT') if defined?(JRUBY_VERSION)
  end
end

.wait_until_signaledObject



25
26
27
# File 'lib/hutch/waiter.rb', line 25

def self.wait_until_signaled
  new.wait_until_signaled
end

Instance Method Details

#handle_shutdown_signal(sig) ⇒ Object



62
63
64
# File 'lib/hutch/waiter.rb', line 62

def handle_shutdown_signal(sig)
  logger.info "caught SIG#{sig}, stopping hutch..."
end

#handle_signal(sig) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/hutch/waiter.rb', line 44

def handle_signal(sig)
  raise ContinueProcessingSignals unless REGISTERED_SIGNALS.include?(sig)
  if user_signal?(sig)
    handle_user_signal(sig)
  else
    handle_shutdown_signal(sig)
  end
end

#handle_user_signal(sig) ⇒ Object

Raises:

  • ContinueProcessingSignals



54
55
56
57
58
59
60
# File 'lib/hutch/waiter.rb', line 54

def handle_user_signal(sig)
  case sig
  when 'USR2' then log_thread_backtraces
  else raise "Assertion failed - unhandled signal: #{sig.inspect}"
  end
  raise ContinueProcessingSignals
end

#wait_until_signaledObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hutch/waiter.rb', line 29

def wait_until_signaled
  self.sig_read, self.sig_write = IO.pipe

  register_signal_handlers

  begin
    wait_for_signal

    sig = sig_read.gets.strip
    handle_signal(sig)
  rescue ContinueProcessingSignals
    retry
  end
end