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