Class: CMDx::Lock
- Inherits:
-
Object
- Object
- CMDx::Lock
- Defined in:
- lib/cmdx/lock.rb
Overview
Declared via Task.lock_with. After inputs resolve, acquires a store lease
(SET with nx: true) and runs idempotency (when declared), the full
retry_on loop, and output verification inside that lease.
Store keys are namespace:TaskName:segment (see StoreKey#compose). The
stored value is the holding task's tid. Release uses a conditional delete so
a late releaser cannot drop a newer holder's lease after TTL expiry.
After a successful acquire, the lease is released in ensure for any exit from
the wrapped block (success, skip! / fail!, or uncaught error). The lease
is not released between in-process retry_on attempts.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
-
.disabled ⇒ Lock
Disabled configuration (no key).
Instance Method Summary collapse
-
#build(new_key, new_options) ⇒ Lock
Returns a new Lock layering
new_optionsonto the current one. - #condition_if ⇒ Symbol, ...
- #condition_unless ⇒ Symbol, ...
- #enabled? ⇒ Boolean
-
#initialize(key, options = EMPTY_HASH) ⇒ Lock
constructor
A new instance of Lock.
- #namespace ⇒ String
-
#on_busy ⇒ Symbol
:skipor:fail. -
#process(task) { ... } ⇒ Object
Runs the given block behind a store lease when #enabled? and gates pass.
- #ttl ⇒ Integer
- #wait ⇒ Float
Constructor Details
#initialize(key, options = EMPTY_HASH) ⇒ Lock
Returns a new instance of Lock.
43 44 45 46 |
# File 'lib/cmdx/lock.rb', line 43 def initialize(key, = EMPTY_HASH) @key = key @options = .freeze end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
26 27 28 |
# File 'lib/cmdx/lock.rb', line 26 def key @key end |
Class Method Details
.disabled ⇒ Lock
Returns disabled configuration (no key).
20 21 22 |
# File 'lib/cmdx/lock.rb', line 20 def disabled DISABLED end |
Instance Method Details
#build(new_key, new_options) ⇒ Lock
Returns a new Lock layering new_options onto the current one. When
new_key is non-nil it replaces the resolver; when new_key is nil,
only options merge and the existing resolver is kept.
93 94 95 96 97 98 99 100 |
# File 'lib/cmdx/lock.rb', line 93 def build(new_key, ) return self if new_key.nil? && .empty? merged_key = new_key.nil? ? @key : new_key = @options.merge() self.class.new(merged_key, ) end |
#condition_if ⇒ Symbol, ...
72 73 74 |
# File 'lib/cmdx/lock.rb', line 72 def condition_if @options[:if] end |
#condition_unless ⇒ Symbol, ...
77 78 79 |
# File 'lib/cmdx/lock.rb', line 77 def condition_unless @options[:unless] end |
#enabled? ⇒ Boolean
82 83 84 |
# File 'lib/cmdx/lock.rb', line 82 def enabled? !key.nil? end |
#namespace ⇒ String
49 50 51 |
# File 'lib/cmdx/lock.rb', line 49 def namespace @options[:namespace] || "cmdx:lock" end |
#on_busy ⇒ Symbol
Returns :skip or :fail.
67 68 69 |
# File 'lib/cmdx/lock.rb', line 67 def on_busy @options[:on_busy] || :fail end |
#process(task) { ... } ⇒ Object
When the lease cannot be acquired, signals skip! or fail! on
task — control does not return to the caller of #process. Busy
metadata may include :holder and :retry_after when the backend
exposes the current value and TTL.
Runs the given block behind a store lease when #enabled? and gates pass.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/cmdx/lock.rb', line 114 def process(task) return yield unless enabled? return yield unless Util.satisfied?(condition_if, condition_unless, task) store_key = StoreKey.task_key(task, namespace:, resolver: key) return yield if store_key.nil? token = task.tid if acquire_lock(store_key, token) Telemetry.emit(task, :lock_acquired, lock_key: store_key) begin yield ensure release_lock(store_key, token) end else busy!(task, store_key) end end |
#ttl ⇒ Integer
54 55 56 |
# File 'lib/cmdx/lock.rb', line 54 def ttl @options[:ttl] || 300 end |
#wait ⇒ Float
59 60 61 62 63 64 |
# File 'lib/cmdx/lock.rb', line 59 def wait raw = @options[:wait] return 0.0 if raw.nil? || raw == false Float(raw) end |