Class: Agent::Lock::Facade
- Inherits:
-
Object
- Object
- Agent::Lock::Facade
- Defined in:
- lib/agent/lock/facade.rb,
lib/agent/lock/facade/memo.rb,
lib/agent/lock/facade/async.rb,
lib/agent/lock/facade/backend.rb,
lib/agent/lock/facade/refusal.rb,
lib/agent/lock/facade/session.rb,
lib/agent/lock/facade/strategies.rb
Overview
Every alock command as a method, run in this process, so a caller
pays for no new Ruby process per call. (git and ps still run, as
they do under the CLI, to find the checkout and check holders.)
One facade is one locker. It picks its backend when it is built and
keeps it, so every thread calling it takes locks in the same store. A
second facade with another redis_url is a separate locker. Building
one never touches local or distributed.
Thread-safe: a facade is frozen, and each call builds its own Manager, so threads share nothing but the store, whose mutex serializes the writes.
A lock method returns the Manager::Result on success. Where the CLI
would print a refusal on stderr, it raises Refused, whose result is
the same Manager::Result. :not_found from #release,
#break_lock and #resume is returned, although the CLI exits 1 on
it: nothing was there, so nothing was refused.
Every lock method also takes these keywords, which override the facade's defaults for that one call:
dir:[String] a directory inside the checkout (default: the current one)agent_id:[String] who holds the lockparent_id:[String, false] that holder's parent session;falsefor nonestale_minutes:[Integer] when an unverifiable lock counts as stalesession_name:[String] the session's name, tied to no process
The checkout's backend marker binds a facade just as it binds alock:
a facade claims a virgin checkout for its backend, and raises
Store::Mismatch in one already claimed for the other, so a facade
and the CLI can never take the same scope in two stores.
async: true adds #async, which runs any lock method on
concurrent-ruby's I/O pool and returns a Concurrent::Promises::Future:
value! is the result or re-raises, value is nil when the call raised
and reason holds why. Arguments, keywords and blocks pass through as
on a direct call, and direct calls stay synchronous. concurrent-ruby is
not a dependency of this gem; add it to the Gemfile to use this.
Defined Under Namespace
Modules: Refusal, Strategies Classes: Async, Backend, Memo, Session
Constant Summary collapse
- DEFAULT_STRATEGIES =
Tried in this order when the caller does not pass
strategies. %w[redis file].freeze
- DEFAULT_REDIS_URL =
Used when
redis_urlis omitted and neitherAGENT_LOCK_REDIS_URLnorREDIS_URLis set. It is the local Redisalockitself tries, by the same constant, so the two cannot drift apart again: this wasredis://localhost:6379, a second spelling of the same server. Store::RedisStore::LOCAL_URL
Class Method Summary collapse
-
.redis_url(redis_url = nil) ⇒ String
The Redis a facade uses, by the rule
alockfollows: what the caller named, thenAGENT_LOCK_REDIS_URL, thenREDIS_URL.
Instance Method Summary collapse
-
#acquire(scope, intent = nil, enforce: false, allow_wide: false, **session) {|result| ... } ⇒ Manager::Result, Object
alock acquire: claim a scope before writing in it. -
#async ⇒ Async
The lock methods again, each returning a future instead of waiting.
-
#backend_name ⇒ String
"redis" or "file", chosen once at construction.
-
#break_lock(scope, **session) ⇒ Manager::Result
alock break: drop somebody else's lock. -
#check(scope, **session) ⇒ Manager::Result
alock check: may I write here?. -
#initialize(strategies: DEFAULT_STRATEGIES, redis_url: nil, async: false, **defaults) ⇒ Facade
constructor
A new instance of Facade.
-
#list(**session) ⇒ Manager::Result
alock list. -
#mine(**session) ⇒ Manager::Result
alock mine. -
#note(scope, text, **session) ⇒ Manager::Result
alock note: leave a note on a lock you hold. -
#redis_url ⇒ String
The Redis URL this facade was given, used or not.
-
#release(scope, **session) ⇒ Manager::Result
alock release. -
#release_all(**session) ⇒ Manager::Result
alock release-all. -
#resume(scope, **session) ⇒ Manager::Result
alock resume: take back interrupted work, notes and all. -
#show_repo(repo = nil, dir: Dir.pwd, redis_url: nil) ⇒ Array<Store::RedisIndex::Lock>
alock show repo [owner/name]. -
#show_repos(redis_url: nil) ⇒ Array<Store::RedisIndex::Repo>
alock show repos. -
#skills_install(into: nil, agent: nil, overwrite: false) ⇒ Skill::Result
alock skills install. -
#skills_path ⇒ String
alock skills path. -
#version ⇒ String
alock version. -
#whoami(**session) ⇒ Identity
alock whoami: who a lock taken through this facade would name.
Constructor Details
#initialize(strategies: DEFAULT_STRATEGIES, redis_url: nil, async: false, **defaults) ⇒ Facade
Returns a new instance of Facade.
111 112 113 114 115 116 |
# File 'lib/agent/lock/facade.rb', line 111 def initialize(strategies: DEFAULT_STRATEGIES, redis_url: nil, async: false, **defaults) @backend = Backend.new(strategies:, redis_url: Facade.redis_url(redis_url)) @session = Session.new(@backend, **defaults) @async = async ? Async.tap(&:load!).new(self) : nil freeze end |
Class Method Details
.redis_url(redis_url = nil) ⇒ String
The Redis a facade uses, by the rule alock follows: what the
caller named, then AGENT_LOCK_REDIS_URL, then REDIS_URL.
A blank REDIS_URL counts as unset, as it does for alock
(Store::RedisStore.candidate_urls). Taken for a URL, REDIS_URL=""
sent a facade to the file store beside a local Redis that alock
went on to use, so the two could claim one new checkout for
different backends.
82 83 84 85 |
# File 'lib/agent/lock/facade.rb', line 82 def self.redis_url(redis_url = nil) ambient = ENV.fetch("REDIS_URL", "").strip redis_url || Store.named_redis_url || (ambient.empty? ? DEFAULT_REDIS_URL : ambient) end |
Instance Method Details
#acquire(scope, intent = nil, enforce: false, allow_wide: false, **session) {|result| ... } ⇒ Manager::Result, Object
alock acquire: claim a scope before writing in it.
With a block, yields the Manager::Result and returns the block's
value. The lock is released afterwards only when this call took it
(:acquired). :already_mine yields and leaves the lock you already
held, so a nested block does not drop the outer one. A refusal raises
before the block runs. An exception from the block releases, then
propagates. When that release is itself refused (somebody broke the
lock and took the scope while the block ran), the block's exception
still wins: replacing it with the release's Refused hid the bug
the caller actually had.
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/agent/lock/facade.rb', line 159 def acquire(scope, intent = nil, enforce: false, allow_wide: false, **session) result = Refusal.check!( @session.manager(**session).acquire(scope, intent: intent || "unspecified", enforce:, wide: allow_wide), scope: ) return result unless block_given? return yield(result) unless result.status == :acquired within(scope, session) { yield result } end |
#async ⇒ Async
The lock methods again, each returning a future instead of waiting.
133 |
# File 'lib/agent/lock/facade.rb', line 133 def async = @async || raise(ArgumentError, "build the facade with async: true to call #async") |
#backend_name ⇒ String
Returns "redis" or "file", chosen once at construction.
119 |
# File 'lib/agent/lock/facade.rb', line 119 def backend_name = @backend.name |
#break_lock(scope, **session) ⇒ Manager::Result
alock break: drop somebody else's lock. Announce it first. Never
raises a refusal: breaking a free scope returns :not_found.
216 |
# File 'lib/agent/lock/facade.rb', line 216 def break_lock(scope, **session) = @session.manager(**session).break_lock(scope) |
#check(scope, **session) ⇒ Manager::Result
alock check: may I write here?
186 |
# File 'lib/agent/lock/facade.rb', line 186 def check(scope, **session) = Refusal.check!(@session.manager(**session).check(scope), scope:) |
#list(**session) ⇒ Manager::Result
alock list.
193 |
# File 'lib/agent/lock/facade.rb', line 193 def list(**session) = @session.manager(**session).list |
#mine(**session) ⇒ Manager::Result
alock mine.
200 |
# File 'lib/agent/lock/facade.rb', line 200 def mine(**session) = @session.manager(**session).mine |
#note(scope, text, **session) ⇒ Manager::Result
alock note: leave a note on a lock you hold.
226 227 228 |
# File 'lib/agent/lock/facade.rb', line 226 def note(scope, text, **session) Refusal.check!(@session.manager(**session).note(scope, text), scope:, also: %i[not_found]) end |
#redis_url ⇒ String
Returns the Redis URL this facade was given, used or not.
122 |
# File 'lib/agent/lock/facade.rb', line 122 def redis_url = @backend.redis_url |
#release(scope, **session) ⇒ Manager::Result
alock release.
177 |
# File 'lib/agent/lock/facade.rb', line 177 def release(scope, **session) = Refusal.check!(@session.manager(**session).release(scope), scope:) |
#release_all(**session) ⇒ Manager::Result
alock release-all.
207 |
# File 'lib/agent/lock/facade.rb', line 207 def release_all(**session) = @session.manager(**session).release_all |
#resume(scope, **session) ⇒ Manager::Result
alock resume: take back interrupted work, notes and all.
238 |
# File 'lib/agent/lock/facade.rb', line 238 def resume(scope, **session) = Refusal.check!(@session.manager(**session).resume(scope), scope:) |
#show_repo(repo = nil, dir: Dir.pwd, redis_url: nil) ⇒ Array<Store::RedisIndex::Lock>
alock show repo [owner/name]. Redis only, same as #show_repos.
287 288 289 |
# File 'lib/agent/lock/facade.rb', line 287 def show_repo(repo = nil, dir: Dir.pwd, redis_url: nil) Store::RedisIndex.open(redis_url).locks(repo || Store::RedisIndex.slug_for(dir)) end |
#show_repos(redis_url: nil) ⇒ Array<Store::RedisIndex::Repo>
alock show repos. Redis only: the file store keeps one checkout's locks
and has no list of repositories.
274 |
# File 'lib/agent/lock/facade.rb', line 274 def show_repos(redis_url: nil) = Store::RedisIndex.open(redis_url).repos |
#skills_install(into: nil, agent: nil, overwrite: false) ⇒ Skill::Result
alock skills install.
259 260 261 |
# File 'lib/agent/lock/facade.rb', line 259 def skills_install(into: nil, agent: nil, overwrite: false) Refusal.check!(Skill.new(into: into || Skill.default_into(agent)).install(overwrite:)) end |
#skills_path ⇒ String
alock skills path.
249 |
# File 'lib/agent/lock/facade.rb', line 249 def skills_path = Skill.source |
#version ⇒ String
alock version.
266 |
# File 'lib/agent/lock/facade.rb', line 266 def version = VERSION |
#whoami(**session) ⇒ Identity
alock whoami: who a lock taken through this facade would name.
244 |
# File 'lib/agent/lock/facade.rb', line 244 def whoami(**session) = @session.identity(**session) |