Class: SidekiqUniqueJobs::Lock::BaseLock Abstract
- Inherits:
-
Object
- Object
- SidekiqUniqueJobs::Lock::BaseLock
- Includes:
- SidekiqUniqueJobs::Logging
- Defined in:
- lib/sidekiq_unique_jobs/lock/base_lock.rb
Overview
This class is abstract.
Abstract base class for locks
Direct Known Subclasses
UntilAndWhileExecuting, UntilExecuted, UntilExecuting, UntilExpired, WhileExecuting
Instance Method Summary collapse
-
#delete ⇒ Object
Deletes the job from redis if it is locked.
-
#delete! ⇒ Object
Forcefully deletes the job from redis.
-
#execute ⇒ Object
Execute the job in the Sidekiq server processor.
-
#initialize(item, callback, redis_pool = nil) ⇒ BaseLock
constructor
A new instance of BaseLock.
-
#lock ⇒ String
Handles locking of sidekiq jobs.
-
#locked? ⇒ true, false
Checks if the item has achieved a lock.
-
#unlock ⇒ String, false
Unlocks the job from redis.
Methods included from SidekiqUniqueJobs::Logging
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context
Constructor Details
#initialize(item, callback, redis_pool = nil) ⇒ BaseLock
Returns a new instance of BaseLock.
15 16 17 18 19 20 |
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 15 def initialize(item, callback, redis_pool = nil) @item = item @callback = callback @redis_pool = redis_pool add_uniqueness_when_missing # Used to ease testing end |
Instance Method Details
#delete ⇒ Object
Deletes the job from redis if it is locked.
50 51 52 |
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 50 def delete locksmith.delete # Soft delete (don't forcefully remove when expiration is set) end |
#delete! ⇒ Object
Forcefully deletes the job from redis.
This is good for jobs when a previous lock was not unlocked
56 57 58 |
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 56 def delete! locksmith.delete! # Force delete the lock end |
#execute ⇒ Object
Execute the job in the Sidekiq server processor
38 39 40 |
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 38 def execute raise NotImplementedError, "##{__method__} needs to be implemented in #{self.class}" end |
#lock ⇒ String
Handles locking of sidekiq jobs.
Will call a conflict strategy if lock can't be achieved.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 25 def lock @attempt = 0 return item[JID_KEY] if locked? if (token = locksmith.lock(item[LOCK_TIMEOUT_KEY])) token else call_strategy end end |
#locked? ⇒ true, false
Checks if the item has achieved a lock
63 64 65 |
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 63 def locked? locksmith.locked?(item[JID_KEY]) end |
#unlock ⇒ String, false
Unlocks the job from redis
45 46 47 |
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 45 def unlock locksmith.unlock(item[JID_KEY]) # Only signal to release the lock end |