feature/T2026-05-016 implementar cargas de tareas en background e implementar y corregir auditoria para datastages
This commit is contained in:
@@ -57,46 +57,61 @@ from celery.result import AsyncResult
|
||||
|
||||
|
||||
class TaskStatusView(APIView):
|
||||
"""
|
||||
Vista para consultar el estado de tareas de Celery.
|
||||
"""
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
|
||||
def get(self, request, task_id):
|
||||
"""
|
||||
Consulta el estado de una tarea de Celery.
|
||||
|
||||
Returns:
|
||||
- PENDING: La tarea está esperando ser procesada
|
||||
- STARTED: La tarea ha sido iniciada
|
||||
- SUCCESS: La tarea se completó exitosamente
|
||||
- FAILURE: La tarea falló
|
||||
- RETRY: La tarea está reintentando
|
||||
Consulta el estado de una tarea Celery.
|
||||
|
||||
Estados posibles:
|
||||
PENDING — en cola, aún no inició
|
||||
STARTED — worker la tomó y está ejecutando
|
||||
SUCCESS — terminó correctamente, `result` contiene el resumen
|
||||
FAILURE — lanzó una excepción no capturada, `error` describe el problema
|
||||
RETRY — el worker la está reintentando
|
||||
"""
|
||||
try:
|
||||
task_result = AsyncResult(task_id)
|
||||
|
||||
state = task_result.state
|
||||
|
||||
response_data = {
|
||||
'task_id': task_id,
|
||||
'status': task_result.state,
|
||||
'status': state,
|
||||
'ready': task_result.ready(),
|
||||
'successful': task_result.successful() if task_result.ready() else None,
|
||||
}
|
||||
|
||||
if task_result.ready() and task_result.successful():
|
||||
try:
|
||||
response_data['result'] = task_result.result
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if task_result.state == 'FAILURE':
|
||||
|
||||
if state == 'SUCCESS':
|
||||
result = task_result.result
|
||||
response_data['result'] = result
|
||||
|
||||
# Resumen legible cuando es auditoría masiva de organización
|
||||
if isinstance(result, dict) and 'total_pedimentos' in result:
|
||||
total = result.get('total_pedimentos', 0)
|
||||
completados = result.get('completados', 0)
|
||||
con_pendientes = result.get('con_pendientes', 0)
|
||||
con_errores = result.get('con_errores', 0)
|
||||
|
||||
if con_pendientes == 0 and con_errores == 0:
|
||||
mensaje = f'Auditoría completa — {completados}/{total} pedimentos sin pendientes'
|
||||
else:
|
||||
partes = []
|
||||
if con_pendientes:
|
||||
partes.append(f'{con_pendientes} con documentos pendientes')
|
||||
if con_errores:
|
||||
partes.append(f'{con_errores} con error')
|
||||
mensaje = f'{completados}/{total} pedimentos completos — {", ".join(partes)}'
|
||||
|
||||
response_data['mensaje'] = mensaje
|
||||
|
||||
elif state == 'FAILURE':
|
||||
response_data['error'] = str(task_result.info)
|
||||
|
||||
if task_result.state == 'STARTED':
|
||||
|
||||
elif state == 'STARTED':
|
||||
response_data['info'] = str(task_result.info) if task_result.info else None
|
||||
|
||||
|
||||
return Response(response_data, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
except Exception as e:
|
||||
return Response(
|
||||
{'error': f'Error al consultar tarea: {str(e)}'},
|
||||
|
||||
Reference in New Issue
Block a user