Sha256: 0c7b8adf1a29ce31644334a57fc58a6ca17c39ae269425da83ea852f44067e7b

Contents?: true

Size: 1.81 KB

Versions: 24

Compression:

Stored size: 1.81 KB

Contents

module Rpush
  module Daemon
    class SignalHandler
      extend Loggable

      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
      rescue StandardError => e
        log_error(e)
        reflect(:error, e)
      ensure
        @thread = nil
      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

24 entries across 24 versions & 2 rubygems

Version Path
rpush-4.1.1 lib/rpush/daemon/signal_handler.rb
rpush-4.1.0 lib/rpush/daemon/signal_handler.rb
rpush-4.0.1 lib/rpush/daemon/signal_handler.rb
rpush-4.0.0 lib/rpush/daemon/signal_handler.rb
rpush-3.3.1 lib/rpush/daemon/signal_handler.rb
rpush-3.3.0 lib/rpush/daemon/signal_handler.rb
rpush_extended-3.2.6 lib/rpush/daemon/signal_handler.rb
rpush_extended-3.2.5 lib/rpush/daemon/signal_handler.rb
rpush-3.2.4 lib/rpush/daemon/signal_handler.rb
rpush-3.2.3 lib/rpush/daemon/signal_handler.rb
rpush-3.2.2 lib/rpush/daemon/signal_handler.rb
rpush-3.2.1 lib/rpush/daemon/signal_handler.rb
rpush-3.2.0 lib/rpush/daemon/signal_handler.rb
rpush-3.1.1 lib/rpush/daemon/signal_handler.rb
rpush-3.1.0 lib/rpush/daemon/signal_handler.rb
rpush-3.0.2 lib/rpush/daemon/signal_handler.rb
rpush-3.0.1 lib/rpush/daemon/signal_handler.rb
rpush-3.0.0 lib/rpush/daemon/signal_handler.rb
rpush-3.0.0.rc1 lib/rpush/daemon/signal_handler.rb
rpush-2.7.0-java lib/rpush/daemon/signal_handler.rb