Last active
May 9, 2018 13:30
-
-
Save anddam/6a53e926df7e5e3a148bbec26816711b to your computer and use it in GitHub Desktop.
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
# coding: utf-8 | |
from datetime import datetime | |
from time import sleep | |
from sys import exit | |
from psycopg2 import connect, OperationalError | |
def wait_for_database(delay=4, retries=10, host='db', dbname='postgres', | |
user='postgres'): | |
for _ in range(retries): | |
try: | |
connection = connect(host=host, dbname=dbname, user=user) | |
except OperationalError: | |
print("Couldn't connect to PostgreSQL at <%s>, retrying in %d s." % | |
(datetime.now(), delay)) | |
sleep(delay) | |
else: | |
print("Succesfully connected to PostgreSQL.") | |
connection.close() | |
return 0 | |
print("Error: maximum number of retries (%d) reached." % retries) | |
return -1 | |
if __name__ == '__main__': | |
exit(wait_for_database()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment