Skip to content

Commit

Permalink
lifecycle: refresh PostgreSQL connection params on startup while test…
Browse files Browse the repository at this point in the history
…ing connection (goauthentik#10996)

Signed-off-by: Jens Langhammer <[email protected]>
  • Loading branch information
BeryJu authored Aug 20, 2024
1 parent d54718c commit 5d33f3c
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lifecycle/wait_for_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@

from authentik.lib.config import CONFIG, redis_url

CHECK_THRESHOLD = 30


def check_postgres():
attempt = 0
while True:
if attempt >= CHECK_THRESHOLD:
sysexit(1)
try:
conn = connect(
dbname=CONFIG.get("postgresql.name"),
user=CONFIG.get("postgresql.user"),
password=CONFIG.get("postgresql.password"),
host=CONFIG.get("postgresql.host"),
dbname=CONFIG.refresh("postgresql.name"),
user=CONFIG.refresh("postgresql.user"),
password=CONFIG.refresh("postgresql.password"),
host=CONFIG.refresh("postgresql.host"),
port=CONFIG.get_int("postgresql.port"),
sslmode=CONFIG.get("postgresql.sslmode"),
sslrootcert=CONFIG.get("postgresql.sslrootcert"),
Expand All @@ -30,19 +35,26 @@ def check_postgres():
except OperationalError as exc:
sleep(1)
CONFIG.log("info", f"PostgreSQL connection failed, retrying... ({exc})")
finally:
attempt += 1
CONFIG.log("info", "PostgreSQL connection successful")


def check_redis():
url = CONFIG.get("cache.url") or redis_url(CONFIG.get("redis.db"))
attempt = 0
while True:
if attempt >= CHECK_THRESHOLD:
sysexit(1)
try:
redis = Redis.from_url(url)
redis.ping()
break
except RedisError as exc:
sleep(1)
CONFIG.log("info", f"Redis Connection failed, retrying... ({exc})")
finally:
attempt += 1
CONFIG.log("info", "Redis Connection successful")


Expand Down

0 comments on commit 5d33f3c

Please sign in to comment.