Sha256: 83dce145d9cf0923bec78420a502faf722d2377c1613e0907944c827fcf8f85b
Contents?: true
Size: 1.21 KB
Versions: 2
Compression:
Stored size: 1.21 KB
Contents
module Sidetiq class Lock # :nodoc: all attr_reader :key, :timeout OWNER = "#{Socket.gethostname}:#{Process.pid}" def initialize(key, timeout = Sidetiq.config.lock_expire) @key = key.kind_of?(Class) ? "sidetiq:#{key.name}:lock" : "sidetiq:#{key}:lock" @timeout = timeout end def synchronize Sidekiq.redis do |redis| if lock(redis) Sidetiq.logger.debug "Sidetiq::Clock lock #{key}" begin yield redis ensure unlock(redis) Sidetiq.logger.debug "Sidetiq::Clock unlock #{key}" end end end end private def lock(redis) acquired = false watch(redis, key) do if !redis.exists(key) acquired = !!redis.multi do |multi| multi.psetex(key, timeout, OWNER) end end end acquired end def unlock(redis) watch(redis, key) do if redis.get(key) == OWNER redis.multi do |multi| multi.del(key) end end end end def watch(redis, *args) redis.watch(*args) begin yield ensure redis.unwatch end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
sidetiq-0.3.7 | lib/sidetiq/lock.rb |
sidetiq-0.3.6 | lib/sidetiq/lock.rb |