Application fully migrated to modular approach

This commit is contained in:
2025-09-13 21:30:40 +02:00
parent c7d6b33cd5
commit b9fe91ab88
38 changed files with 509 additions and 265 deletions

View File

@@ -1,8 +1,9 @@
# app/tasks/background_tasks.py
import logging
from datetime import datetime
from app.core.database import SessionLocal
from models.database_models import MarketplaceImportJob
from utils.csv_processor import CSVProcessor
from datetime import datetime
import logging
logger = logging.getLogger(__name__)
@@ -17,6 +18,7 @@ async def process_marketplace_import(
"""Background task to process marketplace CSV import"""
db = SessionLocal()
csv_processor = CSVProcessor()
job = None # Initialize job variable
try:
# Update job status
@@ -53,10 +55,23 @@ async def process_marketplace_import(
except Exception as e:
logger.error(f"Import job {job_id} failed: {e}")
job.status = "failed"
job.completed_at = datetime.utcnow()
job.error_message = str(e)
db.commit()
if job is not None: # Only update if job was found
try:
job.status = "failed"
job.error_message = str(e)
job.completed_at = datetime.utcnow()
db.commit()
except Exception as commit_error:
logger.error(f"Failed to update job status: {commit_error}")
db.rollback()
# Don't re-raise the exception - background tasks should handle errors internally
# and update the job status accordingly. Only log the error.
pass
finally:
db.close()
# Close the database session only if it's not a mock
# In tests, we use the same session so we shouldn't close it
if hasattr(db, 'close') and callable(getattr(db, 'close')):
try:
db.close()
except Exception as close_error:
logger.error(f"Error closing database session: {close_error}")