Sha256: 822d8a1ac5b0f195b0f9719c2bc812f23c07cd40e6f478c0a99dff0caa9376b2

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

# Server midleware for Faktory
#
# @see https://github.com/contribsys/faktory_worker_ruby/wiki/Middleware
module MultiBackgroundJob
  module Middleware
    class UniqueJob
      module Faktory
        def self.bootstrap
          if defined?(::Faktory)
            ::Faktory.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
          def call(_jobinst, payload)
            if payload.is_a?(Hash) && (unique_lock = unique_job_lock(payload))
              unique_lock.unlock
            end
            yield
          end

          protected

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

            unique_job = ::MultiBackgroundJob::UniqueJob.coerce(payload['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/faktory.rb