-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #538 from mdellweg/rework_wait_for_db
Use django to check db connection
- Loading branch information
Showing
3 changed files
with
9 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,22 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import socket | ||
import sys | ||
import time | ||
from django.db import connection, utils | ||
|
||
if __name__ == "__main__": | ||
|
||
postgres_is_alive = False | ||
s4 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
s6 = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) | ||
tries = 0 | ||
print("Waiting on postgresql to start...") | ||
while not postgres_is_alive and tries < 100: | ||
tries += 1 | ||
pg_port = 5432 | ||
for dummy in range(100): | ||
try: | ||
env_port = os.environ.get("POSTGRES_SERVICE_PORT", "5432") | ||
pg_port = int(env_port) | ||
except ValueError: | ||
pass | ||
try: | ||
print("Checking postgres host %s" % os.environ["POSTGRES_SERVICE_HOST"]) | ||
print("Checking postgres port %s" % os.environ["POSTGRES_SERVICE_PORT"]) | ||
if tries % 2: | ||
s4.connect((os.environ["POSTGRES_SERVICE_HOST"], pg_port)) | ||
else: | ||
s6.connect((os.environ["POSTGRES_SERVICE_HOST"], pg_port)) | ||
except socket.error: | ||
connection.ensure_connection() | ||
break | ||
except utils.OperationalError: | ||
time.sleep(3) | ||
else: | ||
postgres_is_alive = True | ||
|
||
if postgres_is_alive: | ||
print("Postgres started!") | ||
sys.exit(0) | ||
else: | ||
print("Unable to reach postgres on port %s" % os.environ["POSTGRES_SERVICE_PORT"]) | ||
print("Unable to reach postgres.") | ||
sys.exit(1) | ||
|
||
print("Postgres started.") | ||
sys.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters