From 5624dbaa9bff651041788f21989bd2910df64a72 Mon Sep 17 00:00:00 2001 From: Michael Fitz-Payne Date: Thu, 9 Mar 2023 14:03:51 +1000 Subject: [PATCH] FIX(cache_critical_dns): use DB port sourced from environment Fixes an assumption that the PostgreSQL port will always be reachable at the discovered host on the default port when performing the healthcheck. Instead we should be sourcing this from the same environment variable that the application will be using to connect to. Defaults to the standard PG port, 5432. --- script/cache_critical_dns | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/script/cache_critical_dns b/script/cache_critical_dns index d45bbf47ce3..803b0216e6f 100755 --- a/script/cache_critical_dns +++ b/script/cache_critical_dns @@ -107,6 +107,7 @@ CRITICAL_HOST_ENV_VARS = %w{ ) DEFAULT_DB_NAME = "discourse" +DEFAULT_DB_PORT = 5432 DEFAULT_REDIS_PORT = 6379 HOST_RESOLVER_CACHE = {} @@ -267,12 +268,13 @@ ensure client.close if client end -def postgres_healthcheck(host:, user:, password:, dbname:) +def postgres_healthcheck(host:, user:, password:, dbname:, port: DEFAULT_DB_PORT) client = PG::Connection.new( host: host, user: user, password: password, dbname: dbname, + port: port, connect_timeout: 2, # minimum ) client.exec(';').none? @@ -291,12 +293,14 @@ HEALTH_CHECKS = Hash.new( host: addr, user: ENV["DISCOURSE_DB_USERNAME"] || DEFAULT_DB_NAME, dbname: ENV["DISCOURSE_DB_NAME"] || DEFAULT_DB_NAME, + port: ENV["DISCOURSE_DB_PORT"] || DEFAULT_DB_PORT, password: ENV["DISCOURSE_DB_PASSWORD"])}, "DISCOURSE_DB_REPLICA_HOST": lambda { |addr| postgres_healthcheck( host: addr, user: ENV["DISCOURSE_DB_USERNAME"] || DEFAULT_DB_NAME, dbname: ENV["DISCOURSE_DB_NAME"] || DEFAULT_DB_NAME, + port: ENV["DISCOURSE_DB_REPLICA_PORT"] || DEFAULT_DB_PORT, password: ENV["DISCOURSE_DB_PASSWORD"])}, "DISCOURSE_REDIS_HOST": lambda { |addr| redis_healthcheck(