
- Implemented APScheduler for automatic uptime checks - Each monitor runs on its own configured interval (default 5 minutes) - Scheduler starts automatically on application startup - Real-time job management when monitors are created/updated/deleted - Added scheduler status and control endpoints - Background logging of all check results - Updated documentation with scheduling features
55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
from fastapi import APIRouter
|
|
from app.services.scheduler import uptime_scheduler
|
|
|
|
router = APIRouter(prefix="/scheduler", tags=["scheduler"])
|
|
|
|
|
|
@router.get("/status")
|
|
def get_scheduler_status():
|
|
"""Get the current status of the scheduler and all running jobs"""
|
|
if not uptime_scheduler.is_running:
|
|
return {"status": "stopped", "jobs": []}
|
|
|
|
jobs = []
|
|
for job in uptime_scheduler.scheduler.get_jobs():
|
|
jobs.append(
|
|
{
|
|
"id": job.id,
|
|
"name": job.name,
|
|
"next_run_time": job.next_run_time.isoformat()
|
|
if job.next_run_time
|
|
else None,
|
|
"trigger": str(job.trigger),
|
|
}
|
|
)
|
|
|
|
return {"status": "running", "total_jobs": len(jobs), "jobs": jobs}
|
|
|
|
|
|
@router.post("/start")
|
|
def start_scheduler():
|
|
"""Start the scheduler"""
|
|
if uptime_scheduler.is_running:
|
|
return {"message": "Scheduler is already running"}
|
|
|
|
uptime_scheduler.start()
|
|
return {"message": "Scheduler started successfully"}
|
|
|
|
|
|
@router.post("/stop")
|
|
def stop_scheduler():
|
|
"""Stop the scheduler"""
|
|
if not uptime_scheduler.is_running:
|
|
return {"message": "Scheduler is already stopped"}
|
|
|
|
uptime_scheduler.stop()
|
|
return {"message": "Scheduler stopped successfully"}
|
|
|
|
|
|
@router.post("/restart")
|
|
def restart_scheduler():
|
|
"""Restart the scheduler and reschedule all monitors"""
|
|
uptime_scheduler.stop()
|
|
uptime_scheduler.start()
|
|
return {"message": "Scheduler restarted successfully"}
|