New alert service

This commit is contained in:
Urtzi Alfaro
2025-12-05 20:07:01 +01:00
parent 1fe3a73549
commit 667e6e0404
393 changed files with 26002 additions and 61033 deletions

View File

@@ -135,26 +135,45 @@ class TrainingTenantDeletionService(BaseTenantDataDeletionService):
result = TenantDataDeletionResult(tenant_id=tenant_id, service_name=self.service_name)
try:
import os
# Step 1: Delete model artifacts (references models)
logger.info("training.tenant_deletion.deleting_artifacts", tenant_id=tenant_id)
# TODO: Delete physical files from storage before deleting DB records
# artifacts = await self.db.execute(
# select(ModelArtifact).where(ModelArtifact.tenant_id == tenant_id)
# )
# for artifact in artifacts.scalars():
# try:
# os.remove(artifact.file_path) # Delete physical file
# except Exception as e:
# logger.warning("Failed to delete artifact file",
# path=artifact.file_path, error=str(e))
# Delete physical files from storage before deleting DB records
artifacts = await self.db.execute(
select(ModelArtifact).where(ModelArtifact.tenant_id == tenant_id)
)
deleted_files = 0
failed_files = 0
for artifact in artifacts.scalars():
try:
if artifact.file_path and os.path.exists(artifact.file_path):
os.remove(artifact.file_path)
deleted_files += 1
logger.info("Deleted artifact file",
path=artifact.file_path,
artifact_id=artifact.id)
except Exception as e:
failed_files += 1
logger.warning("Failed to delete artifact file",
path=artifact.file_path,
artifact_id=artifact.id if hasattr(artifact, 'id') else 'unknown',
error=str(e))
logger.info("Artifact files deletion complete",
deleted_files=deleted_files,
failed_files=failed_files)
# Now delete DB records
artifacts_result = await self.db.execute(
delete(ModelArtifact).where(
ModelArtifact.tenant_id == tenant_id
)
)
result.deleted_counts["model_artifacts"] = artifacts_result.rowcount
result.deleted_counts["artifact_files_deleted"] = deleted_files
result.deleted_counts["artifact_files_failed"] = failed_files
logger.info(
"training.tenant_deletion.artifacts_deleted",
tenant_id=tenant_id,
@@ -206,26 +225,54 @@ class TrainingTenantDeletionService(BaseTenantDataDeletionService):
# Step 5: Delete trained models (parent records)
logger.info("training.tenant_deletion.deleting_models", tenant_id=tenant_id)
# TODO: Delete physical model files (.pkl) before deleting DB records
# models = await self.db.execute(
# select(TrainedModel).where(TrainedModel.tenant_id == tenant_id)
# )
# for model in models.scalars():
# try:
# if model.model_path:
# os.remove(model.model_path) # Delete .pkl file
# if model.metadata_path:
# os.remove(model.metadata_path) # Delete metadata file
# except Exception as e:
# logger.warning("Failed to delete model file",
# path=model.model_path, error=str(e))
# Delete physical model files (.pkl) before deleting DB records
models = await self.db.execute(
select(TrainedModel).where(TrainedModel.tenant_id == tenant_id)
)
deleted_model_files = 0
failed_model_files = 0
for model in models.scalars():
try:
# Delete .pkl file
if hasattr(model, 'model_path') and model.model_path and os.path.exists(model.model_path):
os.remove(model.model_path)
deleted_model_files += 1
logger.info("Deleted model file",
path=model.model_path,
model_id=model.id)
# Delete model_file_path if it exists
if hasattr(model, 'model_file_path') and model.model_file_path and os.path.exists(model.model_file_path):
os.remove(model.model_file_path)
deleted_model_files += 1
logger.info("Deleted model file",
path=model.model_file_path,
model_id=model.id)
# Delete metadata file if exists
if hasattr(model, 'metadata_path') and model.metadata_path and os.path.exists(model.metadata_path):
os.remove(model.metadata_path)
logger.info("Deleted metadata file",
path=model.metadata_path,
model_id=model.id)
except Exception as e:
failed_model_files += 1
logger.warning("Failed to delete model file",
path=getattr(model, 'model_path', getattr(model, 'model_file_path', 'unknown')),
model_id=model.id if hasattr(model, 'id') else 'unknown',
error=str(e))
logger.info("Model files deletion complete",
deleted_files=deleted_model_files,
failed_files=failed_model_files)
# Now delete DB records
models_result = await self.db.execute(
delete(TrainedModel).where(
TrainedModel.tenant_id == tenant_id
)
)
result.deleted_counts["trained_models"] = models_result.rowcount
result.deleted_counts["model_files_deleted"] = deleted_model_files
result.deleted_counts["model_files_failed"] = failed_model_files
logger.info(
"training.tenant_deletion.models_deleted",
tenant_id=tenant_id,

View File

@@ -6,7 +6,7 @@ Simple, clean event publisher for the 4 main training steps
import structlog
from datetime import datetime
from typing import Dict, Any, Optional
from shared.messaging.rabbitmq import RabbitMQClient
from shared.messaging import RabbitMQClient
from app.core.config import settings
logger = structlog.get_logger()