Skip to content

Commit

Permalink
FIX(cache_critical_dns): use DB port sourced from environment
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fitzy101 committed Mar 10, 2023
1 parent 0603bd5 commit 5624dba
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion script/cache_critical_dns
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ CRITICAL_HOST_ENV_VARS = %w{
)

DEFAULT_DB_NAME = "discourse"
DEFAULT_DB_PORT = 5432
DEFAULT_REDIS_PORT = 6379

HOST_RESOLVER_CACHE = {}
Expand Down Expand Up @@ -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?
Expand All @@ -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(
Expand Down

0 comments on commit 5624dba

Please sign in to comment.