Pārlūkot izejas kodu

job改为1分钟执行一次

jax 3 nedēļas atpakaļ
vecāks
revīzija
4fab727809
3 mainītis faili ar 45 papildinājumiem un 50 dzēšanām
  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");
     router.push("/personal-center");
   };
   };
 
 
-  const handleLogout = () => {
-    setCurrentUser(null);
-    localStorage.removeItem("currentUser");
-    router.push("/");
-  };
 
 
   return (
   return (
     <div className="bg-blue-600 text-white min-h-screen">
     <div className="bg-blue-600 text-white min-h-screen">
@@ -68,12 +63,7 @@ export default function Home() {
                     {currentUser.username}
                     {currentUser.username}
                   </span>
                   </span>
                 </div>
                 </div>
-                <span
+
-                  className="text-sm font-bold cursor-pointer text-red-400 hover:text-red-500"
-                  onClick={handleLogout}
-                >
-                  退出登录
-                </span>
               </div>
               </div>
             ) : (
             ) : (
               <button
               <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) {
   if (isLoading) {
     return (
     return (
       <div className="bg-blue-600 text-white min-h-screen p-4">Loading...</div>
       <div className="bg-blue-600 text-white min-h-screen p-4">Loading...</div>
@@ -201,8 +207,15 @@ const PersonalCenter = () => {
           size={32}
           size={32}
         />
         />
         <h1 className="text-xl font-bold ml-4">个人中心</h1>
         <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>
-
       <div className="bg-white text-black rounded-lg p-4 mb-4">
       <div className="bg-white text-black rounded-lg p-4 mb-4">
         <div className="flex items-center justify-between">
         <div className="flex items-center justify-between">
           <div className="flex items-center">
           <div className="flex items-center">

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

@@ -1,83 +1,75 @@
 // src/utils/scheduler.js
 // src/utils/scheduler.js
 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");
 
 
-            // 创建2小时前的时间点
             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("定时任务:每5分钟执行一次" + twoHoursAgoDate + ":" + twoHoursAgoTime);
+
+            console.log(`[定时任务执行] 当前时间: ${currentDateStr} ${currentTimeStr}, 两小时前: ${twoHoursAgoDateStr} ${twoHoursAgoTimeStr}`);
+
             try {
             try {
-                // 更新已结束的比赛
+                // 标记为已结束
-                const updateResult1 = await Match.updateMany(
+                await Match.updateMany(
                     {
                     {
                         $or: [
                         $or: [
-                            // 早于今天的比赛
+                            { date: { $lt: twoHoursAgoDateStr } },
-                            { date: { $lt: twoHoursAgoDate } },
-                            // 今天的比赛且开始时间在2小时前
                             {
                             {
-                                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: "已结束" } }
                 );
                 );
 
 
-                // console.log(`已结束的比赛数量: ${updateResult1.modifiedCount}`);
+                // 标记为进行中
-
+                await Match.updateMany(
-                // 更新进行中的比赛
-                const updateResult2 = await Match.updateMany(
                     {
                     {
-                        date: { $lte: currentDate },
+                        date: { $lte: currentDateStr },
-                        time: { $lte: currentTimeString },
+                        time: { $lte: currentTimeStr },
                         status: "未开始",
                         status: "未开始",
-                        // 排除已经过去2小时的比赛(因为上面的查询会处理)
                         $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: "进行中" } }
                 );
                 );
-
-                // console.log(`进行中的比赛数量: ${updateResult2.modifiedCount}`);
             } catch (error) {
             } catch (error) {
-                console.error("更新比赛状态出错:", error);
+                console.error("更新比赛状态出错:", error);
             }
             }
         },
         },
         null,
         null,
         true,
         true,
-        "Asia/Shanghai"
+        timezone
     );
     );
+
     job.start();
     job.start();
 }
 }
 
 
-// 使用 process.on 确保任务在服务器启动时执行
 process.on("SIGINT", () => {
 process.on("SIGINT", () => {
     console.log("服务器关闭,清理定时任务...");
     console.log("服务器关闭,清理定时任务...");
-    // 在这里可以清理定时任务
+    job?.stop();
     process.exit();
     process.exit();
 });
 });
 
 
-// 立即启动定时任务
 startJob();
 startJob();