Skip to content

Commit

Permalink
db: Add retry on initial connect
Browse files Browse the repository at this point in the history
Creating the metadata is the very first action on startup and can fail
if the database is not (yet) available. Add our eponymous retry logic
there as well.

Also, increase the backoff base to a whopping two seconds because
database startup/restart is just a very slow operation.

Closes: scVENUS#177.
  • Loading branch information
michaelweiser committed Jun 24, 2021
1 parent 0f046c7 commit 0704e79
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions peekaboo/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,25 @@ def __init__(self, db_url, instance_id=0,
# attempt 3: 10 * 2**(3) == 40-80msecs
# attempt 4: 10 * 2**(4) == 80-160msecs
self.deadlock_backoff_base = 10
self.connect_backoff_base = 100
self.connect_backoff_base = 2000

Base.metadata.create_all(self.__engine)
with self.__lock:
attempt = 1
while attempt <= self.retries:
try:
Base.metadata.create_all(self.__engine)
except (OperationalError, DBAPIError,
SQLAlchemyError) as error:
attempt = self.was_transient_error(
error, attempt, 'create metadata')
if attempt > 0:
continue

raise PeekabooDatabaseError(
'Failed to create schema in database: %s' %
error)

break

def was_transient_error(self, error, attempt, action):
""" Decide if an exception signals a transient error condition and
Expand Down

0 comments on commit 0704e79

Please sign in to comment.