Module: Pgbus::CLI
- Defined in:
- lib/pgbus/cli.rb,
lib/pgbus/cli/dlq.rb
Defined Under Namespace
Modules: DLQ
Class Method Summary collapse
- .apply_capsule_filter(name) ⇒ Object
-
.apply_doctor_flag(options) ⇒ Object
--doctor runs the boot preflight in report mode; --doctor-strict aborts the boot on a fatal finding.
-
.apply_role_filter(options) ⇒ Object
Translates --workers-only / --scheduler-only / --dispatcher-only into the corresponding
Pgbus.configuration.rolesarray. -
.apply_start_options(args) ⇒ Object
Parses CLI flags for
pgbus startand applies them to the global configuration before the supervisor boots. - .list_queues ⇒ Object
- .parse_start_options(args) ⇒ Object
- .print_help ⇒ Object
-
.run_doctor ⇒ Object
Runs the deployment preflight diagnostics and prints the report.
- .show_status ⇒ Object
- .start(args) ⇒ Object
-
.start_mcp_server ⇒ Object
Boots the read-only MCP diagnostic server over stdio.
- .start_supervisor(args = []) ⇒ Object
Class Method Details
.apply_capsule_filter(name) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/pgbus/cli.rb', line 129 def apply_capsule_filter(name) capsule = Pgbus.configuration.capsule_named(name) unless capsule available = (Pgbus.configuration.workers || []).filter_map { |c| c[:name] } raise ArgumentError, "no capsule named #{name.inspect} (available: #{available.join(", ")})" end # Go through the public setter so any future normalization/validation # in workers= is applied consistently to the CLI override path too. Pgbus.configuration.workers = [capsule] end |
.apply_doctor_flag(options) ⇒ Object
--doctor runs the boot preflight in report mode; --doctor-strict aborts the boot on a fatal finding. Both set config.doctor_on_boot, which the supervisor reads at boot (issue #347). Strict wins if both are passed — it is the stronger gate, so a user asking for both clearly wants strict.
61 62 63 64 |
# File 'lib/pgbus/cli.rb', line 61 def apply_doctor_flag() Pgbus.configuration.doctor_on_boot = :report if [:doctor] Pgbus.configuration.doctor_on_boot = :strict if [:doctor_strict] end |
.apply_role_filter(options) ⇒ Object
Translates --workers-only / --scheduler-only / --dispatcher-only into
the corresponding Pgbus.configuration.roles array. Mutually exclusive —
passing more than one of the three flags raises ArgumentError.
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/pgbus/cli.rb', line 116 def apply_role_filter() role_flags = .slice(*ROLE_FLAG_TO_ROLE.keys).compact return if role_flags.empty? if role_flags.size > 1 raise ArgumentError, "--workers-only, --scheduler-only, and --dispatcher-only are mutually exclusive " \ "(got: #{role_flags.keys.map { |k| "--#{k.to_s.tr("_", "-")}" }.join(", ")})" end Pgbus.configuration.roles = [ROLE_FLAG_TO_ROLE.fetch(role_flags.keys.first)] end |
.apply_start_options(args) ⇒ Object
Parses CLI flags for pgbus start and applies them to the global
configuration before the supervisor boots. Designed to override the
initializer config without requiring a redeploy.
47 48 49 50 51 52 53 54 55 |
# File 'lib/pgbus/cli.rb', line 47 def (args) = (args) Pgbus.configuration.workers = [:queues] if [:queues] Pgbus.configuration.execution_mode = [:execution_mode].to_sym if [:execution_mode] apply_capsule_filter([:capsule]) if [:capsule] apply_role_filter() apply_doctor_flag() end |
.list_queues ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/pgbus/cli.rb', line 175 def list_queues Pgbus.client.list_queues metrics = Pgbus.client.metrics puts "QUEUE DEPTH VISIBLE OLDEST (s) TOTAL " puts "-" * 95 Array(metrics).each do |m| puts format("%-40s %-10s %-10s %-15s %-15s", m.queue_name, m.queue_length, m.queue_visible_length, m.oldest_msg_age_sec || "-", m.) end end |
.parse_start_options(args) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/pgbus/cli.rb', line 66 def (args) = {} OptionParser.new do |opts| opts. = "Usage: pgbus start [options]" opts.on("--queues STRING", "Override worker capsules (e.g. \"critical: 5; default: 10\")") do |v| [:queues] = v end opts.on("--capsule NAME", "Run only the named capsule from the configured workers") do |v| [:capsule] = v end opts.on("--workers-only", "Run only the worker processes (no scheduler/dispatcher/consumers/outbox)") do [:workers_only] = true end opts.on("--scheduler-only", "Run only the recurring scheduler (the cron pod pattern)") do [:scheduler_only] = true end opts.on("--dispatcher-only", "Run only the dispatcher (the maintenance pod pattern)") do [:dispatcher_only] = true end opts.on("--execution-mode MODE", "Execution mode: threads (default) or async") do |v| [:execution_mode] = v end opts.on("--doctor", "Run doctor preflight checks at boot and log the report (issue #347)") do [:doctor] = true end opts.on("--doctor-strict", "Run doctor preflight and refuse to boot on a fatal finding") do [:doctor_strict] = true end end.parse!(args.dup) end |
.print_help ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/pgbus/cli.rb', line 189 def print_help puts <<~HELP Usage: pgbus <command> [options] Commands: start Start the Pgbus supervisor (workers + dispatcher) status Show running Pgbus processes queues List queues with metrics dlq Inspect and drain dead-letter queues (list/show/retry/retry-all/purge) mcp Start the read-only MCP diagnostic server over stdio doctor Run environment diagnostics (config, DB, PGMQ, queues, LISTEN/NOTIFY, process liveness); exits 1 on any failure version Show version help Show this help Options for `start`: --queues STRING Override worker capsules from the CLI (e.g. "critical: 5; default: 10") --capsule NAME Run only the named capsule from the configured workers (useful for one-capsule-per-process deployments) --workers-only Run only worker processes (no scheduler/ dispatcher/consumers/outbox — for worker-only containers) --scheduler-only Run only the recurring scheduler (the cron pod pattern — exactly one of these per deployment) --dispatcher-only Run only the dispatcher (the maintenance pod pattern) --execution-mode Execution mode: threads (default) or async (fiber-based, lower connection usage) --doctor Run doctor preflight checks in the booting supervisor and log the report (one Rails boot instead of `pgbus doctor` + `pgbus start`) --doctor-strict Like --doctor, but refuse to boot (exit non-zero, fork nothing) if a fatal check fails (bad config or an absent PGMQ schema) Environment for `mcp`: PGBUS_MCP_TOKEN If set, PGBUS_MCP_AUTH_TOKEN must match for the server to start (optional token gate). PGBUS_MCP_ALLOW_PAYLOADS Truthy to let tools return raw message bodies/headers (off by default; redacted). HELP end |
.run_doctor ⇒ Object
Runs the deployment preflight diagnostics and prints the report. Exits 1 on any failed check so it can gate a deploy or CI run; exits 0 when all checks pass (warnings do not fail). Doctor itself never raises.
169 170 171 172 173 |
# File 'lib/pgbus/cli.rb', line 169 def run_doctor doctor = Pgbus::Doctor.new puts doctor.report exit 1 unless doctor.success? end |
.show_status ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/pgbus/cli.rb', line 142 def show_status processes = ProcessEntry.order(:kind, :created_at) .select(:kind, :hostname, :pid, :metadata, :last_heartbeat_at) if processes.none? puts "No Pgbus processes running." return end puts "KIND HOST PID HEARTBEAT METADATA" puts "-" * 100 processes.each do |p| puts format("%-12s %-20s %-8s %-30s %s", p.kind, p.hostname, p.pid, p.last_heartbeat_at, p.) end end |
.start(args) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pgbus/cli.rb', line 9 def start(args) command = args.first || "help" case command when "start" start_supervisor(args[1..] || []) when "status" show_status when "queues" list_queues when "dlq" DLQ.start(args[1..] || []) when "mcp" start_mcp_server when "doctor" run_doctor when "version" puts "pgbus #{Pgbus::VERSION}" when "help", "--help", "-h" print_help else puts "Unknown command: #{command}" print_help exit 1 end end |