Sha256: f69ebf6cb07c3ad2a0f74b03573827ce840cc201341565b20173fb9f6ef66ffd

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require_relative "interruption_adapters/null_adapter"

module JobIteration
  module InterruptionAdapters
    BUNDLED_ADAPTERS = [:resque, :sidekiq].freeze # @api private

    class << self
      # Returns adapter for specified name.
      #
      #   JobIteration::InterruptionAdapters.lookup(:sidekiq)
      #   # => JobIteration::InterruptionAdapters::SidekiqAdapter
      def lookup(name)
        registry.fetch(name.to_sym) do
          Deprecation.warn(<<~DEPRECATION_MESSAGE, caller_locations(1))
            No interruption adapter is registered for #{name.inspect}; falling back to `NullAdapter`, which never interrupts.
            Use `JobIteration::InterruptionAdapters.register(#{name.to_sym.inspect}, <adapter>) to register one.
            This will raise starting in version #{Deprecation.deprecation_horizon} of #{Deprecation.gem_name}!"
          DEPRECATION_MESSAGE

          NullAdapter
        end
      end

      # Registers adapter for specified name.
      #
      #   JobIteration::InterruptionAdapters.register(:sidekiq, MyCustomSidekiqAdapter)
      def register(name, adapter)
        raise ArgumentError, "adapter must be callable" unless adapter.respond_to?(:call)

        registry[name.to_sym] = adapter
      end

      private

      attr_reader :registry
    end

    @registry = {}

    # Built-in Rails adapters. It doesn't make sense to interrupt for these.
    register(:async, NullAdapter)
    register(:inline, NullAdapter)
    register(:test, NullAdapter)

    # External adapters
    BUNDLED_ADAPTERS.each do |name|
      require_relative "interruption_adapters/#{name}_adapter"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
job-iteration-1.5.1 lib/job-iteration/interruption_adapters.rb
job-iteration-1.5.0 lib/job-iteration/interruption_adapters.rb