Checking onboardin flow - fix 3

This commit is contained in:
Urtzi Alfaro
2025-07-27 11:04:32 +02:00
parent 30ac945058
commit 0b14cf9eb2
3 changed files with 92 additions and 12 deletions

View File

@@ -83,13 +83,11 @@ async def start_training_job(
except Exception as e:
logger.warning("Failed to publish job started event", error=str(e))
# Start training in background
background_tasks.add_task(
training_service.execute_training_job,
db, # Pass the database session
job.job_id,
job.tenant_id,
request # Pass the request object
training_service.execute_training_job_simple,
new_job_id,
tenant_id_str,
request
)
logger.info("Training job created",

View File

@@ -31,6 +31,36 @@ class TrainingService:
def __init__(self):
self.ml_trainer = BakeryMLTrainer()
async def execute_training_job_simple(self, job_id: str, tenant_id_str: str, request: TrainingJobRequest):
"""Simple wrapper that creates its own database session"""
try:
# Import database_manager locally to avoid circular imports
from app.core.database import database_manager
logger.info(f"Starting background training job {job_id} for tenant {tenant_id_str}")
# Create new session for background task
async with database_manager.async_session_local() as session:
await self.execute_training_job(session, job_id, tenant_id_str, request)
await session.commit()
except Exception as e:
logger.error(f"Background training job {job_id} failed: {str(e)}")
# Try to update job status to failed
try:
from app.core.database import database_manager
async with database_manager.async_session_local() as error_session:
await self._update_job_status(
error_session, job_id, "failed", 0,
f"Training failed: {str(e)}", error_message=str(e)
)
await error_session.commit()
except Exception as update_error:
logger.error(f"Failed to update job status: {str(update_error)}")
raise
async def create_training_job(self,
db: AsyncSession,
@@ -425,7 +455,7 @@ class TrainingService:
params["limit"] = limit
response = await client.get(
f"{settings.DATA_SERVICE_URL}/api/v1/tenants/{tenant_id}/sales/",
f"{settings.DATA_SERVICE_URL}/api/v1/tenants/{tenant_id}/sales",
params=params,
headers=headers,
timeout=30.0
@@ -516,7 +546,7 @@ class TrainingService:
params["end_date"] = request.end_date.isoformat()
response = await client.get(
f"{settings.DATA_SERVICE_URL}/tenants/{tenant_id}/traffic/historical",
f"http://gateway:8000/tenants/{tenant_id}/traffic/historical",
params=params,
timeout=30.0
)