From b2a9a38f384b050c5d5d81abf5a4eac979667e51 Mon Sep 17 00:00:00 2001
From: sunwang <2747217174@qq.com>
Date: Thu, 8 Oct 2026 08:44:56 +0800
Subject: [PATCH] =?UTF-8?q?=E4=B8=B4=E6=97=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
HHWMS.Web/Support/QuartzService.cs | 55 ++++++++++++++++++++++++++++--
HHWMS.Web/appsettings.json | 4 +++
2 files changed, 57 insertions(+), 2 deletions(-)
diff --git a/HHWMS.Web/Support/QuartzService.cs b/HHWMS.Web/Support/QuartzService.cs
index cc023a6..0ec3697 100644
--- a/HHWMS.Web/Support/QuartzService.cs
+++ b/HHWMS.Web/Support/QuartzService.cs
@@ -2,6 +2,7 @@ using System;
using System.Linq;
using System.Threading.Tasks;
using HHWMS.Web.Models;
+using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Triggers;
@@ -44,9 +45,29 @@ namespace HHWMS.Web.Support
try
{
var test = _gd.AllAssembly.FirstOrDefault(x => x.FullName.Contains("HHWMS.Web"));
+ if (test == null)
+ {
+ // 程序集未加载:记录日志并跳过,避免启动流程中断
+ WriteJobError($"启动任务 {jobDetail.JobName} 失败:未找到 HHWMS.Web 程序集");
+ return;
+ }
var t = test.GetExportedTypes().FirstOrDefault(x => typeof(WtmJob).IsAssignableFrom(x) && x.Name == jobDetail.JobName);
+ if (t == null)
+ {
+ // 类名不匹配:列出可用类型,便于排查数据库 SysJob 表中的无效记录
+ var available = string.Join(", ", test.GetExportedTypes()
+ .Where(x => typeof(WtmJob).IsAssignableFrom(x))
+ .Select(x => x.Name));
+ WriteJobError($"启动任务 {jobDetail.JobName} 失败:未找到对应类型,可用类型:{available}");
+ return;
+ }
var ci = t.GetConstructor(Type.EmptyTypes);
- if (ci?.Invoke(null) as WtmJob != null)
+ if (ci == null)
+ {
+ WriteJobError($"启动任务 {jobDetail.JobName} 失败:类型 {t.FullName} 缺少无参构造函数");
+ return;
+ }
+ if (ci.Invoke(null) as WtmJob != null)
{
string jobName = jobDetail.JobName;
string groupName = jobDetail.JobName;
@@ -78,7 +99,37 @@ namespace HHWMS.Web.Support
_scheduler.PauseJob(new JobKey(jobName, groupName)).Wait();
}
}
- catch { }
+ catch (Exception ex)
+ {
+ // 记录启动任务的异常信息,避免被静默吞掉导致难以排查
+ WriteJobError($"启动任务 {jobDetail.JobName} 异常:{ex.Message}");
+ }
+ }
+
+ ///
+ /// 将启动任务的错误信息写入系统日志(ActionLogs),便于排查任务配置问题
+ ///
+ /// 错误信息
+ private void WriteJobError(string message)
+ {
+ try
+ {
+ // WTMContext 是 Scoped 服务,不能从 root provider 直接解析,需创建独立作用域
+ var scopeFactory = _sp.GetService(typeof(IServiceScopeFactory)) as IServiceScopeFactory;
+ if (scopeFactory == null)
+ {
+ return;
+ }
+ using (var scope = scopeFactory.CreateScope())
+ {
+ var wtm = scope.ServiceProvider.GetService(typeof(WTMContext)) as WTMContext;
+ wtm?.DoLog("", ActionLogTypesEnum.Normal, "QuartzService", result: message);
+ }
+ }
+ catch
+ {
+ // 写日志失败不影响主流程
+ }
}
public void DeleteJob(SysJob job)
diff --git a/HHWMS.Web/appsettings.json b/HHWMS.Web/appsettings.json
index 5a274f1..4d9680d 100644
--- a/HHWMS.Web/appsettings.json
+++ b/HHWMS.Web/appsettings.json
@@ -24,7 +24,11 @@
"Connections": [
{
"Key": "default",
+<<<<<<< Updated upstream
"Value": "Server=127.0.0.1;Port=3306;Database=wms_yongfeng_cp;Uid=root;Password=abc123*;Character Set=utf8mb4;",
+=======
+ "Value": "Server=127.0.0.1;Port=3306;Database=wms_yongfeng_cp;Uid=root;Password=123456;Character Set=utf8mb4;",
+>>>>>>> Stashed changes
"DBType": "mysql"
},
{