|
@@ -1,83 +1,75 @@
|
|
|
// src/utils/scheduler.js
|
|
|
import { CronJob } from "cron";
|
|
|
-import moment from "moment-timezone/moment-timezone-utils";
|
|
|
-import Match from "../models/Match";
|
|
|
+import moment from "moment-timezone";
|
|
|
+import Match from "../models/Match.js";
|
|
|
|
|
|
const timezone = "Asia/Shanghai";
|
|
|
-
|
|
|
-
|
|
|
+let job;
|
|
|
|
|
|
function startJob() {
|
|
|
- const job = new CronJob(
|
|
|
- "*/5 * * * *",
|
|
|
+ job = new CronJob(
|
|
|
+ "*/1 * * * *", // 每分钟执行一次
|
|
|
async () => {
|
|
|
const now = moment.tz(timezone);
|
|
|
- const currentDate = now.toDate();
|
|
|
- const currentTimeString = now.format("HH:mm");
|
|
|
+ const currentDateStr = now.format("YYYY-MM-DD");
|
|
|
+ const currentTimeStr = now.format("HH:mm");
|
|
|
|
|
|
- // 创建2小时前的时间点
|
|
|
const twoHoursAgo = moment(now).subtract(2, "hours");
|
|
|
- const twoHoursAgoDate = twoHoursAgo.format("YYYY-MM-DD"); // 使用日期字符串
|
|
|
- const twoHoursAgoTime = twoHoursAgo.format("HH:mm");
|
|
|
- // console.log("定时任务:每5分钟执行一次" + twoHoursAgoDate + ":" + twoHoursAgoTime);
|
|
|
+ const twoHoursAgoDateStr = twoHoursAgo.format("YYYY-MM-DD");
|
|
|
+ const twoHoursAgoTimeStr = twoHoursAgo.format("HH:mm");
|
|
|
+
|
|
|
+ console.log(`[定时任务执行] 当前时间: ${currentDateStr} ${currentTimeStr}, 两小时前: ${twoHoursAgoDateStr} ${twoHoursAgoTimeStr}`);
|
|
|
+
|
|
|
try {
|
|
|
- // 更新已结束的比赛
|
|
|
- const updateResult1 = await Match.updateMany(
|
|
|
+ // 标记为已结束
|
|
|
+ await Match.updateMany(
|
|
|
{
|
|
|
$or: [
|
|
|
- // 早于今天的比赛
|
|
|
- { date: { $lt: twoHoursAgoDate } },
|
|
|
- // 今天的比赛且开始时间在2小时前
|
|
|
+ { date: { $lt: twoHoursAgoDateStr } },
|
|
|
{
|
|
|
- date: twoHoursAgoDate,
|
|
|
- time: { $lte: twoHoursAgoTime },
|
|
|
+ date: twoHoursAgoDateStr,
|
|
|
+ time: { $lte: twoHoursAgoTimeStr },
|
|
|
},
|
|
|
],
|
|
|
status: { $in: ["未开始", "进行中"] },
|
|
|
type: { $in: ["football", "basketball"] },
|
|
|
},
|
|
|
- { $set: { status: "已结束" } } // 设置状态为已结束
|
|
|
+ { $set: { status: "已结束" } }
|
|
|
);
|
|
|
|
|
|
- // console.log(`已结束的比赛数量: ${updateResult1.modifiedCount}`);
|
|
|
-
|
|
|
- // 更新进行中的比赛
|
|
|
- const updateResult2 = await Match.updateMany(
|
|
|
+ // 标记为进行中
|
|
|
+ await Match.updateMany(
|
|
|
{
|
|
|
- date: { $lte: currentDate },
|
|
|
- time: { $lte: currentTimeString },
|
|
|
+ date: { $lte: currentDateStr },
|
|
|
+ time: { $lte: currentTimeStr },
|
|
|
status: "未开始",
|
|
|
- // 排除已经过去2小时的比赛(因为上面的查询会处理)
|
|
|
$or: [
|
|
|
- { date: { $gt: twoHoursAgoDate } },
|
|
|
+ { date: { $gt: twoHoursAgoDateStr } },
|
|
|
{
|
|
|
- date: twoHoursAgoDate,
|
|
|
- time: { $gt: twoHoursAgoTime },
|
|
|
+ date: twoHoursAgoDateStr,
|
|
|
+ time: { $gt: twoHoursAgoTimeStr },
|
|
|
},
|
|
|
],
|
|
|
type: { $in: ["football", "basketball"] },
|
|
|
},
|
|
|
- { $set: { status: "进行中" } } // 设置状态为进行中
|
|
|
+ { $set: { status: "进行中" } }
|
|
|
);
|
|
|
-
|
|
|
- // console.log(`进行中的比赛数量: ${updateResult2.modifiedCount}`);
|
|
|
} catch (error) {
|
|
|
- console.error("更新比赛状态时出错:", error);
|
|
|
+ console.error("更新比赛状态出错:", error);
|
|
|
}
|
|
|
},
|
|
|
null,
|
|
|
true,
|
|
|
- "Asia/Shanghai"
|
|
|
+ timezone
|
|
|
);
|
|
|
+
|
|
|
job.start();
|
|
|
}
|
|
|
|
|
|
-// 使用 process.on 确保任务在服务器启动时执行
|
|
|
process.on("SIGINT", () => {
|
|
|
console.log("服务器关闭,清理定时任务...");
|
|
|
- // 在这里可以清理定时任务
|
|
|
+ job?.stop();
|
|
|
process.exit();
|
|
|
});
|
|
|
|
|
|
-// 立即启动定时任务
|
|
|
startJob();
|