summaryrefslogtreecommitdiff
path: root/database.py
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2017-02-07 20:09:32 +0100
committerschneefux <schneefux+commit@schneefux.xyz>2017-02-07 20:10:56 +0100
commitda71062508f53d2674e1b9ea90764b7ba650c412 (patch)
tree2b7e5cc907a4a32cf38dbd9140c35524bf7378d5 /database.py
parentd72f18265cdb4a02324d7021b9a59175ecb9f513 (diff)
downloadapigrabber-da71062508f53d2674e1b9ea90764b7ba650c412.tar.gz
apigrabber-da71062508f53d2674e1b9ea90764b7ba650c412.zip
db connection params from env, fix early start
Diffstat (limited to 'database.py')
-rw-r--r--database.py15
1 files changed, 13 insertions, 2 deletions
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.