10 changed files with 181 additions and 41 deletions
@ -0,0 +1,41 @@ |
|||||
|
// Job Controller 示例:如何调用 service 层动态控制和查询定时任务
|
||||
|
import JobService from "services/JobService.js" |
||||
|
import Router from "utils/router.js" |
||||
|
|
||||
|
class JobController { |
||||
|
static routes() { |
||||
|
const router = new Router({ prefix: "/api/jobs" }) |
||||
|
router.get("/", JobController.list) |
||||
|
router.post("/start/:id", JobController.start) |
||||
|
router.post("/stop/:id", JobController.stop) |
||||
|
router.post("/update/:id", JobController.updateCron) |
||||
|
return router |
||||
|
} |
||||
|
|
||||
|
static async list(ctx) { |
||||
|
ctx.body = JobService.listJobs() |
||||
|
} |
||||
|
|
||||
|
static async start(ctx) { |
||||
|
const { id } = ctx.params |
||||
|
JobService.startJob(id) |
||||
|
ctx.body = { success: true, message: `${id} 任务已启动` } |
||||
|
} |
||||
|
|
||||
|
static async stop(ctx) { |
||||
|
const { id } = ctx.params |
||||
|
JobService.stopJob(id) |
||||
|
ctx.body = { success: true, message: `${id} 任务已停止` } |
||||
|
} |
||||
|
|
||||
|
static async updateCron(ctx) { |
||||
|
const { id } = ctx.params |
||||
|
const { cronTime } = ctx.request.body |
||||
|
JobService.updateJobCron(id, cronTime) |
||||
|
ctx.body = { success: true, message: `${id} 任务频率已修改` } |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export default JobController |
||||
|
|
||||
|
// 你可以在路由中引入这些 controller 方法,实现接口调用
|
@ -1,8 +1,11 @@ |
|||||
|
import { jobLogger } from "@/logger"; |
||||
|
|
||||
export default { |
export default { |
||||
id: 'exampleJob', |
id: 'example', |
||||
cronTime: '*/5 * * * * *', // 每5秒执行一次
|
cronTime: '*/10 * * * * *', // 每10秒执行一次
|
||||
task: () => { |
task: () => { |
||||
console.log('定时任务执行:', new Date()); |
jobLogger.info('Example Job 执行了'); |
||||
}, |
}, |
||||
options: { scheduled: false } // 由调度器统一启动
|
options: {}, |
||||
|
autoStart: false |
||||
}; |
}; |
||||
|
@ -1,30 +1,19 @@ |
|||||
import log4js from "log4js"; |
import log4js from "log4js"; |
||||
|
|
||||
// ANSI颜色代码常量定义
|
|
||||
const COLORS = { |
|
||||
REQ: '\x1b[36m', // 青色 - 请求标记
|
|
||||
RES: '\x1b[32m', // 绿色 - 响应标记
|
|
||||
TIME: '\x1b[33m', // 黄色 - 响应时间
|
|
||||
METHOD: '\x1b[1m', // 加粗 - HTTP方法
|
|
||||
RESET: '\x1b[0m' // 重置颜色
|
|
||||
}; |
|
||||
|
|
||||
const logger = log4js.getLogger(); |
const logger = log4js.getLogger(); |
||||
|
|
||||
/** |
/** |
||||
* 响应时间记录中间件 |
* 响应时间记录中间件 |
||||
* 为请求和响应添加彩色日志标记,并记录处理时间 |
|
||||
* @param {Object} ctx - Koa上下文对象 |
* @param {Object} ctx - Koa上下文对象 |
||||
* @param {Function} next - Koa中间件链函数 |
* @param {Function} next - Koa中间件链函数 |
||||
*/ |
*/ |
||||
export default async (ctx, next) => { |
export default async (ctx, next) => { |
||||
// 彩色请求日志:青色标记 + 加粗方法名
|
logger.info("====================[REQ]===================="); |
||||
logger.debug(`${COLORS.REQ}::req::${COLORS.RESET} ${COLORS.METHOD}%s${COLORS.RESET} %s`, ctx.method, ctx.path); |
logger.info(`➡️ ${ctx.method} ${ctx.path}`); |
||||
const start = Date.now(); |
const start = Date.now(); |
||||
await next(); |
await next(); |
||||
const ms = Date.now() - start; |
const ms = Date.now() - start; |
||||
ctx.set("X-Response-Time", `${ms}ms`); |
ctx.set("X-Response-Time", `${ms}ms`); |
||||
// 彩色响应日志:绿色标记 + 黄色响应时间 + 加粗方法名
|
logger.info(`⬅️ ${ctx.method} ${ctx.url} | ⏱️ ${ms}ms`); |
||||
logger.debug(`${COLORS.RES}::res::${COLORS.RESET} takes ${COLORS.TIME}%s${COLORS.RESET} for ${COLORS.METHOD}%s${COLORS.RESET} %s`, |
logger.info("====================[END]====================\n"); |
||||
ctx.response.get("X-Response-Time"), ctx.method, ctx.url); |
|
||||
} |
} |
||||
|
@ -0,0 +1,18 @@ |
|||||
|
import jobs from '../jobs'; |
||||
|
|
||||
|
class JobService { |
||||
|
static startJob(id) { |
||||
|
return jobs.start(id); |
||||
|
} |
||||
|
static stopJob(id) { |
||||
|
return jobs.stop(id); |
||||
|
} |
||||
|
static updateJobCron(id, cronTime) { |
||||
|
return jobs.updateCronTime(id, cronTime); |
||||
|
} |
||||
|
static listJobs() { |
||||
|
return jobs.list(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export default JobService; |
Loading…
Reference in new issue