Class: Sidekiq::Middleware::Client::UniqueJobs

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/middleware/client/unique_jobs.rb

Constant Summary collapse

HASH_KEY_EXPIRATION =
30 * 60

Instance Method Summary collapse

Instance Method Details

#call(worker_class, item, queue) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sidekiq/middleware/client/unique_jobs.rb', line 10

def call(worker_class, item, queue)
  enabled = worker_class.get_sidekiq_options['unique']
  if enabled
    payload_hash = Digest::MD5.hexdigest(Sidekiq.dump_json(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
  else
    yield
  end
end