From dde606aabf510d7bac06ea1bddf5dc48afe26fcd Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sun, 24 May 2026 21:52:17 -0400 Subject: [PATCH] Expand error handling and fix runtime issues --- main.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index da45f08..f2ef8bf 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ from datetime import datetime from time import sleep from sqlalchemy import func -from sqlalchemy.exc import IntegrityError +from sqlalchemy.exc import IntegrityError, PendingRollbackError from data.database import dal, MegaMillionsDraw, PowerballDraw import lottery_predictor.collect as predictor @@ -45,6 +45,10 @@ def main(): session.commit() except IntegrityError: pass + except PendingRollbackError: + session.rollback() + finally: + session.close() # sleep between records and years to avoid querying the site too often sleep(60) @@ -64,6 +68,10 @@ def main(): session.commit() except IntegrityError: pass + except PendingRollbackError: + session.rollback() + finally: + session.close() # sleep between records and years to avoid querying the site too often sleep(60) @@ -82,6 +90,10 @@ def main(): session.commit() except IntegrityError: pass + except PendingRollbackError: + session.rollback() + finally: + session.close() # sleep between records and years to avoid querying the site too often sleep(60) @@ -89,7 +101,7 @@ def main(): session.commit() else: most_recent = session.query(func.max(PowerballDraw.draw_date)).first() - start_year = datetime.strptime(most_recent[0], str(date_format)).year + start_year = datetime.strptime(str(most_recent[0]), str(date_format)).year # loop over the years needed and gather records for year in range(start_year, end_year + 1): print(f"\tCollecting {year}...") @@ -102,11 +114,13 @@ def main(): session.commit() except IntegrityError: pass + except PendingRollbackError: + session.rollback() + finally: + session.close() # sleep between records and years to avoid querying the site too often sleep(60) - - mm_count = session.query(MegaMillionsDraw).count() pb_count = session.query(PowerballDraw).count()