Skip to content

Commit

Permalink
fix: add some extra try except around db conns #42
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Mar 24, 2024
1 parent ee4b1c6 commit b2f4fa0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 9 additions & 2 deletions icon_governance/db.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import sys

from loguru import logger
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlmodel import SQLModel, create_engine
from psycopg2 import OperationalError

from icon_governance.config import settings
from icon_governance.models.preps import Prep
Expand Down Expand Up @@ -35,5 +38,9 @@ async def get_session() -> AsyncSession:
yield session


engine = create_engine(SQLALCHEMY_DATABASE_URL)
session_factory = sessionmaker(bind=engine)
try:
engine = create_engine(SQLALCHEMY_DATABASE_URL)
session_factory = sessionmaker(bind=engine)
except Exception as e:
logger.error(f"Database connection dropped: {str(e)}")
raise
9 changes: 7 additions & 2 deletions icon_governance/main_cron.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from typing import Callable, TypedDict

from apscheduler.schedulers.background import BlockingScheduler
Expand Down Expand Up @@ -105,8 +106,12 @@ class Cron(TypedDict):


def run_cron_with_session(cron: Callable):
with session_factory() as session:
cron(session=session)
try:
with session_factory() as session:
cron(session=session)
except Exception as e:
logger.error(f"Database operation failed: {str(e)}")
sys.exit(1)


def main():
Expand Down

0 comments on commit b2f4fa0

Please sign in to comment.