This commit is contained in:
sunwang 2026-10-08 08:44:56 +08:00
parent 6466be3f0c
commit b2a9a38f38
2 changed files with 57 additions and 2 deletions

View File

@ -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}");
}
}
/// <summary>
/// 将启动任务的错误信息写入系统日志(ActionLogs),便于排查任务配置问题
/// </summary>
/// <param name="message">错误信息</param>
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)

View File

@ -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"
},
{