Class: SidekiqUniqueJobs::Locksmith
- Inherits:
-
Object
- Object
- SidekiqUniqueJobs::Locksmith
- Includes:
- Connection
- Defined in:
- lib/sidekiq_unique_jobs/locksmith.rb
Overview
Lock manager class that handles all the various locks
Constant Summary collapse
- API_VERSION =
rubocop:disable ClassLength
'1'- EXPIRES_IN =
10
Instance Method Summary collapse
-
#available_count ⇒ Integer
The number of available resourced for this lock.
-
#create ⇒ String
Creates the necessary keys in redis to attempt a lock.
-
#delete ⇒ Object
Deletes the lock unless it has an expiration set.
-
#delete! ⇒ Object
Deletes the lock regardless of if it has an expiration set.
-
#exists? ⇒ true, false
Checks if the exists key is created in redis.
-
#initialize(item, redis_pool = nil) ⇒ Locksmith
constructor
A new instance of Locksmith.
-
#lock(timeout = nil) { ... } ⇒ Object
(also: #wait)
Create a lock for the item.
-
#locked?(token = nil) ⇒ true, false
Removes the lock keys from Redis.
-
#signal(token = nil) ⇒ Integer
Signal that the token should be released.
-
#unlock ⇒ false, String
Removes the lock keys from Redis.
Methods included from Connection
Constructor Details
#initialize(item, redis_pool = nil) ⇒ Locksmith
Returns a new instance of Locksmith.
18 19 20 21 22 23 24 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 18 def initialize(item, redis_pool = nil) @concurrency = 1 # removed in a0cff5bc42edbe7190d6ede7e7f845074d2d7af6 @expiration = item[LOCK_EXPIRATION_KEY] @jid = item[JID_KEY] @unique_digest = item[UNIQUE_DIGEST_KEY] @redis_pool = redis_pool end |
Instance Method Details
#available_count ⇒ Integer
The number of available resourced for this lock
45 46 47 48 49 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 45 def available_count return concurrency unless exists? redis(redis_pool) { |conn| conn.llen(available_key) } end |
#create ⇒ String
Creates the necessary keys in redis to attempt a lock
28 29 30 31 32 33 34 35 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 28 def create Scripts.call( :create, redis_pool, keys: [exists_key, grabbed_key, available_key, version_key, UNIQUE_SET, unique_digest], argv: [jid, expiration, API_VERSION, concurrency], ) end |
#delete ⇒ Object
Deletes the lock unless it has an expiration set
52 53 54 55 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 52 def delete return if expiration delete! end |
#delete! ⇒ Object
Deletes the lock regardless of if it has an expiration set
58 59 60 61 62 63 64 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 58 def delete! Scripts.call( :delete, redis_pool, keys: [exists_key, grabbed_key, available_key, version_key, UNIQUE_SET, unique_digest], ) end |
#exists? ⇒ true, false
Checks if the exists key is created in redis
39 40 41 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 39 def exists? redis(redis_pool) { |conn| conn.exists(exists_key) } end |
#lock(timeout = nil) { ... } ⇒ Object Also known as: wait
Create a lock for the item
71 72 73 74 75 76 77 78 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 71 def lock(timeout = nil, &block) create grab_token(timeout) do |token| touch_grabbed_token(token) return_token_or_block_value(token, &block) end end |
#locked?(token = nil) ⇒ true, false
Removes the lock keys from Redis
93 94 95 96 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 93 def locked?(token = nil) token ||= jid redis(redis_pool) { |conn| conn.hexists(grabbed_key, token) } end |
#signal(token = nil) ⇒ Integer
Signal that the token should be released
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 102 def signal(token = nil) token ||= jid Scripts.call( :signal, redis_pool, keys: [exists_key, grabbed_key, available_key, version_key, UNIQUE_SET, unique_digest], argv: [token, expiration], ) end |
#unlock ⇒ false, String
Removes the lock keys from Redis
84 85 86 87 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 84 def unlock return false unless locked? signal(jid) end |