diff --git a/index.ts b/index.ts index 1a811dd..1ff10d3 100644 --- a/index.ts +++ b/index.ts @@ -1,11 +1,11 @@ import { manageCIMonitorReq } from "./routes/ci" import { manageGitlabEventReq } from "./routes/event" -import initSchedule from "./schedule" +// import initSchedule from "./schedule" import netTool from "./service/netTool" import { makeCheckPathTool } from "./utils/pathTools" // 启动定时任务 -initSchedule() +// initSchedule() const PREFIX = "/gitlab_monitor" diff --git a/schedule/index.ts b/schedule/index.ts index ac5e990..428b18b 100644 --- a/schedule/index.ts +++ b/schedule/index.ts @@ -1,6 +1,7 @@ import { scheduleJob } from "node-schedule" import manageRobot from "../controllers/manageRobot" +import monitorJob from "./monitorJob" import syncPipeLine from "./syncPipeLine" const initSchedule = async () => { @@ -9,11 +10,11 @@ const initSchedule = async () => { // 每15分钟同步一次CI数据 scheduleJob("*/15 * * * *", syncPipeLine) // // 每30秒执行一次监控任务 - // scheduleJob("*30 * * * * *", monitorJob) + scheduleJob("*30 * * * * *", monitorJob) // 立即同步一次 syncPipeLine() // // 立即执行一次监控任务 - // monitorJob() + monitorJob() } export default initSchedule diff --git a/schedule/monitorJob.ts b/schedule/monitorJob.ts index aa51699..8a21642 100644 --- a/schedule/monitorJob.ts +++ b/schedule/monitorJob.ts @@ -49,16 +49,20 @@ const doMonitor = async (monitor: DB.Monitor): Promise => { * @returns {Promise} 无返回值 * @description 该函数从数据库中获取所有监控项,并移除创建时间超过24小时的监控项。 */ -const removeOverTimeMonitor = async (): Promise => { - const fullMonitorList = await db.monitor.getFullList() +const removeOverTimeMonitor = async ( + fullMonitorList: DB.Monitor[] +): Promise => { const now = Date.now() + const limit = pLimit(5) await Promise.all( - fullMonitorList.map(async (monitor) => { - const createdAtTimestamp = new Date(monitor.created_at).getTime() - if (now - createdAtTimestamp > 24 * 60 * 60 * 1000) { - await db.monitor.del(monitor.id) - } - }) + fullMonitorList.map((monitor) => + limit(async () => { + const createdAtTimestamp = new Date(monitor.created_at).getTime() + if (now - createdAtTimestamp > 24 * 60 * 60 * 1000) { + await db.monitor.del(monitor.id) + } + }) + ) ) } @@ -70,7 +74,7 @@ const monitorJob = async (): Promise => { // 获取全部监控项 const fullMonitorList = await db.monitor.getFullList() // 移除超过24小时的监控项 - removeOverTimeMonitor() + removeOverTimeMonitor(fullMonitorList) if (fullMonitorList.length === 0) return // 并发限制 const limit = pLimit(3)