Sha256: d98446263e9e94f403a3533361a367cf7d06c282ecf2d9715c1b48c2413e4d58

Contents?: true

Size: 653 Bytes

Versions: 2

Compression:

Stored size: 653 Bytes

Contents

require 'multi_json'
require 'digest'

module Sidekiq
  module Middleware
    module Client
      class UniqueJobs
        HASH_KEY_EXPIRATION = 30 * 60

        def call(item, queue)
          payload_hash = Digest::MD5.hexdigest(MultiJson.encode(item))
          unique = false

          Sidekiq.redis do |conn|
            conn.watch(payload_hash)

            if conn.get(payload_hash)
              conn.unwatch
            else
              unique = conn.multi do
                conn.setex(payload_hash, HASH_KEY_EXPIRATION, 1)
              end
            end
          end

          yield if unique
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sidekiq-0.10.1 lib/sidekiq/middleware/client/unique_jobs.rb
sidekiq-0.10.0 lib/sidekiq/middleware/client/unique_jobs.rb