Class: Shikibu::Locking::LockGuard
- Inherits:
-
Object
- Object
- Shikibu::Locking::LockGuard
- Defined in:
- lib/shikibu/locking.rb
Overview
Lock guard for automatic release
Instance Attribute Summary collapse
-
#instance_id ⇒ Object
readonly
Returns the value of attribute instance_id.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
-
#worker_id ⇒ Object
readonly
Returns the value of attribute worker_id.
Instance Method Summary collapse
-
#acquire(timeout: 300) ⇒ Boolean
Acquire the lock.
-
#acquired? ⇒ Boolean
Check if lock is held.
-
#initialize(storage, instance_id, worker_id) ⇒ LockGuard
constructor
A new instance of LockGuard.
-
#release ⇒ Object
Release the lock.
-
#with_lock(timeout: 300) ⇒ Object
Execute block with lock.
Constructor Details
#initialize(storage, instance_id, worker_id) ⇒ LockGuard
Returns a new instance of LockGuard.
71 72 73 74 75 76 |
# File 'lib/shikibu/locking.rb', line 71 def initialize(storage, instance_id, worker_id) @storage = storage @instance_id = instance_id @worker_id = worker_id @acquired = false end |
Instance Attribute Details
#instance_id ⇒ Object (readonly)
Returns the value of attribute instance_id.
69 70 71 |
# File 'lib/shikibu/locking.rb', line 69 def instance_id @instance_id end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
69 70 71 |
# File 'lib/shikibu/locking.rb', line 69 def storage @storage end |
#worker_id ⇒ Object (readonly)
Returns the value of attribute worker_id.
69 70 71 |
# File 'lib/shikibu/locking.rb', line 69 def worker_id @worker_id end |
Instance Method Details
#acquire(timeout: 300) ⇒ Boolean
Acquire the lock
81 82 83 |
# File 'lib/shikibu/locking.rb', line 81 def acquire(timeout: 300) @acquired = storage.try_acquire_lock(instance_id, worker_id, timeout: timeout) end |
#acquired? ⇒ Boolean
Check if lock is held
94 95 96 |
# File 'lib/shikibu/locking.rb', line 94 def acquired? @acquired end |
#release ⇒ Object
Release the lock
86 87 88 89 90 91 |
# File 'lib/shikibu/locking.rb', line 86 def release return unless @acquired storage.release_lock(instance_id, worker_id) @acquired = false end |
#with_lock(timeout: 300) ⇒ Object
Execute block with lock
99 100 101 102 103 104 105 106 107 |
# File 'lib/shikibu/locking.rb', line 99 def with_lock(timeout: 300) return unless acquire(timeout: timeout) begin yield ensure release end end |