bmc_hub/app/workflows/monitoring.py

38 lines
2.3 KiB
Python

"""Authorized queue inspection and explicit retry, without runtime activation."""
from uuid import uuid4
from fastapi import APIRouter,Depends,HTTPException
from psycopg2.extras import Json
from app.core.auth_dependencies import require_permission
from app.core.config import settings
from .service import transaction
from .integration import effective_customer_ids
router=APIRouter(prefix='/workflows/runtime')
@router.get('/status')
def status(user=Depends(require_permission('workflows.view'))):
with transaction() as cursor:
cursor.execute('SELECT status,count(*) AS count,min(created_at) AS oldest FROM bmc_workflow_events GROUP BY status')
queues=cursor.fetchall()
cursor.execute("SELECT id,trigger,entity_type,entity_id,status,attempts,error,created_at FROM bmc_workflow_events WHERE status='failed' ORDER BY created_at DESC LIMIT 50")
failed=cursor.fetchall()
customer_ids=effective_customer_ids(cursor)
return {'enabled':settings.WORKFLOWS_ENABLED,'mode':settings.WORKFLOWS_MODE,
'customer_ids':customer_ids,'internal_enabled':settings.WORKFLOWS_INTERNAL_ENABLED,
'ai_enabled':settings.WORKFLOW_AI_ENABLED,'queues':queues,'failed':failed}
@router.post('/events/{event_id}/retry')
def retry(event_id:str,user=Depends(require_permission('workflows.manage'))):
if not settings.WORKFLOWS_ENABLED:
raise HTTPException(409,'Workflowmotoren er slukket')
with transaction() as cursor:
cursor.execute('SELECT * FROM bmc_workflow_events WHERE id=%s FOR UPDATE',(event_id,))
previous=cursor.fetchone()
if not previous:raise HTTPException(404,'Hændelsen findes ikke')
if previous['status']!='failed':raise HTTPException(409,'Kun fejlede hændelser kan genforsøges')
new_id=str(uuid4())
context=dict(previous['context'],retry_of=previous['id'],retry_by=user['id'])
cursor.execute('''INSERT INTO bmc_workflow_events(id,event_key,trigger,entity_type,entity_id,revision,context,chain)
VALUES(%s,%s,%s,%s,%s,%s,%s,%s)''',(new_id,'retry:'+previous['id']+':'+new_id,previous['trigger'],previous['entity_type'],previous['entity_id'],previous['revision'],Json(context),Json(previous['chain'])))
return {'event_id':new_id,'retry_of':previous['id']}