Sha256: ba92ff199af48643694011128f9381047a5029752efc7233195d17d4df1b8588

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

require 'sidekiq_unique_jobs/middleware/client/strategies/unique'
require 'sidekiq_unique_jobs/middleware/client/strategies/testing_inline'

module SidekiqUniqueJobs
  module Middleware
    module Client
      class UniqueJobs
        STRATEGIES = [
          Strategies::TestingInline,
          Strategies::Unique
        ]

        attr_reader :item, :worker_class, :redis_pool

        def call(worker_class, item, queue, redis_pool = nil)
          @worker_class = worker_class_constantize(worker_class)
          @item = item
          @redis_pool = redis_pool

          if unique_enabled?
            strategy.review(worker_class, item, queue, redis_pool) { yield }
          else
            yield
          end
        end

        private

        def unique_enabled?
          worker_class.get_sidekiq_options['unique'] || item['unique']
        end

        def strategy
          STRATEGIES.detect(&:elegible?)
        end

        # Attempt to constantize a string worker_class argument, always
        # failing back to the original argument.
        def worker_class_constantize(worker_class)
          return worker_class unless worker_class.is_a?(String)
          worker_class.constantize
        rescue NameError
          worker_class
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-3.0.8 lib/sidekiq_unique_jobs/middleware/client/unique_jobs.rb
sidekiq-unique-jobs-3.0.7 lib/sidekiq_unique_jobs/middleware/client/unique_jobs.rb
sidekiq-unique-jobs-3.0.5 lib/sidekiq_unique_jobs/middleware/client/unique_jobs.rb