Se agregaron estados y update a cada una de las tareas

This commit is contained in:
2025-10-12 07:53:05 -06:00
parent 770e0a4d13
commit 62a7f67b23
17 changed files with 640 additions and 229 deletions

View File

@@ -1,17 +1,19 @@
from fastapi import FastAPI
from fastapi import FastAPI, Depends
from fastapi.routing import APIRouter
from celery_app import celery_app
from fastapi import APIRouter, HTTPException, BackgroundTasks
from datetime import datetime
from fastapi.responses import JSONResponse
from typing import Dict, Any, List, Optional
from api.api_v2.modules.authentication.services import get_current_user
import logging
logger = logging.getLogger(__name__)
router = APIRouter()
@router.get("/async/task-status/{task_id}")
async def get_task_status(task_id: str):
@router.get("/async/task-status/{task_id}", response_model=Dict[str, Any])
async def get_task_status(task_id: str, current_user: Dict = Depends(get_current_user)):
"""
Consulta el estado de una tarea agendada.
@@ -84,37 +86,9 @@ async def get_task_status(task_id: str):
detail=f"Error al consultar el estado de la tarea: {str(e)}"
)
@router.get("/async/tasks/active")
async def get_active_tasks():
"""
Lista todas las tareas activas en el sistema.
Returns:
JSONResponse con la lista de tareas activas
"""
try:
# Obtener tareas activas desde Celery
inspect = celery_app.control.inspect()
active_tasks = inspect.active()
scheduled_tasks = inspect.scheduled()
response_data = {
"active_tasks": active_tasks or {},
"scheduled_tasks": scheduled_tasks or {},
"timestamp": datetime.utcnow().isoformat()
}
return JSONResponse(content=response_data, status_code=200)
except Exception as e:
logger.error(f"Error al obtener tareas activas: {e}")
raise HTTPException(
status_code=500,
detail=f"Error al obtener tareas activas: {str(e)}"
)
@router.delete("/async/task/{task_id}")
async def cancel_task(task_id: str):
@router.delete("/async/task/{task_id}", response_model=Dict[str, Any])
async def cancel_task(task_id: str, current_user: Dict = Depends(get_current_user)):
"""
Cancela una tarea agendada.