Sha256: abd4d23403dcdccb75c446a7f17c31bdb95519b8b52a5e1a6e1096fb188bb12b

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

require 'active_support/concern'
require 'concurrent'

module RocketJob
  class Supervisor
    module Shutdown
      extend ActiveSupport::Concern

      included do
        # Set shutdown indicator for this server process
        def self.shutdown!
          @shutdown.set
          event!
        end

        # Returns [true|false] whether the shutdown indicator has been set for this server process
        def self.shutdown?
          @shutdown.set?
        end

        # An event has occured
        def self.event!
          @event.set
        end

        # Returns [true|false] whether the shutdown indicator was set before the timeout was reached
        def self.wait_for_event(timeout = nil)
          @event.wait(timeout)
          @event.reset
        end

        @shutdown = Concurrent::Event.new
        @event    = Concurrent::Event.new

        # Register handlers for the various signals
        # Term:
        #   Perform clean shutdown
        #
        def self.register_signal_handlers
          Signal.trap 'SIGTERM' do
            Thread.new do
              shutdown!
              message = 'Shutdown signal (SIGTERM) received. Will shutdown as soon as active jobs/slices have completed.'
              logger.warn(message)
            end
          end

          Signal.trap 'INT' do
            Thread.new do
              shutdown!
              message = 'Shutdown signal (INT) received. Will shutdown as soon as active jobs/slices have completed.'
              logger.warn(message)
            end
          end
        rescue StandardError
          logger.warn 'SIGTERM handler not installed. Not able to shutdown gracefully'
        end

        private_class_method :register_signal_handlers
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rocketjob-4.1.0 lib/rocket_job/supervisor/shutdown.rb