Sha256: c731d86c87bdf6d8dadfc6144fc7ecbfb402ca4dd93257b8575de8b29233c18c

Contents?: true

Size: 825 Bytes

Versions: 1

Compression:

Stored size: 825 Bytes

Contents

require 'digest'

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

        def call(worker_class, item, queue)

          enabled = worker_class.get_sidekiq_options['unique']

          if enabled

            payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(item['args']))

            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
          else
            yield
          end
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-2.2.0 lib/sidekiq-unique-jobs/middleware/client/unique_jobs.rb