Class: Rpush::Daemon::SignalHandler

Inherits:
Object
  • Object
show all
Extended by:
Loggable
Defined in:
lib/rpush/daemon/signal_handler.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Loggable

log_debug, log_error, log_info, log_warn

Class Attribute Details

.threadObject (readonly)

Returns the value of attribute thread.



7
8
9
# File 'lib/rpush/daemon/signal_handler.rb', line 7

def thread
  @thread
end

Class Method Details

.handle_hupObject



57
58
59
60
61
62
63
# File 'lib/rpush/daemon/signal_handler.rb', line 57

def self.handle_hup
  Rpush.logger.reopen
  Rpush.logger.info('Received HUP signal.')
  Rpush::Daemon.store.reopen_log
  Synchronizer.sync
  Feeder.wakeup
end

.handle_usr2Object



65
66
67
68
# File 'lib/rpush/daemon/signal_handler.rb', line 65

def self.handle_usr2
  Rpush.logger.info('Received USR2 signal.')
  AppRunner.debug
end

.startObject



10
11
12
13
14
15
16
17
18
# File 'lib/rpush/daemon/signal_handler.rb', line 10

def self.start
  return unless trap_signals?

  read_io, @write_io = IO.pipe
  start_handler(read_io)
  %w(INT TERM HUP USR2).each do |signal|
    Signal.trap(signal) { @write_io.puts(signal) }
  end
end

.start_handler(read_io) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rpush/daemon/signal_handler.rb', line 30

def self.start_handler(read_io)
  @thread = Thread.new do
    while readable_io = IO.select([read_io]) # rubocop:disable AssignmentInCondition
      signal = readable_io.first[0].gets.strip

      begin
        case signal
        when 'HUP'
          handle_hup
        when 'USR2'
          handle_usr2
        when 'INT', 'TERM'
          Thread.new { Rpush::Daemon.shutdown }
          break
        when 'break'
          break
        else
          Rpush.logger.error("Unhandled signal: #{signal}")
        end
      rescue StandardError => e
        Rpush.logger.error("Error raised when handling signal '#{signal}'")
        Rpush.logger.error(e)
      end
    end
  end
end

.stopObject



20
21
22
23
24
25
26
27
28
# File 'lib/rpush/daemon/signal_handler.rb', line 20

def self.stop
  @write_io.puts('break') if @write_io
  @thread.join if @thread
rescue StandardError => e
  log_error(e)
  reflect(:error, e)
ensure
  @thread = nil
end

.trap_signals?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rpush/daemon/signal_handler.rb', line 70

def self.trap_signals?
  !Rpush.config.embedded
end