Class: Usps::Support::HealthController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/usps/support/health_controller.rb

Overview

Base controller for external synthetic health probes (see the infrastructure repo's doc/application_health_probes.md). Unlike Rails' /up (boot-only) and nginx's static /instance-health-check, this exercises an app's critical dependencies and returns the sentinel "HEALTHCHECK_OK " only when every check passes — a Route 53 HTTPS_STR_MATCH probe asserts the "HEALTHCHECK_OK" prefix, so a quiet app-level failure flips the Statuspage component even with no organic traffic.

Host apps subclass this and declare which dependencies to probe with a CHECKS constant — a list of symbols, each naming a #check_ method:

class HealthController < Usps::Support::HealthController
CHECKS = %i[database redis hq_database imis].freeze
end

and route to it (get 'health' => 'health#show'). Each check_* primitive returns a boolean or raises; the base times every check and treats a raise as a failed check, so the action always responds. Apps run only the checks they list — admin-console has no HQ connection, so it omits :hq_database; an iMIS-backed app adds :imis. For a bespoke dependency, define a #check_ in the subclass (e.g. wrapping #check_http(url)) and add to CHECKS.

Every probe's per-component results (ok, latency, error) are also handed to Usps::Support::HealthDiagnostics, which — when enabled — publishes them to CloudWatch so a past failure can be attributed to a specific component after it has recovered.

Inherits ActionController::Base directly — NOT the host's ApplicationController — to bypass Devise auth, Pundit authorization, PaperTrail, and especially allow_browser, whose modern-browser gate would 406 Route 53's non-browser prober and fail the probe for the wrong reason.

Constant Summary collapse

SENTINEL_PREFIX =

rubocop:disable Rails/ApplicationController

'HEALTHCHECK_OK'
CHECKS =

Symbols naming the dependency checks to run; each invokes the matching #check_. Override per app by defining a CHECKS constant. Default probes the primary database only, which presumably every app needs.

%i[database].freeze

Instance Method Summary collapse

Instance Method Details

#showObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/usps/support/health_controller.rb', line 42

def show
  results = checks
  healthy = results.values.all?

  body = "#{healthy ? "#{SENTINEL_PREFIX} #{app_name}" : "HEALTHCHECK_FAIL #{app_name}"}\n"
  results.each { |name, ok| body << "#{name}=#{ok ? 'ok' : 'fail'}\n" }

  response.set_header('Cache-Control', 'no-store')
  response.set_header('Retry-After', '30') unless healthy
  HealthDiagnostics.record(app: app_name, components: @measurements)
  render(plain: body, content_type: 'text/plain', status: healthy ? :ok : :service_unavailable)
end