Class: Pgbus::HealthProbe

Inherits:
Object
  • Object
show all
Defined in:
lib/pgbus/health_probe.rb

Overview

Dependency-free readiness probe for container HEALTHCHECKs (issue #386).

exe/pgbus-health loads this file via require_relative and nothing else: a docker HEALTHCHECK runs the probe every few seconds, so it must never drag in Bundler, Zeitwerk, Rails, or the rest of the gem. Only Ruby's bundled socket library is allowed here.

healthcheck:
cmd: bin/pgbus-health            # port from PGBUS_HEALTH_PORT
cmd: bin/pgbus-health --port 9394 --path /livez

Exit codes: 0 healthy (HTTP 2xx), 1 unhealthy (non-2xx, refused, timeout), 2 usage error (no/invalid port).

Constant Summary collapse

EXIT_OK =
0
EXIT_UNHEALTHY =
1
EXIT_USAGE =
2
DEFAULT_PATH =
"/readyz"
DEFAULT_TIMEOUT =
2.0
HOST =
"127.0.0.1"
USAGE =
"usage: pgbus-health [--port PORT] [--path PATH] [--timeout SECONDS]\n       " \
"port falls back to the PGBUS_HEALTH_PORT environment variable\n"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, env: ENV, out: $stdout, err: $stderr) ⇒ HealthProbe

Returns a new instance of HealthProbe.



35
36
37
38
39
40
41
42
43
# File 'lib/pgbus/health_probe.rb', line 35

def initialize(argv, env: ENV, out: $stdout, err: $stderr)
  @out = out
  @err = err
  @path = DEFAULT_PATH
  @timeout = DEFAULT_TIMEOUT
  @port = env["PGBUS_HEALTH_PORT"]
  @usage_error = false
  parse(argv)
end

Class Method Details

.run(argv, env: ENV, out: $stdout, err: $stderr) ⇒ Object



31
32
33
# File 'lib/pgbus/health_probe.rb', line 31

def self.run(argv, env: ENV, out: $stdout, err: $stderr)
  new(argv, env: env, out: out, err: err).run
end

Instance Method Details

#runObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/pgbus/health_probe.rb', line 45

def run
  return usage_failure if @usage_error

  port = Integer(@port, exception: false)
  # Out-of-range ports would reach Socket.tcp and raise SocketError — a
  # backtrace where a HEALTHCHECK needs a deterministic exit code.
  return usage_failure unless port&.between?(1, 65_535)

  probe(port)
end