From da71062508f53d2674e1b9ea90764b7ba650c412 Mon Sep 17 00:00:00 2001 From: schneefux Date: Tue, 7 Feb 2017 20:09:32 +0100 Subject: db connection params from env, fix early start --- database.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'database.py') diff --git a/database.py b/database.py index 0679a52..45467f6 100644 --- a/database.py +++ b/database.py @@ -10,13 +10,24 @@ class Database(object): def __init__(self): self._pool = None - async def connect(self, connstring): + async def connect(self, host, port, user, password, database): """Connects to the database. :param connstring: Connection string containing user and database. :type connstring: str """ - self._pool = await asyncpg.create_pool(connstring) + while True: # retry until connection succeeds + try: + print("attempting to connect to db…") + self._pool = await asyncpg.create_pool( + host=host, port=port, user=user, + password=password, database=database) + break + except asyncpg.exceptions.CannotConnectNowError: + await self._pool.close() + print("Database is not ready yet. Retrying…") + await asyncio.sleep(5) + async def upsert_type(self, obj, objtype, many=False): """Upserts an object of given `objtype` into the corresponding database. -- cgit v1.3.1