|
@@ -1,83 +1,75 @@
|
|
|
|
|
|
import { CronJob } from "cron";
|
|
import { CronJob } from "cron";
|
|
-import moment from "moment-timezone/moment-timezone-utils";
|
|
+import moment from "moment-timezone";
|
|
-import Match from "../models/Match";
|
|
+import Match from "../models/Match.js";
|
|
|
|
|
|
const timezone = "Asia/Shanghai";
|
|
const timezone = "Asia/Shanghai";
|
|
-
|
|
+let job;
|
|
-
|
|
|
|
|
|
|
|
function startJob() {
|
|
function startJob() {
|
|
- const job = new CronJob(
|
|
+ job = new CronJob(
|
|
- "*/5 * * * *",
|
|
+ "*/1 * * * *",
|
|
async () => {
|
|
async () => {
|
|
const now = moment.tz(timezone);
|
|
const now = moment.tz(timezone);
|
|
- const currentDate = now.toDate();
|
|
+ const currentDateStr = now.format("YYYY-MM-DD");
|
|
- const currentTimeString = now.format("HH:mm");
|
|
+ const currentTimeStr = now.format("HH:mm");
|
|
|
|
|
|
-
|
|
|
|
const twoHoursAgo = moment(now).subtract(2, "hours");
|
|
const twoHoursAgo = moment(now).subtract(2, "hours");
|
|
- const twoHoursAgoDate = twoHoursAgo.format("YYYY-MM-DD");
|
|
+ const twoHoursAgoDateStr = twoHoursAgo.format("YYYY-MM-DD");
|
|
- const twoHoursAgoTime = twoHoursAgo.format("HH:mm");
|
|
+ const twoHoursAgoTimeStr = twoHoursAgo.format("HH:mm");
|
|
-
|
|
+
|
|
|
|
+ console.log(`[定时任务执行] 当前时间: ${currentDateStr} ${currentTimeStr}, 两小时前: ${twoHoursAgoDateStr} ${twoHoursAgoTimeStr}`);
|
|
|
|
+
|
|
try {
|
|
try {
|
|
-
|
|
+
|
|
- const updateResult1 = await Match.updateMany(
|
|
+ await Match.updateMany(
|
|
{
|
|
{
|
|
$or: [
|
|
$or: [
|
|
-
|
|
+ { date: { $lt: twoHoursAgoDateStr } },
|
|
- { date: { $lt: twoHoursAgoDate } },
|
|
|
|
-
|
|
|
|
{
|
|
{
|
|
- date: twoHoursAgoDate,
|
|
+ date: twoHoursAgoDateStr,
|
|
- time: { $lte: twoHoursAgoTime },
|
|
+ time: { $lte: twoHoursAgoTimeStr },
|
|
},
|
|
},
|
|
],
|
|
],
|
|
status: { $in: ["未开始", "进行中"] },
|
|
status: { $in: ["未开始", "进行中"] },
|
|
type: { $in: ["football", "basketball"] },
|
|
type: { $in: ["football", "basketball"] },
|
|
},
|
|
},
|
|
- { $set: { status: "已结束" } }
|
|
+ { $set: { status: "已结束" } }
|
|
);
|
|
);
|
|
|
|
|
|
-
|
|
+
|
|
-
|
|
+ await Match.updateMany(
|
|
-
|
|
|
|
- const updateResult2 = await Match.updateMany(
|
|
|
|
{
|
|
{
|
|
- date: { $lte: currentDate },
|
|
+ date: { $lte: currentDateStr },
|
|
- time: { $lte: currentTimeString },
|
|
+ time: { $lte: currentTimeStr },
|
|
status: "未开始",
|
|
status: "未开始",
|
|
-
|
|
|
|
$or: [
|
|
$or: [
|
|
- { date: { $gt: twoHoursAgoDate } },
|
|
+ { date: { $gt: twoHoursAgoDateStr } },
|
|
{
|
|
{
|
|
- date: twoHoursAgoDate,
|
|
+ date: twoHoursAgoDateStr,
|
|
- time: { $gt: twoHoursAgoTime },
|
|
+ time: { $gt: twoHoursAgoTimeStr },
|
|
},
|
|
},
|
|
],
|
|
],
|
|
type: { $in: ["football", "basketball"] },
|
|
type: { $in: ["football", "basketball"] },
|
|
},
|
|
},
|
|
- { $set: { status: "进行中" } }
|
|
+ { $set: { status: "进行中" } }
|
|
);
|
|
);
|
|
-
|
|
|
|
-
|
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- console.error("更新比赛状态时出错:", error);
|
|
+ console.error("更新比赛状态出错:", error);
|
|
}
|
|
}
|
|
},
|
|
},
|
|
null,
|
|
null,
|
|
true,
|
|
true,
|
|
- "Asia/Shanghai"
|
|
+ timezone
|
|
);
|
|
);
|
|
|
|
+
|
|
job.start();
|
|
job.start();
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
process.on("SIGINT", () => {
|
|
process.on("SIGINT", () => {
|
|
console.log("服务器关闭,清理定时任务...");
|
|
console.log("服务器关闭,清理定时任务...");
|
|
-
|
|
+ job?.stop();
|
|
process.exit();
|
|
process.exit();
|
|
});
|
|
});
|
|
|
|
|
|
-
|
|
|
|
startJob();
|
|
startJob();
|