Files
ubicloud/prog/heartbeat.rb
Daniel Farina e5bf93bb6f Add missing service logging to heartbeat
This comes at some complication, but has been suggested as useful to
log, since heartbeat failures are opaque.  To look in the logs is not
particularly easy to discover, but then again, the number of
ditch-effort heartbeat failure alerts is rare these days.
2024-03-29 11:15:53 -07:00

35 lines
909 B
Ruby

# frozen_string_literal: true
require "excon"
class Prog::Heartbeat < Prog::Base
CONNECTED_APPLICATION_QUERY = <<SQL
SELECT
(regexp_matches(application_name, '/(puma|monitor|respirate)$'))[1]
FROM
(SELECT DISTINCT application_name FROM pg_stat_activity) AS psa
ORDER BY 1
SQL
EXPECTED = %w[monitor puma respirate].freeze
def fetch_connected
DB[CONNECTED_APPLICATION_QUERY].flat_map(&:values).freeze
end
label def wait
if (connected = fetch_connected) != EXPECTED
Clog.emit("some expected connected clover services are missing") {
{heartbeat_missing: {difference: EXPECTED.difference(connected)}}
}
nap 10
end
Excon.get(Config.heartbeat_url, read_timeout: 2, write_timeout: 2, connect_timeout: 2)
nap 10
rescue Excon::Error::Timeout => e
Clog.emit("heartbeat request timed out") { {exception: {message: e.message}} }
nap 10
end
end