Skip to content

Commit cc8afdb

Browse files
committed
Added retry logic
1 parent 9bcb73c commit cc8afdb

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

service/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ def create_app():
4545
from service.common import error_handlers, cli_commands # noqa: F401, E402
4646

4747
try:
48-
# models.init_db(app) # make our sqlalchemy tables
49-
db.create_all()
48+
models.init_db() # make our sqlalchemy tables
5049
except Exception as error: # pylint: disable=broad-except
5150
app.logger.critical("%s: Cannot continue", error)
5251
# gunicorn requires exit code 4 to stop spawning workers when they die

service/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,29 @@
3030
birthday (date) - the day the pet was born
3131
3232
"""
33+
import os
3334
import logging
3435
from datetime import date
3536
from enum import Enum
37+
from retry import retry
3638
from flask_sqlalchemy import SQLAlchemy
3739

40+
# global variables for retry (must be int)
41+
RETRY_COUNT = int(os.environ.get("RETRY_COUNT", 5))
42+
RETRY_DELAY = int(os.environ.get("RETRY_DELAY", 1))
43+
RETRY_BACKOFF = int(os.environ.get("RETRY_BACKOFF", 2))
44+
3845
logger = logging.getLogger("flask.app")
3946

4047
# Create the SQLAlchemy object to be initialized later in init_db()
4148
db = SQLAlchemy()
4249

4350

51+
@retry(Exception, delay=RETRY_DELAY, backoff=RETRY_BACKOFF, tries=RETRY_COUNT, logger=logger)
52+
def init_db() -> None:
53+
"""Initialize Tables"""
54+
db.create_all()
55+
4456
class DataValidationError(Exception):
4557
"""Used for an data validation errors when deserializing"""
4658

0 commit comments

Comments
 (0)