Scheduler fix (#196)
* Add get_next_run endpoint to scheduler routes and remove the tablename * let apscheduler create the table for me * Update time interval for agent_sync job
taylor_socfortress committed
Apr 23, 2024 at 10:56 UTC
9232c4c6887330ddf8fb706e951ed08a9a920f5b
2 files changed
+24
-1
backend/app/schedulers/routes/scheduler.py
+23
@@ -3,10 +3,12 @@ from typing import Optional
3
4
from fastapi import APIRouter
5
from fastapi import Depends
6
+from datetime import datetime
7
from fastapi import HTTPException
8
from loguru import logger
9
from sqlalchemy.ext.asyncio import AsyncSession
10
from sqlalchemy.future import select
11
+import pytz
12
13
from app.db.db_session import get_db
14
from app.schedulers.models.scheduler import JobMetadata
@@ -114,6 +116,27 @@ async def get_all_jobs(session: AsyncSession = Depends(get_db)) -> JobsResponse:
116
message="Jobs successfully retrieved.",
117
)
118
119
+@scheduler_router.get("/next_run/{job_id}")
120
+async def get_next_run(job_id: str):
121
+ """
122
+ Get the next run time of a job.
123
+
124
+ Args:
125
+ job_id (str): The ID of the job.
126
+
127
+ Returns:
128
+ dict: A dictionary containing the next run time of the job.
129
+ """
130
+ scheduler = await get_scheduler_instance()
131
+ job = await find_job_by_id(scheduler, job_id)
132
+ if job is None:
133
+ raise HTTPException(status_code=404, detail="Job not found")
134
+ now = datetime.now(pytz.utc)
135
+ next_run_time = job.next_run_time
136
+ delta = next_run_time - now
137
+ logger.info(f"Next run time for job {job_id}: {next_run_time}")
138
+ return None
139
+
140
141
@scheduler_router.post("/add", description="Add a job")
142
async def add_job(
backend/app/schedulers/scheduler.py
+1
-1
@@ -77,7 +77,7 @@ async def init_scheduler():
77
78
logger.info("Initializing new scheduler...")
79
try:
80
- jobstores = {"default": SQLAlchemyJobStore(engine=sync_engine, tablename="schedulerjob")}
80
+ jobstores = {"default": SQLAlchemyJobStore(engine=sync_engine)}
81
executors = {"default": AsyncIOExecutor()} # This executor can run asyncio coroutines
82
event_loop = asyncio.get_event_loop()
83
scheduler_instance = AsyncIOScheduler(event_loop=event_loop)