初始化
This commit is contained in:
80
api/scheduler_api.py
Normal file
80
api/scheduler_api.py
Normal file
@@ -0,0 +1,80 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from typing import Any, Dict
|
||||
from core.db import SessionDep
|
||||
from core.dto.api_response import ApiResponse
|
||||
from core.task.dlt_scheduler import scheduler
|
||||
|
||||
# 彩票相关的路由
|
||||
scheduler_router = APIRouter(prefix="/scheduler", tags=["大乐透调度器管理"])
|
||||
|
||||
@scheduler_router.get("/status")
|
||||
async def get_scheduler_status() -> Dict[str, Any]:
|
||||
"""获取调度器状态"""
|
||||
lists = []
|
||||
for job in scheduler.get_jobs():
|
||||
lists.append({
|
||||
"id": job.id,
|
||||
"name": job.name,
|
||||
"next_run_time": job.next_run_time
|
||||
})
|
||||
return {
|
||||
"running": scheduler.scheduler.running,
|
||||
"jobs": lists
|
||||
}
|
||||
|
||||
|
||||
@scheduler_router.post("/run")
|
||||
async def run_job_now() -> Dict[str, Any]:
|
||||
"""立即执行定时任务"""
|
||||
try:
|
||||
success, message = await scheduler.run_job_now()
|
||||
return {
|
||||
"success": success,
|
||||
"message": message
|
||||
}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@scheduler_router.post("/pause")
|
||||
async def pause_scheduler() -> Dict[str, Any]:
|
||||
"""暂停调度器"""
|
||||
if scheduler.pause():
|
||||
return {
|
||||
"success": True,
|
||||
"message": "调度器已停止"
|
||||
}
|
||||
|
||||
return {
|
||||
"success": False,
|
||||
"message": "调度器停止失败"
|
||||
}
|
||||
|
||||
|
||||
@scheduler_router.post("/resume")
|
||||
async def resume_scheduler() -> Dict[str, Any]:
|
||||
"""暂停调度器"""
|
||||
if scheduler.resume():
|
||||
return {
|
||||
"success": True,
|
||||
"message": "调度器已恢复运行"
|
||||
}
|
||||
|
||||
return {
|
||||
"success": False,
|
||||
"message": "调度器恢复运行失败"
|
||||
}
|
||||
|
||||
@scheduler_router.get("/jobs")
|
||||
async def list_jobs() -> Dict[str, Any]:
|
||||
"""列出所有定时任务"""
|
||||
lists = []
|
||||
for job in scheduler.get_jobs():
|
||||
lists.append({
|
||||
"id": job.id,
|
||||
"name": job.name,
|
||||
"next_run_time": job.next_run_time
|
||||
})
|
||||
return {
|
||||
"jobs": lists
|
||||
}
|
||||
Reference in New Issue
Block a user