Browse Source

修改内容
1. 创建了 InitScheduledJobs 异步方法
将所有定时任务的初始化逻辑提取到一个独立的异步方法中
添加了XML注释说明方法用途
2. 将所有 _ = QuartzUtil.Add(...) 改为 await QuartzUtil.Add(...)
不再使用"fire-and-forget"模式
现在会等待每个任务注册完成后再继续
确保所有任务都正确注册到Quartz调度器中
3. 在 Main 方法中调用 InitScheduledJobs().Wait()
在启动Web服务之前先完成所有定时任务的注册
使用 .Wait() 阻塞等待异步初始化完成

luocaiyang 3 tuần trước cách đây
mục cha
commit
386b5b59b8

+ 2 - 15
wispro.sp.api/Job/BatchUpdateJXDataJob.cs

@@ -20,10 +20,8 @@ namespace wispro.sp.api.Job
     {
         private spDbContext spDb = new spDbContext();
 
-        string strLogFile = null;
         public Task Execute(IJobExecutionContext context)
         {
-            strLogFile = null;
             Log($"{DateTime.Now}\t批量更新任务开始");
 
             try
@@ -138,14 +136,6 @@ namespace wispro.sp.api.Job
                 for (int i = 0; i < totalCount; i++)
                 {
                     var item = lstItem[i];
-
-                    //调查案不更新
-                    if (item.CaseNo.Trim().StartsWith("S"))
-                    {
-                        Log($"{DateTime.Now}\t[{i + 1}/{totalCount}]\t{item.CaseNo}\t专案忽略!");
-                        continue;
-                    }
-
                     System.Threading.Thread.Sleep(100);
 
                     try
@@ -429,11 +419,8 @@ namespace wispro.sp.api.Job
         {
             try
             {
-                if(string.IsNullOrEmpty(strLogFile))
-                {
-                    strLogFile = $"c:\\temp\\{DateTime.Now.ToString("yyyyMMdd")}-Batch_Update_log.txt";
-                }
-                StreamWriter sw = File.AppendText(strLogFile);
+                string logPath = "c:\\temp\\batch_update_log.txt";
+                StreamWriter sw = File.AppendText(logPath);
                 sw.WriteLine($"{strMessage}");
                 sw.Flush();
                 sw.Close();

+ 18 - 12
wispro.sp.api/Program.cs

@@ -42,6 +42,17 @@ namespace wispro.sp.api
                 }
             }
 
+            // 初始化定时任务
+            InitScheduledJobs().Wait();
+
+            host.Run();
+        }
+
+        /// <summary>
+        /// 初始化所有定时任务
+        /// </summary>
+        private static async System.Threading.Tasks.Task InitScheduledJobs()
+        {
             #region 每月获取绩效数据Job
             JobKey jobKey = new JobKey("ImportReportData");
             var trigger = TriggerBuilder.Create()
@@ -49,8 +60,7 @@ namespace wispro.sp.api
                 .WithSchedule(CronScheduleBuilder.CronSchedule(utility.ConfigHelper.GetSectionValue("IPEasySetting:ScheduleSetting")).WithMisfireHandlingInstructionDoNothing())
                 .Build();
 
-
-            _ = QuartzUtil.Add(typeof(ImportReportJob), jobKey, trigger);
+            await QuartzUtil.Add(typeof(ImportReportJob), jobKey, trigger);
             #endregion
 
             #region 每月获取绩效数据Job
@@ -60,8 +70,7 @@ namespace wispro.sp.api
                 .WithSchedule(CronScheduleBuilder.CronSchedule(utility.ConfigHelper.GetSectionValue("ImportProjectScheduleSetting")).WithMisfireHandlingInstructionDoNothing())
                 .Build();
 
-
-            _ = QuartzUtil.Add(typeof(ImportProjectInfoJob), jobProject, triggerProject);
+            await QuartzUtil.Add(typeof(ImportProjectInfoJob), jobProject, triggerProject);
             #endregion
 
             #region 每天更新绩效数据
@@ -71,9 +80,9 @@ namespace wispro.sp.api
                 .WithSchedule(CronScheduleBuilder.CronSchedule(utility.ConfigHelper.GetSectionValue("UpdateScheduleSetting")).WithMisfireHandlingInstructionDoNothing())
                 .Build();
 
-
-            _ = QuartzUtil.Add(typeof(BatchUpdateJXDataJob), jobKey1, trigger1);
+            await QuartzUtil.Add(typeof(BatchUpdateJXDataJob), jobKey1, trigger1);
             #endregion
+
             #region 疑似绩效数据通知
             JobKey jobKey2 = new JobKey("InvalidData");
             var trigger2 = TriggerBuilder.Create()
@@ -81,8 +90,7 @@ namespace wispro.sp.api
                 .WithSchedule(CronScheduleBuilder.CronSchedule(utility.ConfigHelper.GetSectionValue("InvalidDataScheduleSetting")).WithMisfireHandlingInstructionDoNothing())
                 .Build();
 
-
-            _ = QuartzUtil.Add(typeof(InvalidDataMessageJob), jobKey2, trigger2);
+            await QuartzUtil.Add(typeof(InvalidDataMessageJob), jobKey2, trigger2);
             #endregion
 
             #region 通知代理人反馈
@@ -92,7 +100,7 @@ namespace wispro.sp.api
                 .WithSchedule(CronScheduleBuilder.CronSchedule(utility.ConfigHelper.GetSectionValue("AgentMessageScheduleSetting")).WithMisfireHandlingInstructionDoNothing())
                 .Build();
 
-            _ = QuartzUtil.Add(typeof(AgentMessageJob), jobKey3, trigger3);
+            await QuartzUtil.Add(typeof(AgentMessageJob), jobKey3, trigger3);
             #endregion
 
             #region 每天计算绩效数据
@@ -102,10 +110,8 @@ namespace wispro.sp.api
                 .WithSchedule(CronScheduleBuilder.CronSchedule("0 30 5 * * ?").WithMisfireHandlingInstructionDoNothing())
                 .Build();
 
-            _ = QuartzUtil.Add(typeof(CalJXJob), jobCalJX, triggerCalJX);
+            await QuartzUtil.Add(typeof(CalJXJob), jobCalJX, triggerCalJX);
             #endregion
-
-            host.Run();
         }
 
         public static IHostBuilder CreateHostBuilder(string[] args) =>

+ 1 - 1
wispro.sp.api/appsettings.json

@@ -31,7 +31,7 @@
     "Account": "caiyangl",
     "Password": "j)wx*lier*@3",
     "ChormeDriverPath": "e:\\source\\repos\\StaffPerformance\\packages\\ChormeDriver\\142",
-    "ScheduleSetting": "00 55 15 16 * ? *",
+    "ScheduleSetting": "00 33 14 21 * ? *",
     "IPEasyWeb": "http://47.106.94.35/Login.aspx"
   },
   "MailSetting": {