Sha256: 2b1327377289c568dca6f29bf75d3de9771785f8b677f6ba442c0dff1797caf0

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module ActiveJob
  module TrafficControl
    module Disable
      extend ::ActiveSupport::Concern

      DISABLED_REENQUEUE_DELAY = 60...60 * 10
      SHOULD_DROP = "drop"
      SHOULD_DISABLE = "true"

      private_constant :SHOULD_DROP, :SHOULD_DISABLE, :DISABLED_REENQUEUE_DELAY

      class_methods do
        def disable!(drop: false)
          cache_client.write(disable_key, drop ? SHOULD_DROP : SHOULD_DISABLE)
        end

        def enable!
          cache_client.delete(disable_key)
        end

        def disabled?
          cache_client && !cache_client.read(disable_key).nil?
        end

        def disable_key
          @disable_key ||= "traffic_control:disable:#{cleaned_name}"
        end
      end

      included do
        include ActiveJob::TrafficControl::Base

        around_perform do |_, block|
          if cache_client
            disabled = cache_client.read(self.class.disable_key)

            if disabled == SHOULD_DROP
              drop("disabled")
            elsif disabled == SHOULD_DISABLE
              reenqueue(DISABLED_REENQUEUE_DELAY, "disabled")
            else
              block.call
            end
          else
            block.call
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
activejob-traffic_control-0.1.3 lib/active_job/traffic_control/disable.rb
activejob-traffic_control-0.1.2 lib/active_job/traffic_control/disable.rb
activejob-traffic_control-0.1.1 lib/active_job/traffic_control/disable.rb