Skip to content

Commit

Permalink
Added retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rofrano committed Jun 16, 2024
1 parent 9bcb73c commit cc8afdb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 1 addition & 2 deletions service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def create_app():
from service.common import error_handlers, cli_commands # noqa: F401, E402

try:
# models.init_db(app) # make our sqlalchemy tables
db.create_all()
models.init_db() # make our sqlalchemy tables
except Exception as error: # pylint: disable=broad-except
app.logger.critical("%s: Cannot continue", error)
# gunicorn requires exit code 4 to stop spawning workers when they die
Expand Down
12 changes: 12 additions & 0 deletions service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,29 @@
birthday (date) - the day the pet was born
"""
import os
import logging
from datetime import date
from enum import Enum
from retry import retry
from flask_sqlalchemy import SQLAlchemy

# global variables for retry (must be int)
RETRY_COUNT = int(os.environ.get("RETRY_COUNT", 5))
RETRY_DELAY = int(os.environ.get("RETRY_DELAY", 1))
RETRY_BACKOFF = int(os.environ.get("RETRY_BACKOFF", 2))

logger = logging.getLogger("flask.app")

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


@retry(Exception, delay=RETRY_DELAY, backoff=RETRY_BACKOFF, tries=RETRY_COUNT, logger=logger)
def init_db() -> None:
"""Initialize Tables"""
db.create_all()

class DataValidationError(Exception):
"""Used for an data validation errors when deserializing"""

Expand Down

0 comments on commit cc8afdb

Please sign in to comment.