Class: Agent::Lock::Store::RedisIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/lock/store/redis_index.rb

Overview

Every lock in one Redis, across repositories. The file store has no equivalent: its records live inside one checkout's .git and cannot be listed from anywhere else.

Keys are the ones RedisStore writes, [alock-<version>-<slug>]-<sha256>:<id>. The mutex beside them is not a lock and is skipped. Anything still under the old agent-lock: prefix is counted, never read.

Defined Under Namespace

Classes: Lock, Repo

Constant Summary collapse

LEGACY_PREFIX =

What every key started with before the label named the repository. The old mutex was agent-lock-mutex:, so it does not match.

"agent-lock:"
LOCK_KEY =

A lock key. The slug is everything after alock-v<major>- up to the closing bracket, which for GitHub is owner/name.

/\A\[(?<label>alock-v\d+-(?<slug>[^\]]+))\]-(?<digest>[0-9a-f]{64}):(?<id>.+)\z/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ RedisIndex

Returns a new instance of RedisIndex.

Parameters:

  • client (::Redis)


46
47
48
# File 'lib/agent/lock/store/redis_index.rb', line 46

def initialize(client)
  @client = client
end

Class Method Details

.alive(record) ⇒ String

Whether the holder can be checked from this machine, and the answer.

A pid on another host cannot be asked. Reporting it alive is how a dead process on a build box used to look occupied forever.

Parameters:

Returns:

  • (String) —

    "yes", "no", or "unknown"



129
130
131
132
133
134
# File 'lib/agent/lock/store/redis_index.rb', line 129

def self.alive(record)
  here = record.host.nil? || record.host == Socket.gethostname
  return "unknown" unless here && record.pid

  record.alive? ? "yes" : "no"
end

.format_bytes(bytes) ⇒ String

Parameters:

  • bytes (Integer, nil)

Returns:

  • (String)


86
87
88
89
90
91
92
# File 'lib/agent/lock/store/redis_index.rb', line 86

def self.format_bytes(bytes)
  return "n/a" if bytes.nil?
  return "#{bytes} B" if bytes < 1024

  value, unit = bytes < (1024 * 1024) ? [bytes / 1024.0, "KB"] : [bytes / (1024.0 * 1024), "MB"]
  format("%<value>.1f %<unit>s", value:, unit:)
end

.normalize_slug(text) ⇒ String

github.com/kigster/laser-cutter and kigster/laser-cutter are the same repository. Any other forge keeps its host, the way Tree#redis_slug writes it: gitlab.com/kigster/laser-cutter. A bare name is refused; the slug is then matched whole, never as a prefix of some other one.

Parameters:

  • text (String)

Returns:

  • (String)

Raises:



64
65
66
67
68
69
70
# File 'lib/agent/lock/store/redis_index.rb', line 64

def self.normalize_slug(text)
  slug = text.to_s.strip.sub(%r{\Agithub\.com/}i, "").delete_suffix("/").delete_suffix(".git")
  return slug if slug.match?(%r{\A[^/]+(?:/[^/]+)+\z})

  raise Error, "pass the repository as owner/name, for example kigster/laser-cutter, " \
               "or host/owner/name for a forge other than GitHub"
end

.open(url = nil) ⇒ RedisIndex

Parameters:

Returns:

Raises:



53
# File 'lib/agent/lock/store/redis_index.rb', line 53

def self.open(url = nil) = new(RedisStore.open(url))

.slug_for(dir = Dir.pwd) ⇒ String

The repository show repo reads when none is named: the origin remote of the checkout dir is in.

Parameters:

  • dir (String) (defaults to: Dir.pwd) —

    a directory inside a checkout

Returns:

  • (String) —

    owner/name, or host/owner/name off GitHub

Raises:

  • (Error) —

    when dir is in no checkout with an origin remote



78
79
80
81
82
# File 'lib/agent/lock/store/redis_index.rb', line 78

def self.slug_for(dir = Dir.pwd)
  Tree.for(dir).origin_slug ||
    raise(Error, "no repository named, and #{dir} is not in a checkout with an origin remote. " \
                 "Pass it as owner/name")
end

Instance Method Details

#legacy_count ⇒ Integer

Returns keys an older gem wrote under LEGACY_PREFIX, which this version neither reads nor reaps.

Returns:

  • (Integer) —

    keys an older gem wrote under LEGACY_PREFIX, which this version neither reads nor reaps



120
# File 'lib/agent/lock/store/redis_index.rb', line 120

def legacy_count = client.scan_each(match: "#{LEGACY_PREFIX}*").to_a.uniq.size

#locks(slug) ⇒ Array<Lock>

Matched whatever case the name is typed in. A label is written lower-case (see Tree.normalize_remote), so Acme/App, which is how the forge shows the repository, used to find nothing. The same match still reaches a label 4.0.0 wrote in the remote's own case, which no store reads any more and show repos goes on listing.

Parameters:

  • slug (String) —

    owner/name, a GitHub URL path, or host/owner/name on another forge

Returns:

Raises:

  • (Error) —

    when slug is a bare name



110
111
112
113
114
115
116
# File 'lib/agent/lock/store/redis_index.rb', line 110

def locks(slug)
  wanted = self.class.normalize_slug(slug)
  pattern = /\Aalock-v\d+-#{Regexp.escape(wanted)}\z/i
  grouped.select { |label, _entries| label.match?(pattern) }
         .flat_map { |label, entries| entries.map { |entry| Lock.new(entry[:record], label) } }
         .sort_by { |lock| [lock.record.scope, lock.record.created_at.to_s] }
end

#repos ⇒ Array<Repo>

Returns every label, fewest locks last.

Returns:

  • (Array<Repo>) —

    every label, fewest locks last



95
96
97
98
# File 'lib/agent/lock/store/redis_index.rb', line 95

def repos
  grouped.map { |label, entries| Repo.new(label, entries.size, bytes_of(entries)) }
         .sort_by { |repo| [-repo.count, repo.namespace] }
end