From 6e3dd832f2325dfb6700326e175889182faf3193 Mon Sep 17 00:00:00 2001
From: PChief <520133661@qq.com>
Date: Tue, 23 Dec 2025 18:21:25 +0800
Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3App.json=E9=85=8D=E7=BD=AE?=
=?UTF-8?q?=E6=98=AF=E5=90=A6=E5=90=AF=E7=94=A8=E5=AE=9A=E6=97=B6=E4=BB=BB?=
=?UTF-8?q?=E5=8A=A1=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Admin.NET.Core/Enum/ErrorCodeEnum.cs | 6 +++++
.../Service/Job/SysJobService.cs | 24 ++++++++++++++++---
Admin.NET/Admin.NET.Web.Core/Startup.cs | 16 +++++++++----
3 files changed, 39 insertions(+), 7 deletions(-)
diff --git a/Admin.NET/Admin.NET.Core/Enum/ErrorCodeEnum.cs b/Admin.NET/Admin.NET.Core/Enum/ErrorCodeEnum.cs
index dff72be30..422aefea8 100644
--- a/Admin.NET/Admin.NET.Core/Enum/ErrorCodeEnum.cs
+++ b/Admin.NET/Admin.NET.Core/Enum/ErrorCodeEnum.cs
@@ -745,6 +745,12 @@ public enum ErrorCodeEnum
[ErrorCodeItemMetadata("执行作业失败")]
D1705,
+ ///
+ /// 作业调度功能已禁用
+ ///
+ [ErrorCodeItemMetadata("作业调度功能已禁用")]
+ D1706,
+
///
/// 已存在同名打印模板
///
diff --git a/Admin.NET/Admin.NET.Core/Service/Job/SysJobService.cs b/Admin.NET/Admin.NET.Core/Service/Job/SysJobService.cs
index 61c39cf59..c5e91c097 100644
--- a/Admin.NET/Admin.NET.Core/Service/Job/SysJobService.cs
+++ b/Admin.NET/Admin.NET.Core/Service/Job/SysJobService.cs
@@ -16,21 +16,21 @@ public class SysJobService : IDynamicApiController, ITransient
private readonly SqlSugarRepository _sysJobTriggerRep;
private readonly SqlSugarRepository _sysJobTriggerRecordRep;
private readonly SqlSugarRepository _sysJobClusterRep;
- private readonly ISchedulerFactory _schedulerFactory;
+ private readonly ISchedulerFactory? _schedulerFactory;
private readonly DynamicJobCompiler _dynamicJobCompiler;
public SysJobService(SqlSugarRepository sysJobDetailRep,
SqlSugarRepository sysJobTriggerRep,
SqlSugarRepository sysJobTriggerRecordRep,
SqlSugarRepository sysJobClusterRep,
- ISchedulerFactory schedulerFactory,
+ IServiceProvider serviceProvider,
DynamicJobCompiler dynamicJobCompiler)
{
_sysJobDetailRep = sysJobDetailRep;
_sysJobTriggerRep = sysJobTriggerRep;
_sysJobTriggerRecordRep = sysJobTriggerRecordRep;
_sysJobClusterRep = sysJobClusterRep;
- _schedulerFactory = schedulerFactory;
+ _schedulerFactory = serviceProvider.GetService();
_dynamicJobCompiler = dynamicJobCompiler;
}
@@ -109,6 +109,7 @@ public class SysJobService : IDynamicApiController, ITransient
throw new NotSupportedException();
}
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
_schedulerFactory.AddJob(JobBuilder.Create(jobType).LoadFrom(input.Adapt()).SetJobType(jobType));
// 延迟一下等待持久化写入,再执行其他字段的更新
@@ -132,6 +133,7 @@ public class SysJobService : IDynamicApiController, ITransient
var sysJobDetail = await _sysJobDetailRep.GetFirstAsync(u => u.Id == input.Id);
if (sysJobDetail.JobId != input.JobId) throw Oops.Oh(ErrorCodeEnum.D1704);
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
var scheduler = _schedulerFactory.GetJob(sysJobDetail.JobId);
var oldScriptCode = sysJobDetail.ScriptCode; // 旧脚本代码
input.Adapt(sysJobDetail);
@@ -171,6 +173,7 @@ public class SysJobService : IDynamicApiController, ITransient
[DisplayName("删除作业")]
public async Task DeleteJobDetail(DeleteJobDetailInput input)
{
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
_schedulerFactory.RemoveJob(input.JobId);
// 如果 _schedulerFactory 中不存在 JodId,则无法触发持久化,下面的代码确保作业和触发器能被删除
@@ -203,6 +206,11 @@ public class SysJobService : IDynamicApiController, ITransient
var jobTrigger = input.Adapt();
jobTrigger.Args = "[" + jobTrigger.Args + "]";
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
var scheduler = _schedulerFactory.GetJob(input.JobId);
scheduler?.AddTrigger(Triggers.Create(input.AssemblyName, input.TriggerType).LoadFrom(jobTrigger));
}
@@ -229,6 +237,7 @@ public class SysJobService : IDynamicApiController, ITransient
}
jobTrigger.Args = "[" + jobTrigger.Args + "]";
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
var scheduler = _schedulerFactory.GetJob(input.JobId);
scheduler?.UpdateTrigger(Triggers.Create(input.AssemblyName, input.TriggerType).LoadFrom(jobTrigger));
}
@@ -241,6 +250,7 @@ public class SysJobService : IDynamicApiController, ITransient
[DisplayName("删除触发器")]
public async Task DeleteJobTrigger(DeleteJobTriggerInput input)
{
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
var scheduler = _schedulerFactory.GetJob(input.JobId);
scheduler?.RemoveTrigger(input.TriggerId);
@@ -255,6 +265,7 @@ public class SysJobService : IDynamicApiController, ITransient
[DisplayName("暂停所有作业")]
public void PauseAllJob()
{
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
_schedulerFactory.PauseAll();
}
@@ -265,6 +276,7 @@ public class SysJobService : IDynamicApiController, ITransient
[DisplayName("启动所有作业")]
public void StartAllJob()
{
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
_schedulerFactory.StartAll();
}
@@ -274,6 +286,7 @@ public class SysJobService : IDynamicApiController, ITransient
[DisplayName("暂停作业")]
public void PauseJob(JobDetailInput input)
{
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
_schedulerFactory.TryPauseJob(input.JobId, out _);
}
@@ -283,6 +296,7 @@ public class SysJobService : IDynamicApiController, ITransient
[DisplayName("启动作业")]
public void StartJob(JobDetailInput input)
{
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
_schedulerFactory.TryStartJob(input.JobId, out _);
}
@@ -292,6 +306,7 @@ public class SysJobService : IDynamicApiController, ITransient
[DisplayName("取消作业")]
public void CancelJob(JobDetailInput input)
{
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
_schedulerFactory.TryCancelJob(input.JobId, out _);
}
@@ -302,6 +317,7 @@ public class SysJobService : IDynamicApiController, ITransient
[DisplayName("执行作业")]
public void RunJob(JobDetailInput input)
{
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
if (_schedulerFactory.TryRunJob(input.JobId, out _) != ScheduleResult.Succeed) throw Oops.Oh(ErrorCodeEnum.D1705);
}
@@ -331,6 +347,7 @@ public class SysJobService : IDynamicApiController, ITransient
[DisplayName("强制唤醒作业调度器")]
public void CancelSleep()
{
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
_schedulerFactory.CancelSleep();
}
@@ -340,6 +357,7 @@ public class SysJobService : IDynamicApiController, ITransient
[DisplayName("强制触发所有作业持久化")]
public void PersistAll()
{
+ if (_schedulerFactory == null) throw Oops.Oh(ErrorCodeEnum.D1706);
_schedulerFactory.PersistAll();
}
diff --git a/Admin.NET/Admin.NET.Web.Core/Startup.cs b/Admin.NET/Admin.NET.Web.Core/Startup.cs
index e808c7e7a..9f43ecb41 100644
--- a/Admin.NET/Admin.NET.Web.Core/Startup.cs
+++ b/Admin.NET/Admin.NET.Web.Core/Startup.cs
@@ -7,9 +7,11 @@
using Admin.NET.Core;
using Admin.NET.Core.ElasticSearch;
using Admin.NET.Core.Service;
+
using AspNetCoreRateLimit;
using Furion;
using Furion.Logging;
+using Furion.Schedule;
using Furion.SpecificationDocument;
using Furion.VirtualFileServer;
using IGeekFan.AspNetCore.Knife4jUI;
@@ -77,11 +79,17 @@ public class Startup : AppStartup
// 任务队列
services.AddTaskQueue();
// 任务调度
- services.AddSchedule(options =>
+ var jobScheduleEnabled = App.GetConfig("JobSchedule:Enabled", false);
+ if (jobScheduleEnabled)
{
- options.AddPersistence(); // 添加作业持久化器
- options.AddMonitor(); // 添加作业执行监视器
- });
+ services.AddSchedule(options =>
+ {
+ options.AddPersistence(); // 添加作业持久化器
+ options.AddMonitor(); // 添加作业执行监视器
+ });
+ }
+ // 当任务调度禁用时,不注册ISchedulerFactory,这样serviceProvider.GetService()会返回null
+ // SysJobService中已经有null检查,会抛出D1706错误提示任务调度功能已禁用
// 脱敏检测
services.AddSensitiveDetection();
--
Gitee