Sha256: e6e48f2e82cfff643443a4c4e4048c3bd7da9b2a52ec17fa140df37a0f371847

Contents?: true

Size: 1.72 KB

Versions: 24

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.info(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.info(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

24 entries across 24 versions & 1 rubygems

Version Path
rocketjob-5.2.0 lib/rocket_job/supervisor/shutdown.rb
rocketjob-5.2.0.beta3 lib/rocket_job/supervisor/shutdown.rb
rocketjob-5.2.0.beta2 lib/rocket_job/supervisor/shutdown.rb
rocketjob-5.2.0.beta1 lib/rocket_job/supervisor/shutdown.rb