Sha256: c3b41debe797a4938ba649121f2f2bc205d1faef4084df0beecc05daaaf23d16

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true


# Provides the Sidekiq middleware that make the unique job control work
#
# @see https://github.com/contribsys/faktory_worker_ruby/wiki/Middleware
module MultiBackgroundJob
  module Middleware
    class UniqueJob
      module Sidekiq
        def self.bootstrap
          if defined?(::Sidekiq)
            ::Sidekiq.configure_worker do |config|
              config.worker_middleware do |chain|
                chain.add Worker
              end
            end
          end
        end

        # Worker middleware runs around the execution of a job
        class Worker
          # @param worker [Object] the worker instance
          # @param job [Hash] the full job payload
          #   * @see https://github.com/mperham/sidekiq/wiki/Job-Format
          # @param queue [String] the name of the queue the job was pulled from
          # @yield the next middleware in the chain or worker `perform` method
          # @return [Void]
          def call(_worker, job, _queue)
            if job.is_a?(Hash) && (unique_lock = unique_job_lock(job))
              unique_lock.unlock
            end
            yield
          end

          protected

          def unique_job_lock(job)
            return unless job['uniq'].is_a?(Hash)

            unique_job = ::MultiBackgroundJob::UniqueJob.coerce(job['uniq'])
            unique_job&.lock
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
multi-background-job-0.1.0 lib/multi_background_job/middleware/unique_job/sidekiq.rb