Class: Agent::Lock::Facade::Backend

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/lock/facade/backend.rb

Overview

The one store a facade uses for its whole life, chosen once.

Strategies are tried in order: file is always usable, redis only when its URL answers. A list that runs out without either raises RedisUnavailableError.

Frozen once built. The ::Redis client it keeps is shared by every thread calling the facade; redis-rb serializes its own commands, so sharing it is safe.

Constant Summary collapse

PROBE_TIMEOUT =

Seconds to wait for Redis to answer the probe, so a dead URL costs a fraction of a second instead of the client's default timeout.

0.2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strategies:, redis_url:) ⇒ Backend

Returns a new instance of Backend.

Parameters:

  • strategies (Array<String, Symbol>) —
  • redis_url (String)

Raises:

  • (ArgumentError) —

    when a strategy name is unknown, the list is empty, or redis_url is tried and is not a Redis URL

  • (RedisUnavailableError) —

    when Redis was required and did not answer



38
39
40
41
42
# File 'lib/agent/lock/facade/backend.rb', line 38

def initialize(strategies:, redis_url:)
  @redis_url = redis_url
  @name, @client = resolve(Strategies.parse(strategies))
  freeze
end

Instance Attribute Details

#name ⇒ String (readonly)

Returns "redis" or "file".

Returns:

  • (String) —

    "redis" or "file"



28
29
30
# File 'lib/agent/lock/facade/backend.rb', line 28

def name
  @name
end

#redis_url ⇒ String (readonly)

Returns the Redis URL this backend was given.

Returns:

  • (String) —

    the Redis URL this backend was given



31
32
33
# File 'lib/agent/lock/facade/backend.rb', line 31

def redis_url
  @redis_url
end

Instance Method Details

#inspect ⇒ String

The default would print @redis_url as given, password and all, into whatever log or console inspects a facade.

Returns:

  • (String)


70
# File 'lib/agent/lock/facade/backend.rb', line 70

def inspect = "#<#{self.class.name} #{name} #{masked_url}>"

#store_for(tree) ⇒ Store::FileSystemStore, Store::RedisStore

Bound by the checkout's backend marker, exactly like alock: a virgin tree is claimed for this backend, so the CLI follows the facade there, and a tree already claimed for the other backend is refused. Silently using another store would let a facade and an alock in one checkout both "acquire" the same scope.

Parameters:

Returns:

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/agent/lock/facade/backend.rb', line 53

def store_for(tree)
  # Read first: a claimed tree, the usual case, costs no write.
  established = Store.recorded(tree) || Store.claim(tree, name)
  unless established == name
    raise Store::Mismatch, "this tree's locks live in #{established}, but this facade uses #{name}; " \
                           "build it with strategies: %w[#{established}]"
  end

  return Store::FileSystemStore.new(tree) if name == "file"

  Store::RedisStore.new(tree, client: @client, url: redis_url)
end