Class: Sidekiq::Single::Lock
- Inherits:
-
Object
- Object
- Sidekiq::Single::Lock
- Defined in:
- lib/sidekiq/single/lock.rb
Constant Summary collapse
- DIGEST_KEY =
"digest"
Instance Attribute Summary collapse
-
#item ⇒ Object
readonly
Returns the value of attribute item.
Instance Method Summary collapse
- #acquire_or_discard ⇒ Object
- #fastened? ⇒ Boolean
-
#initialize(item, pool = nil) ⇒ Lock
constructor
A new instance of Lock.
- #perform_and_release ⇒ Object
- #release ⇒ Object
Constructor Details
#initialize(item, pool = nil) ⇒ Lock
Returns a new instance of Lock.
11 12 13 14 |
# File 'lib/sidekiq/single/lock.rb', line 11 def initialize(item, pool = nil) @item = item @pool = pool || ::Sidekiq.redis_pool end |
Instance Attribute Details
#item ⇒ Object (readonly)
Returns the value of attribute item.
9 10 11 |
# File 'lib/sidekiq/single/lock.rb', line 9 def item @item end |
Instance Method Details
#acquire_or_discard ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sidekiq/single/lock.rb', line 25 def acquire_or_discard args = item.key?("wrapped") ? item.dig("args", 0, "arguments") : item["args"] method = item.delete("unique_args") ttl = item["unique_for"] args = Array(method.call(args)) if method digest = item[DIGEST_KEY] = digest(args) if @pool.with { |conn| conn.call("SET", digest, item["jid"], "NX", "EX", ttl) } yield else handle_conflict end end |
#fastened? ⇒ Boolean
16 17 18 19 20 21 22 23 |
# File 'lib/sidekiq/single/lock.rb', line 16 def fastened? args = item["args"] method = item["unique_args"] args = Array(method.call(args)) if method digest = digest(args) @pool.with { |conn| conn.call("EXISTS", digest) } > 0 end |
#perform_and_release ⇒ Object
39 40 41 42 43 |
# File 'lib/sidekiq/single/lock.rb', line 39 def perform_and_release res = yield release res end |
#release ⇒ Object
45 46 47 |
# File 'lib/sidekiq/single/lock.rb', line 45 def release @pool.with { |conn| conn.call("FCALL", "single_release_lock", 1, item["digest"], item["jid"]) } end |