From a21dcec85d191dd6c25fc1e739c25f5383f8c980 Mon Sep 17 00:00:00 2001 From: coolbry95 Date: Fri, 2 Jul 2021 23:24:16 -0400 Subject: [PATCH] wait for database connection Signed-off-by: coolbry95 --- awx/__init__.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/awx/__init__.py b/awx/__init__.py index 35322396b3..9573c3529a 100644 --- a/awx/__init__.py +++ b/awx/__init__.py @@ -3,6 +3,7 @@ from __future__ import absolute_import, unicode_literals import os +import time import sys import warnings @@ -35,6 +36,7 @@ else: from django.db.models import indexes from django.db.backends.utils import names_digest from django.db import connection + from django.db.utils import OperationalError if HAS_DJANGO is True: @@ -152,6 +154,19 @@ def manage(): # enforce the postgres version is equal to 12. if not, then terminate program with exit code of 1 if not MODE == 'development': + # wait for connection to the database + sys.stdout.write("Wainting for connection to database\n") + connected = False + while not connected: + try: + connection.ensure_connection() + connected = True + except OperationalError: + sys.stdout.write("No connection available\n") + time.sleep(1) + + sys.stdout.write("Connection established\n") + if (connection.pg_version // 10000) < 12: sys.stderr.write("Postgres version 12 is required\n") sys.exit(1)