Class: Sidekiq::Lock::RedisLock
- Inherits:
-
Object
- Object
- Sidekiq::Lock::RedisLock
- Defined in:
- lib/sidekiq/lock/redis_lock.rb
Instance Method Summary collapse
-
#acquire! ⇒ Object
acquire lock using modified SET command introduced in Redis 2.6.12 this also requires redis-rb >= 3.0.5.
- #acquired? ⇒ Boolean
-
#initialize(options_hash, payload) ⇒ RedisLock
constructor
checks for configuration.
- #name ⇒ Object
- #release! ⇒ Object
- #timeout ⇒ Object
Constructor Details
#initialize(options_hash, payload) ⇒ RedisLock
checks for configuration
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/sidekiq/lock/redis_lock.rb', line 5 def initialize(, payload) @options = {} .each_key do |key| @options[key.to_sym] = [key] end @payload = payload @acquired = false timeout && name end |
Instance Method Details
#acquire! ⇒ Object
acquire lock using modified SET command introduced in Redis 2.6.12 this also requires redis-rb >= 3.0.5
24 25 26 27 28 29 30 31 32 |
# File 'lib/sidekiq/lock/redis_lock.rb', line 24 def acquire! @acquired ||= Sidekiq.redis do |r| if Sidekiq::VERSION >= '7.2' r.set(name, value, 'nx', 'px', timeout) else r.set(name, value, nx: true, px: timeout) end end end |
#acquired? ⇒ Boolean
18 19 20 |
# File 'lib/sidekiq/lock/redis_lock.rb', line 18 def acquired? @acquired end |
#name ⇒ Object
43 44 45 46 47 |
# File 'lib/sidekiq/lock/redis_lock.rb', line 43 def name raise ArgumentError, 'Provide a lock name inside sidekiq_options' if [:name].nil? @name ||= ([:name].respond_to?(:call) ? [:name].call(*payload) : [:name]) end |
#release! ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/sidekiq/lock/redis_lock.rb', line 34 def release! # even if lock expired / was take over by another process # it still means from our perspective that we released it @acquired = false # https://redis.io/commands/del/#return release_lock == 1 end |
#timeout ⇒ Object
49 50 51 52 53 |
# File 'lib/sidekiq/lock/redis_lock.rb', line 49 def timeout raise ArgumentError, 'Provide lock timeout inside sidekiq_options' if [:timeout].nil? @timeout ||= ([:timeout].respond_to?(:call) ? [:timeout].call(*payload) : [:timeout]).to_i end |