Sha256: 63e6e8df7cf059ed96fee0b97be4de5be3c95e7984708370acf70207f2fbfa04

Contents?: true

Size: 1.81 KB

Versions: 13

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 Lint/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

13 entries across 13 versions & 1 rubygems

Version Path
rpush-9.1.0 lib/rpush/daemon/signal_handler.rb
rpush-9.0.0 lib/rpush/daemon/signal_handler.rb
rpush-8.0.0 lib/rpush/daemon/signal_handler.rb
rpush-7.0.1 lib/rpush/daemon/signal_handler.rb
rpush-7.0.0 lib/rpush/daemon/signal_handler.rb
rpush-6.0.1 lib/rpush/daemon/signal_handler.rb
rpush-6.0.0 lib/rpush/daemon/signal_handler.rb
rpush-5.4.0 lib/rpush/daemon/signal_handler.rb
rpush-5.3.0 lib/rpush/daemon/signal_handler.rb
rpush-5.2.0 lib/rpush/daemon/signal_handler.rb
rpush-5.1.0 lib/rpush/daemon/signal_handler.rb
rpush-5.0.0 lib/rpush/daemon/signal_handler.rb
rpush-4.2.0 lib/rpush/daemon/signal_handler.rb