| 1 | from datetime import datetime |
| 2 | |
| 3 | from loguru import logger |
| 4 | from sqlalchemy.future import select |
| 5 | |
| 6 | from app.db.db_session import get_db_session |
| 7 | from app.incidents.routes.incident_alert import create_alert_auto_route |
| 8 | from app.schedulers.models.scheduler import JobMetadata |
| 9 | |
| 10 | |
| 11 | async def invoke_alert_creation_collect(): |
| 12 | """ |
| 13 | Synchronizes agents by sending a request to the server and updating the job metadata. |
| 14 | |
| 15 | This function retrieves the scheduler auth token, sends a POST request to the server |
| 16 | to synchronize agents, and updates the job metadata with the last success timestamp. |
| 17 | |
| 18 | If the token retrieval fails, it prints a failure message. If the job metadata for |
| 19 | 'invoke_alert_creation_collect' does not exist, it prints a message indicating the absence of the metadata. |
| 20 | """ |
| 21 | logger.info("Invoking alert creation collection via scheduler...") |
| 22 | async with get_db_session() as session: |
| 23 | await create_alert_auto_route(session=session) |
| 24 | |
| 25 | stmt = select(JobMetadata).where(JobMetadata.job_id == "invoke_alert_creation_collect") |
| 26 | result = await session.execute(stmt) |
| 27 | job_metadata = result.scalars().first() |
| 28 | |
| 29 | if job_metadata: |
| 30 | job_metadata.last_success = datetime.utcnow() |
| 31 | session.add(job_metadata) |
| 32 | await session.commit() # Asynchronously commit the transaction |
| 33 | logger.info("Updated job metadata with the last success timestamp.") |
| 34 | else: |
| 35 | logger.warning("JobMetadata for 'invoke_alert_creation_collect' not found.") |