-
-
Save altendky/7c768d321492258ae883e775a9e4e23f 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 psycopg2 import connect, OperationalError | |
connected = False | |
while not connected: | |
try: | |
connection = connect(host='db', dbname='postgres', user='postgres') | |
except OperationalError: | |
print("Couldn't connect at %s, waiting 4 s ." % datetime.now()) | |
sleep(4) | |
else: | |
connected = True | |
connection.close() | |
def can_connect(retries=10): | |
delay = 1 | |
for _ in range(retries): | |
try: | |
with connect(host='db', dbname='postgres', user='postgres') as connection: | |
# test the connection | |
connection.close() | |
return True | |
except OperationalError: | |
print("Couldn't connect at %s, waiting 4 s ." % datetime.now()) | |
sleep(delay) | |
delay = min(delay * 2, 60) | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment