usps-support 0.2.45 → 0.2.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f333ae8e6b1ebd37bd6ec0aabc4a07db42777ffd2031f4c7b119fb7f71e22392
4
- data.tar.gz: 82e6f7a84a0a8eaa2557ece29630965a896f8ce20502bad455bbe06821d5e78d
3
+ metadata.gz: a1508e9abf6ef884c05e7201b52f3c328a81be47388319938e5a4b3ef8aa5e5a
4
+ data.tar.gz: '08de17c41c0489f4f0a6994a73ca193a25c8b1917ebc652c6788d6d6c69c72d2'
5
5
  SHA512:
6
- metadata.gz: 72e0c952b68a7a62e99f61b3815568aba3453c35b28c69ee15a17acc2882df635ddccca0df5e60290bbbeda36b3c3b3205fec16f93f8d211941d4d98d9b66981
7
- data.tar.gz: 95defc9a3bf120dba61375e6929f01b0affcfc8ad532ed79e200773fbeb70b9080723bda995097b767b8a1922424c5ecdb9ebdef0257c4fc8368d6f6515a9d97
6
+ metadata.gz: 54e24451d92bed2cfb93bda006c78d2d311f9e3e5a1a283cebc7f66e3908c4c5977b81a87ce265a1389e16ef6b8759759ca0e7ac5f304ff35a682bb29e2d9a12
7
+ data.tar.gz: cd741d2ff564082916e9782408762d8117b17d45afae4e8d90e3ddb442c0d1e79e84b3e5beb492387d7f971b5ee15f7db679a9c8e2d2c6753eecfc5adc87ea55
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+
5
+ module Usps::Support
6
+ # Base controller for external synthetic health probes (see the infrastructure repo's
7
+ # doc/application_health_probes.md). Unlike Rails' /up (boot-only) and nginx's static
8
+ # /instance-health-check, this exercises an app's *critical dependencies* and returns the sentinel
9
+ # "HEALTHCHECK_OK <app>" only when every check passes — a Route 53 HTTPS_STR_MATCH probe asserts
10
+ # the "HEALTHCHECK_OK" prefix, so a quiet app-level failure flips the Statuspage component even
11
+ # with no organic traffic.
12
+ #
13
+ # Host apps subclass this and override #checks to pick which dependencies to probe, e.g.
14
+ #
15
+ # class HealthController < Usps::Support::HealthController
16
+ # private
17
+ # def checks = { database: check_database, redis: check_redis }
18
+ # end
19
+ #
20
+ # and route to it (`get 'health' => 'health#show'`). The check_* primitives below each return a
21
+ # boolean and never raise, so the action always responds. Apps run only the checks for the
22
+ # dependencies they actually configure — admin-console has no HQ connection, so it omits
23
+ # check_hq_database; an iMIS-backed app adds check_imis; a custom downstream is check_http(url).
24
+ #
25
+ # Inherits ActionController::Base directly — NOT the host's ApplicationController — to bypass
26
+ # Devise auth, Pundit authorization, PaperTrail, and especially `allow_browser`, whose
27
+ # modern-browser gate would 406 Route 53's non-browser prober and fail the probe for the wrong
28
+ # reason.
29
+ class HealthController < ActionController::Base # rubocop:disable Rails/ApplicationController
30
+ SENTINEL_PREFIX = 'HEALTHCHECK_OK'
31
+
32
+ def show
33
+ results = checks
34
+ healthy = results.values.all?
35
+
36
+ body = "#{healthy ? "#{SENTINEL_PREFIX} #{app_name}" : "HEALTHCHECK_FAIL #{app_name}"}\n"
37
+ results.each { |name, ok| body << "#{name}=#{ok ? 'ok' : 'fail'}\n" }
38
+
39
+ response.set_header('Cache-Control', 'no-store')
40
+ response.set_header('Retry-After', '30') unless healthy
41
+ render(plain: body, content_type: 'text/plain', status: healthy ? :ok : :service_unavailable)
42
+ end
43
+
44
+ private
45
+
46
+ # Dependencies to probe, as { label => boolean }.
47
+ #
48
+ # Override per app.
49
+ #
50
+ # Default is the primary database only, which presumably every app needs.
51
+ def checks = { database: check_database }
52
+
53
+ # Derives "admin-console" from the host application's module (e.g. AdminConsole), so the sentinel
54
+ # identifies which app answered without per-app configuration.
55
+ def app_name = Rails.application.class.module_parent_name.underscore.dasherize
56
+
57
+ # --- Check primitives. Each returns true/false and swallows its own errors, so a dead
58
+ # dependency reads as a failed check rather than a 500. ---
59
+
60
+ # Primary application database (RDS).
61
+ def check_database = check_connection(::ActiveRecord::Base)
62
+
63
+ # HQ database (using VHQAB), reached over the HQ VPN — a *different* host than RDS.
64
+ def check_hq_database = check_connection(Models::Hq::Members::BaseRecord)
65
+
66
+ # Redis / Sidekiq's configured connection pool.
67
+ def check_redis
68
+ ::Sidekiq.redis { it.call('PING') }
69
+ true
70
+ rescue StandardError
71
+ false
72
+ end
73
+
74
+ # iMIS API liveness — authenticating proves network + credentials + the token endpoint, against
75
+ # the environment-appropriate iMIS.
76
+ def check_imis
77
+ ::Usps::Imis::Api.new.auth_token
78
+ true
79
+ rescue StandardError
80
+ false
81
+ end
82
+
83
+ # Generic downstream HTTP liveness for a custom dependency.
84
+ def check_http(url)
85
+ ::Net::HTTP.get_response(URI(url)).is_a?(::Net::HTTPSuccess)
86
+ rescue StandardError
87
+ false
88
+ end
89
+
90
+ # Checks out a connection from the given model's pool and runs a trivial query — proves the
91
+ # connection is live, which a boot-time check skips.
92
+ def check_connection(model)
93
+ model.connection.execute('SELECT 1')
94
+ true
95
+ rescue StandardError
96
+ false
97
+ end
98
+ end
99
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Support
5
- VERSION = '0.2.45'
5
+ VERSION = '0.2.46'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usps-support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.45
4
+ version: 0.2.46
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -73,6 +73,7 @@ files:
73
73
  - Readme.md
74
74
  - app/controllers/concerns/usps/support/admin_menu.rb
75
75
  - app/controllers/usps/support/admins_controller.rb
76
+ - app/controllers/usps/support/health_controller.rb
76
77
  - app/models/usps/support/policy/admin_context.rb
77
78
  - config/routes.rb
78
79
  - db/hq/members_schema.rb