Class: Rpush::Daemon::SignalHandler

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.threadObject (readonly)

Returns the value of attribute thread.



5
6
7
# File 'lib/rpush/daemon/signal_handler.rb', line 5

def thread
  @thread
end

Class Method Details

.handle_hupObject



50
51
52
53
54
# File 'lib/rpush/daemon/signal_handler.rb', line 50

def self.handle_hup
  Rpush.logger.info("Received HUP signal.")
  Synchronizer.sync
  Feeder.wakeup
end

.handle_usr2Object



56
57
58
59
# File 'lib/rpush/daemon/signal_handler.rb', line 56

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

.startObject



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

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rpush/daemon/signal_handler.rb', line 23

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 hndling signal '#{signal}'")
        Rpush.logger.error(e)
      end
    end
  end
end

.stopObject



18
19
20
21
# File 'lib/rpush/daemon/signal_handler.rb', line 18

def self.stop
  @write_io.puts('break') if @write_io
  @thread.join if @thread
end

.trap_signals?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/rpush/daemon/signal_handler.rb', line 61

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