Module: Mistri::Locks

Defined in:
lib/mistri/locks.rb,
lib/mistri/locks/rails_cache.rb

Overview

Cross-process coordination for hosts that run loops in more than one process: leases that say "this is alive right now" and flags that carry one-bit requests (stop) between processes. Everything expires on its own, so a crashed holder never wedges anything; a live holder renews on a heartbeat, so only dead ones expire.

Configure once at boot:

Mistri.locks = Mistri::Locks::Memory.new          # single process
Mistri.locks = Mistri::Locks::RailsCache.new      # requires opt-in file

An adapter implements six methods: acquire(key, ttl:) -> bool, renew(key, ttl:), release(key), held?(key), set_flag(key, ttl:), flag?(key), clear_flag(key). With no adapter configured, everything lease-aware degrades gracefully: children read :running instead of :interrupted, and holds are no-ops.

Defined Under Namespace

Classes: Hold, Memory, RailsCache

Constant Summary collapse

LEASE_TTL =
180
HEARTBEAT =
60

Class Method Summary collapse

Class Method Details

.hold(key, ttl: LEASE_TTL, heartbeat: HEARTBEAT, stop_key: nil, signal: nil) ⇒ Object

Hold a lease for the duration of a block of work: acquire, renew on a heartbeat from a background thread, release on the way out. Returns a Hold to release in an ensure, or nil when no adapter is configured or the lease is already held elsewhere: a caller that was refused must never renew or delete the real holder's key. Callers that need to tell refusal apart from no-adapter use the adapter's acquire directly.

With stop_key: and signal:, the same thread watches the flag and trips the signal within a tick, so a stop request written in another process becomes this run's cooperative abort.



38
39
40
41
42
43
44
# File 'lib/mistri/locks.rb', line 38

def hold(key, ttl: LEASE_TTL, heartbeat: HEARTBEAT, stop_key: nil, signal: nil)
  adapter = Mistri.locks
  return nil unless adapter
  return nil unless adapter.acquire(key, ttl: ttl)

  Hold.new(adapter, key, ttl, heartbeat, stop_key: stop_key, signal: signal)
end