— Use background tasks for alerting (fix #23)

This commit is contained in:
Luc Didry 2024-03-25 16:23:39 +01:00
parent e264f8a068
commit 5914ff889a
No known key found for this signature in database
GPG key ID: EA868E12D0257E3C

View file

@ -1,7 +1,7 @@
"""Web interface for machines""" """Web interface for machines"""
from typing import List, Union from typing import List, Union
from fastapi import APIRouter, Depends, Request from fastapi import APIRouter, BackgroundTasks, Depends, Request
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from argos.logging import logger from argos.logging import logger
@ -30,6 +30,7 @@ async def read_tasks(
async def create_results( async def create_results(
request: Request, request: Request,
results: List[AgentResult], results: List[AgentResult],
background_tasks: BackgroundTasks,
db: Session = Depends(get_db), db: Session = Depends(get_db),
config: Config = Depends(get_config), config: Config = Depends(get_config),
agent_id: Union[None, str] = None, agent_id: Union[None, str] = None,
@ -59,8 +60,9 @@ async def create_results(
# Dont create an alert if the severity has not changed # Dont create an alert if the severity has not changed
if last_severity != severity: if last_severity != severity:
# XXX Use a job queue or make it async background_tasks.add_task(
handle_alert(config, result, task, severity, last_severity, request) handle_alert, config, result, task, severity, last_severity, request
)
db_results.append(result) db_results.append(result)
db.commit() db.commit()