Class: Agent::Lock::Facade::Session
- Inherits:
-
Object
- Object
- Agent::Lock::Facade::Session
- Defined in:
- lib/agent/lock/facade/session.rb
Overview
Who is asking, against which store: a Manager per call.
Holds the facade's defaults and nothing that changes, and builds a fresh Manager, Tree and store for every call. Two threads never share one of those, which is what keeps a facade thread-safe without a lock of its own; the store's own mutex serializes the writes.
Constant Summary collapse
- HOLDERS =
What a lock's holder may be tied to, for liveness: see #initialize.
%i[process session].freeze
- NAME =
What a session's name may be made of: see name!.
%r{\A[A-Za-z0-9._:@/+-]+\z}
Class Method Summary collapse
- .holder!(holder, named:) ⇒ Symbol
-
.name!(name) ⇒ String?
A holder id is pasted back into a shell, so it holds nothing a shell would read as more than a word.
-
.parent!(parent) ⇒ String, ...
trueis refused, not read as some parent: it names nobody.
Instance Method Summary collapse
-
#identity(agent_id: nil, parent_id: nil, session_name: nil) ⇒ Identity
Read fresh from the environment on every call, so it follows
AGENT_IDas that changes; a name from the call or the session overrides it, and the rest of the environment is kept so the session id and fingerprint still resolve. -
#initialize(backend, agent_id: nil, parent_id: nil, stale_minutes: nil, holder: nil, session_name: nil) ⇒ Session
constructor
rubocop:disable-next Metrics/ParameterLists.
-
#manager(dir: nil, agent_id: nil, parent_id: nil, stale_minutes: nil, session_name: nil) ⇒ Manager
Bound to that checkout, this backend and that identity.
Constructor Details
#initialize(backend, agent_id: nil, parent_id: nil, stale_minutes: nil, holder: nil, session_name: nil) ⇒ Session
rubocop:disable-next Metrics/ParameterLists
39 40 41 42 43 44 45 46 47 |
# File 'lib/agent/lock/facade/session.rb', line 39 def initialize(backend, agent_id: nil, parent_id: nil, stale_minutes: nil, holder: nil, session_name: nil) @holder = Session.holder!(holder, named: !session_name.nil?) @backend = backend @agent_id = agent_id @parent_id = Session.parent!(parent_id) @stale_minutes = stale_minutes @session_name = Session.name!(session_name) freeze end |
Class Method Details
.holder!(holder, named:) ⇒ Symbol
54 55 56 57 58 59 60 61 |
# File 'lib/agent/lock/facade/session.rb', line 54 def holder!(holder, named:) if holder && named raise ArgumentError, "holder: does not go with session_name:, which ties a lock to no process" end return holder || :process if holder.nil? || HOLDERS.include?(holder) raise ArgumentError, "holder must be one of #{HOLDERS.join(", ")}, got #{holder.inspect}" end |
.name!(name) ⇒ String?
A holder id is pasted back into a shell, so it holds nothing a shell would read as more than a word.
84 85 86 87 88 |
# File 'lib/agent/lock/facade/session.rb', line 84 def name!(name) return name if name.nil? || (name.is_a?(String) && name.match?(NAME)) raise ArgumentError, "session_name must be letters, digits and any of . _ : @ / + -, got #{name.inspect}" end |
.parent!(parent) ⇒ String, ...
true is refused, not read as some parent: it names nobody. So
is a blank String, which would otherwise reach the environment
as "no parent": parent_id: ENV["X"].to_s is a mistake, and
false is how none is said on purpose.
71 72 73 74 75 76 |
# File 'lib/agent/lock/facade/session.rb', line 71 def parent!(parent) return parent if parent.nil? || parent == false return parent if parent.is_a?(String) && !parent.strip.empty? raise ArgumentError, "parent_id must be a name, or false for none, got #{parent.inspect}" end |
Instance Method Details
#identity(agent_id: nil, parent_id: nil, session_name: nil) ⇒ Identity
Read fresh from the environment on every call, so it follows
AGENT_ID as that changes; a name from the call or the session
overrides it, and the rest of the environment is kept so the
session id and fingerprint still resolve. The pid is read per call
too, so a forked child holds its own locks, not its parent's.
A named session is tied to no process; holder: is for a facade
that names none.
119 120 121 122 123 124 125 |
# File 'lib/agent/lock/facade/session.rb', line 119 def identity(agent_id: nil, parent_id: nil, session_name: nil) name = Session.name!(session_name) || @session_name liveness = ::Process.pid if @holder == :process && name.nil? parent = Session.parent!(parent_id).nil? ? @parent_id : parent_id Identity.new(env: env_for(agent_id || @agent_id, parent), liveness_pid: liveness, session_name: name) end |
#manager(dir: nil, agent_id: nil, parent_id: nil, stale_minutes: nil, session_name: nil) ⇒ Manager
Returns bound to that checkout, this backend and that identity.
97 98 99 100 101 102 |
# File 'lib/agent/lock/facade/session.rb', line 97 def manager(dir: nil, agent_id: nil, parent_id: nil, stale_minutes: nil, session_name: nil) tree = Tree.for(dir || ".") Manager.new(tree: tree, store: @backend.store_for(tree), identity: identity(agent_id:, parent_id:, session_name:), stale_minutes: stale_minutes || @stale_minutes) end |