From 0553788d3bfa053b628138fecf9fa506501151e3 Mon Sep 17 00:00:00 2001 From: Michael Fitz-Payne Date: Mon, 23 May 2022 13:41:15 +1000 Subject: [PATCH] DEV(cache_critical_dns): improve postgres_healthcheck The `PG::Connection#ping` method is only reliable for checking if the given host is accepting connections, and not if the authentication details are valid. This extends the healthcheck to confirm that the auth details are able to both create a connection and execute queries against the database. We expect the empty query to return an empty result set, so we can assert on that. If a failure occurs for any reason, the healthcheck will return false. --- script/cache_critical_dns | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/script/cache_critical_dns b/script/cache_critical_dns index a4350aed5c3..f63763e5894 100755 --- a/script/cache_critical_dns +++ b/script/cache_critical_dns @@ -158,16 +158,18 @@ ensure end def postgres_healthcheck(host:, user:, password:, dbname:) - response = PG::Connection.ping( + client = PG::Connection.new( host: host, user: user, password: password, dbname: dbname, connect_timeout: 2, # minimum ) - response == PG::Constants::PQPING_OK + client.exec(';').none? rescue false +ensure + client.close if client end HEALTH_CHECKS = {