Module: Agent::Lock::Store::BackendName

Defined in:
lib/agent/lock/store/backend_name.rb

Overview

A backend's name as somebody spelled it, read in one place so that nothing but "file" or "redis" can reach claim.

AGENT_LOCK_BACKEND used to reach the marker as typed, only downcased, and was checked after: bogus, filesystem or a variable exported empty bound the tree to a store that does not exist, and every later command read the name back and refused it.

Constant Summary collapse

VARIABLE =

The variable that names the backend when no caller does.

"AGENT_LOCK_BACKEND"

Class Method Summary collapse

Class Method Details

.canonical(name, from = nil) ⇒ String?

A blank name is no name at all. filesystem is file, as it is for --locking-strategy.

Parameters:

  • name (String, nil) —

    as the caller or the environment spelled it

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

    where it was read, said when it is refused

Returns:

  • (String, nil) —

    "file" or "redis"; nil when name is blank

Raises:

  • (Mismatch) —

    when it names anything else



28
29
30
31
32
33
34
35
# File 'lib/agent/lock/store/backend_name.rb', line 28

def canonical(name, from = nil)
  spelled = name.to_s.strip
  return if spelled.empty?

  Facade::Strategies.canonical(spelled.downcase)
rescue ArgumentError
  raise Mismatch, "unknown backend #{spelled.inspect}#{" in #{from}" if from}, expected file or redis"
end

.from_environment ⇒ String?

Returns what VARIABLE asks for; nil when it is unset or blank.

Returns:

  • (String, nil) —

    what VARIABLE asks for; nil when it is unset or blank

Raises:

  • (Mismatch) —

    when it names no backend



39
# File 'lib/agent/lock/store/backend_name.rb', line 39

def from_environment = canonical(ENV.fetch(VARIABLE, nil), VARIABLE)

.marked(name, path, offer: nil) ⇒ String?

What a tree's marker holds, as long as that is a backend. A marker an older alock wrote from a mistyped VARIABLE was refused by every command with the name and nothing more, though switch-backend rebinds such a tree: so that command is offered, with the file store unless the caller asked for redis, since file needs nothing to answer.

Parameters:

  • name (String, nil) —

    what the marker holds; nil for a virgin tree

  • path (String) —

    the marker, said when the name is refused

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

    "file" or "redis", the backend the caller asked for

Returns:

  • (String, nil) —

    name

Raises:

  • (Mismatch) —

    when it names no backend



53
54
55
56
57
58
# File 'lib/agent/lock/store/backend_name.rb', line 53

def marked(name, path, offer: nil)
  return name if name.nil? || BACKENDS.include?(name)

  raise Mismatch.new("unknown backend #{name.inspect} in #{path}, expected file or redis",
                     command: "switch-backend #{offer || "file"}")
end