瀏覽代碼

job改为1分钟执行一次

jax 1 周之前
父節點
當前提交
4fab727809
共有 3 個文件被更改,包括 45 次插入50 次删除
  1. 1 11
      src/app/page.js
  2. 14 1
      src/app/personal-center/page.jsx
  3. 30 38
      src/app/utils/scheduler.js

+ 1 - 11
src/app/page.js

@@ -19,11 +19,6 @@ export default function Home() {
     router.push("/personal-center");
   };
 
-  const handleLogout = () => {
-    setCurrentUser(null);
-    localStorage.removeItem("currentUser");
-    router.push("/");
-  };
 
   return (
     <div className="bg-blue-600 text-white min-h-screen">
@@ -68,12 +63,7 @@ export default function Home() {
                     {currentUser.username}
                   </span>
                 </div>
-                <span
-                  className="text-sm font-bold cursor-pointer text-red-400 hover:text-red-500"
-                  onClick={handleLogout}
-                >
-                  退出登录
-                </span>
+
               </div>
             ) : (
               <button

+ 14 - 1
src/app/personal-center/page.jsx

@@ -178,6 +178,12 @@ const PersonalCenter = () => {
     }
   };
 
+  const handleLogout = () => {
+    setCurrentUser(null);
+    localStorage.removeItem("currentUser");
+    router.push("/");
+  };
+
   if (isLoading) {
     return (
       <div className="bg-blue-600 text-white min-h-screen p-4">Loading...</div>
@@ -201,8 +207,15 @@ const PersonalCenter = () => {
           size={32}
         />
         <h1 className="text-xl font-bold ml-4">个人中心</h1>
+        <div className="ml-auto px-4 py-2 font-bold bg-blue-500 hover:bg-blue-400 rounded-lg transition-colors">
+          <span
+            className="text-sm font-bold cursor-pointer text-red-400 hover:text-red-500"
+            onClick={handleLogout}
+          >
+            退出登录
+          </span>
+        </div>
       </div>
-
       <div className="bg-white text-black rounded-lg p-4 mb-4">
         <div className="flex items-center justify-between">
           <div className="flex items-center">

+ 30 - 38
src/app/utils/scheduler.js

@@ -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();