Fix critical double commit bug and Spanish holidays warning

CRITICAL FIX - Database Transaction:
- Removed duplicate commit logic from _store_in_db() inner function (lines 805-811)
- Prevents 'Method commit() can't be called here' error
- Now only outer scope handles commits (line 821 for new sessions, parent for parent sessions)
- Fixes issue where all 5 models trained successfully but failed to store in DB

MINOR FIX - Logging:
- Fixed Spanish holidays logger call (line 977-978)
- Removed invalid keyword arguments (region=, years=) from logger.info()
- Now uses f-string format consistent with rest of codebase
- Prevents 'Logger._log() got an unexpected keyword argument' warning

Impact:
- Training pipeline can now complete successfully
- Models will be stored in database after training
- No more cascading transaction failures
- Cleaner logs without warnings

Root cause: Double commit introduced during recent session management fixes
Related commits: 673108e, 74215d3, fd0a96e, b2de56e, e585e9f
This commit is contained in:
Claude
2025-11-05 18:34:00 +00:00
parent 0b129b070d
commit 0c4a941ca8

View File

@@ -802,13 +802,10 @@ class BakeryProphetManager:
db_session.add(db_model) db_session.add(db_model)
# Only commit if using a new session, not if using parent session # ✅ FIX: Don't commit here - let the outer scope handle commits
# Parent session commits will be handled by the calling method to prevent conflicts # This prevents double commit when using a new dedicated session
if not use_parent_session: # Parent sessions will commit when parent completes; new sessions commit in outer scope
await db_session.commit() logger.debug(f"Added model {model_id} to session (commit handled by caller)")
logger.info(f"Model {model_id} stored in database successfully")
else:
logger.debug(f"Added model {model_id} to parent session (commit deferred)")
try: try:
# ✅ FIX: Use parent session if provided, otherwise create new one # ✅ FIX: Use parent session if provided, otherwise create new one
@@ -977,9 +974,8 @@ class BakeryProphetManager:
holidays_df = holidays_df.drop_duplicates(subset=['ds', 'holiday']) holidays_df = holidays_df.drop_duplicates(subset=['ds', 'holiday'])
holidays_df = holidays_df.sort_values('ds').reset_index(drop=True) holidays_df = holidays_df.sort_values('ds').reset_index(drop=True)
logger.info(f"Loaded {len(holidays_df)} Spanish holidays dynamically", # ✅ FIX: Don't pass keyword args to logger - use f-string instead
region=region or 'National', logger.info(f"Loaded {len(holidays_df)} Spanish holidays dynamically (region={region or 'National'}, years={min(years)}-{max(years)})")
years=f"{min(years)}-{max(years)}")
return holidays_df return holidays_df
else: else: