Class: Sidecloq::Locker
- Inherits:
-
Object
- Object
- Sidecloq::Locker
- Includes:
- Utils
- Defined in:
- lib/sidecloq/locker.rb
Overview
Locker obtains or waits for an exclusive lock on a key in redis
Constant Summary collapse
- DEFAULT_LOCK_KEY =
"sidecloq_leader_lock"
Instance Method Summary collapse
- #get_or_refresh_lock ⇒ Object
- #has_lock? ⇒ Boolean
-
#initialize(options = {}) ⇒ Locker
constructor
A new instance of Locker.
- #start ⇒ Object
- #stop(timeout = nil) ⇒ Object
-
#with_lock ⇒ Object
blocks until lock is obtained, then yields.
Methods included from Utils
included, #logger, #redis, #will_never_run
Constructor Details
#initialize(options = {}) ⇒ Locker
Returns a new instance of Locker.
8 9 10 11 12 13 14 15 16 |
# File 'lib/sidecloq/locker.rb', line 8 def initialize(={}) # we keep a connection from the pool by default @redis = [:redis] || Sidekiq.redis_pool.checkout @key = [:lock_key] || DEFAULT_LOCK_KEY @ttl = [:ttl] || 60 @check_interval = [:check_interval] || 15 @lock_manager = Redlock::Client.new([@redis]) @obtained_lock = Concurrent::Event.new end |
Instance Method Details
#get_or_refresh_lock ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/sidecloq/locker.rb', line 49 def get_or_refresh_lock # redlock is in ms, not seconds @lock = @lock_manager.lock(@key, @ttl * 1000, extend: @lock) @obtained_lock.set if @lock logger.debug("Leader lock #{"not " if !@lock}held") @lock end |
#has_lock? ⇒ Boolean
35 36 37 |
# File 'lib/sidecloq/locker.rb', line 35 def has_lock? @obtained_lock.set? end |
#start ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/sidecloq/locker.rb', line 41 def start logger.debug("Starting locker check task") @check_task = Concurrent::TimerTask.new(execution_interval: @check_interval, run_now: true) do get_or_refresh_lock end @check_task.execute end |
#stop(timeout = nil) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/sidecloq/locker.rb', line 26 def stop(timeout = nil) if @check_task logger.debug("Stopping locker check task") @check_task.shutdown @check_task.wait_for_termination(timeout) logger.debug("Stopped locker check task") end end |
#with_lock ⇒ Object
blocks until lock is obtained, then yields
19 20 21 22 23 24 |
# File 'lib/sidecloq/locker.rb', line 19 def with_lock start @obtained_lock.wait yield stop end |