// Job Controller 示例:如何调用 service 层动态控制和查询定时任务 import JobService from "services/JobService.js" import { formatResponse } from "utils/helper.js" import Router from "utils/router.js" class JobController { constructor() { this.jobService = new JobService() } async list(ctx) { const data = this.jobService.listJobs() ctx.body = formatResponse(true, data) } async start(ctx) { const { id } = ctx.params this.jobService.startJob(id) ctx.body = formatResponse(true, null, null, `${id} 任务已启动`) } async stop(ctx) { const { id } = ctx.params this.jobService.stopJob(id) ctx.body = formatResponse(true, null, null, `${id} 任务已停止`) } async updateCron(ctx) { const { id } = ctx.params const { cronTime } = ctx.request.body this.jobService.updateJobCron(id, cronTime) ctx.body = formatResponse(true, null, null, `${id} 任务频率已修改`) } static createRoutes() { const controller = new JobController() const router = new Router({ prefix: "/api/jobs" }) router.get("/", controller.list.bind(controller)) router.post("/start/:id", controller.start.bind(controller)) router.post("/stop/:id", controller.stop.bind(controller)) router.post("/update/:id", controller.updateCron.bind(controller)) return router } } export default JobController