Class: Pgbus::Doctor

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

Overview

Preflight diagnostics for a pgbus deployment — the single command that answers "is this environment healthy enough to run?". Runs eight checks and returns a machine-readable result plus a human report, so pgbus doctor and rake pgbus:doctor can gate a deploy or CI run (exit 0 on success, 1 on any failure).

Doctor never touches PGMQ or PostgreSQL directly: every probe goes through Pgbus::Client (DB, PGMQ schema, queues, NOTIFY) or Pgbus::Web::DataSource (process liveness, via Pgbus::MCP::HealthAnalyzer). It also never raises — a broken environment turns into :fail results, never a crash — so it is safe to run against a database that is down or a half-installed schema.

Defined Under Namespace

Classes: Check

Constant Summary collapse

STATUS_ICON =
{ ok: "", warn: "!", fail: "" }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config: Pgbus.configuration, client: Pgbus.client, data_source: nil) ⇒ Doctor

Returns a new instance of Doctor.



28
29
30
31
32
# File 'lib/pgbus/doctor.rb', line 28

def initialize(config: Pgbus.configuration, client: Pgbus.client, data_source: nil)
  @config = config
  @client = client
  @data_source = data_source || Pgbus::Web::DataSource.new(client: client)
end

Instance Method Details

#config_summaryObject

Resolved-config summary for the report. Password-bearing fields (database_url, connection_params) are redacted.



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pgbus/doctor.rb', line 72

def config_summary
  {
    queue_prefix: @config.queue_prefix,
    default_queue: @config.default_queue,
    pgmq_schema_mode: @config.pgmq_schema_mode,
    resolved_pool_size: safe { @config.resolved_pool_size },
    listen_notify: @config.listen_notify,
    roles: @config.roles || "all",
    capsules: capsule_summary,
    database_url: redacted_database_url,
    connection_params: redacted_connection_params
  }.compact
end

#reportObject

Human-readable report: one line per check, then a resolved-config summary with passwords redacted. Suitable for stdout in the CLI and rake task.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pgbus/doctor.rb', line 57

def report
  lines = ["Pgbus Doctor", "=" * 40]
  run.each do |check|
    icon = STATUS_ICON.fetch(check[:status], "?")
    lines << format("%<icon>s %<name>-22s %<detail>s", icon: icon, name: check[:name], detail: check[:detail])
  end
  lines << ""
  lines << "Configuration"
  lines << ("-" * 40)
  config_summary.each { |key, value| lines << format("  %<key>-20s %<value>s", key: key, value: value) }
  lines.join("\n")
end

#runObject

Run all checks and return an array of result hashes:

{ name:, status: :ok|:warn|:fail, detail: }


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pgbus/doctor.rb', line 36

def run
  @run ||= [
    check_configuration,
    check_database,
    check_pgmq_schema,
    check_queues,
    check_notify,
    check_processes,
    check_allowed_global_id_models,
    check_broadcast_queue
  ].map(&:to_h)
end

#success?Boolean

True when no check failed. Warnings do not fail the run — they surface a concern (e.g. an outdated PGMQ schema) without blocking a deploy.

Returns:

  • (Boolean)


51
52
53
# File 'lib/pgbus/doctor.rb', line 51

def success?
  run.none? { |c| c[:status] == :fail }
end