Sha256: c0cad6f9ace663b99d7c893522b8537b5c5946700ce241d2733a02dd410d70b3

Contents?: true

Size: 1.67 KB

Versions: 9

Compression:

Stored size: 1.67 KB

Contents

module Rpush
  module Daemon
    class SignalHandler
      class << self
        attr_reader :thread
      end

      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

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

      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

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

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rpush-2.4.0-java lib/rpush/daemon/signal_handler.rb
rpush-2.4.0 lib/rpush/daemon/signal_handler.rb
rpush-2.3.2-java lib/rpush/daemon/signal_handler.rb
rpush-2.3.2 lib/rpush/daemon/signal_handler.rb
rpush-2.3.1-java lib/rpush/daemon/signal_handler.rb
rpush-2.3.1 lib/rpush/daemon/signal_handler.rb
rpush-2.3.0-java lib/rpush/daemon/signal_handler.rb
rpush-2.3.0 lib/rpush/daemon/signal_handler.rb
rpush-2.3.0.rc1 lib/rpush/daemon/signal_handler.rb