Class: Agent::Lock::Facade::Session

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(backend, agent_id: nil, parent_id: nil, stale_minutes: nil, holder: nil, session_name: nil) ⇒ Session

rubocop:disable-next Metrics/ParameterLists

Parameters:

  • backend (Backend)
  • agent_id (String, nil) (defaults to: nil) —

    the holder name when a call does not pass one

  • parent_id (String, false, nil) (defaults to: nil) —

    the parent when a call does not pass one; false says there is none, where nil infers it

  • stale_minutes (Integer, nil) (defaults to: nil) —

    nil reads AGENT_LOCK_STALE_MINUTES

  • holder (Symbol, nil) (defaults to: nil) —

    :process, the default, ties a lock to this Ruby process, so a host that crashes is reaped like a dead alock session; :session ties it to the first non-shell ancestor, as alock does, for a short script whose lock should outlive it

  • session_name (String, nil) (defaults to: nil) —

    the session's name, in place of CLAUDE_SESSION_ID and the fingerprint: a plan id, a job id. Its locks are tied to no process, so they survive any restart of the host and lapse only when untouched for stale_minutes

Raises:

  • (ArgumentError) —

    for any other holder, for holder: given beside session_name:, which ties a lock to no process at all, and for a name that is not one



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

Parameters:

  • holder (Symbol, nil) —

    nil is :process

  • named (Boolean) —

    whether the facade names its session

Returns:

  • (Symbol)

Raises:

  • (ArgumentError)


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.

Parameters:

  • name (String, nil)

Returns:

  • (String, nil)

Raises:

  • (ArgumentError) —

    for a name that is not NAME



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.

Parameters:

  • parent (String, false, nil)

Returns:

  • (String, false, nil)

Raises:

  • (ArgumentError) —

    for anything else



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.

Parameters:

  • agent_id (String, nil) (defaults to: nil)
  • parent_id (String, false, nil) (defaults to: nil) —

    false for a holder with no parent; nil uses the session's default, and without one infers it

  • session_name (String, nil) (defaults to: nil) —

    overrides the session's default

Returns:

Raises:

  • (ArgumentError) —

    for a name or a parent that is not one



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.

Parameters:

  • dir (String, nil) (defaults to: nil) —

    a directory inside the checkout; nil is the current one

  • agent_id (String, nil) (defaults to: nil) —

    overrides the session's default

  • parent_id (String, false, nil) (defaults to: nil) —

    overrides the session's default

  • stale_minutes (Integer, nil) (defaults to: nil) —

    overrides the session's default

  • session_name (String, nil) (defaults to: nil) —

    overrides the session's default

Returns:

  • (Manager) —

    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