Sha256: e0eb6550db985509216d9c20e447d78e0bb501b5553c5ec15b641a2699a599ea

Contents?: true

Size: 1.37 KB

Versions: 9

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require 'sidekiq_unique_jobs/server/middleware'

module SidekiqUniqueJobs
  module Client
    # The unique sidekiq middleware for the client push
    #
    # @author Mikael Henriksson <mikael@zoolutions.se>
    class Middleware
      include SidekiqUniqueJobs::Logging
      include OptionsWithFallback

      # Calls this client middleware
      #   Used from Sidekiq.process_single
      # @param [String] worker_class name of the sidekiq worker class
      # @param [Hash] item a sidekiq job hash
      # @param [String] queue name of the queue
      # @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
      # @yield when uniqueness is disable or lock successful
      def call(worker_class, item, queue, redis_pool = nil)
        @worker_class = worker_class
        @item         = item
        @queue        = queue
        @redis_pool   = redis_pool

        yield if success?
      end

      private

      # The sidekiq job hash
      # @return [Hash] the Sidekiq job hash
      attr_reader :item

      def success?
        unique_disabled? || locked?
      end

      def locked?
        locked = lock.lock
        warn_about_duplicate unless locked
        locked
      end

      def warn_about_duplicate
        return unless log_duplicate_payload?
        log_warn "payload is not unique #{item}"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-6.0.6 lib/sidekiq_unique_jobs/client/middleware.rb
sidekiq-unique-jobs-6.0.5 lib/sidekiq_unique_jobs/client/middleware.rb
sidekiq-unique-jobs-6.0.4 lib/sidekiq_unique_jobs/client/middleware.rb
sidekiq-unique-jobs-6.0.3 lib/sidekiq_unique_jobs/client/middleware.rb
sidekiq-unique-jobs-6.0.2 lib/sidekiq_unique_jobs/client/middleware.rb
sidekiq-unique-jobs-6.0.1 lib/sidekiq_unique_jobs/client/middleware.rb
sidekiq-unique-jobs-6.0.0 lib/sidekiq_unique_jobs/client/middleware.rb
sidekiq-unique-jobs-6.0.0.rc8 lib/sidekiq_unique_jobs/client/middleware.rb
sidekiq-unique-jobs-6.0.0.rc7 lib/sidekiq_unique_jobs/client/middleware.rb