1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/middleware/client/unique_jobs.rb
Mike Perham f9af66edd7 Rework redis connections so that the manager and
the client use separate pools.

This is so the Rails app Sidekiq::Client and 
Sidekiq::Manager can use different configurations.

Also, fix issue where workers were not unregistered
in Redis upon shutdown.
2012-02-11 13:14:03 -08:00

30 lines
586 B
Ruby

require 'digest'
module Sidekiq
module Middleware
module Client
class UniqueJobs
HASH_KEY_EXPIRATION = 30 * 60
def initialize(redis)
@redis = redis
end
def call(item, queue)
payload_hash = Digest::MD5.hexdigest(MultiJson.encode(item))
return if already_scheduled?(payload_hash)
@redis.setex(payload_hash, HASH_KEY_EXPIRATION, 1)
yield
end
private
def already_scheduled?(payload_hash)
!!@redis.get(payload_hash)
end
end
end
end
end