🐛 fix定时任务手动执行偶现bug

This commit is contained in:
不做码农 2023-07-05 12:03:00 +08:00
parent 5d39958da2
commit 8a8dc3ad16
2 changed files with 12 additions and 8 deletions

View File

@ -253,11 +253,12 @@ namespace ZR.Admin.WebApi.Controllers
[Log(Title = "执行任务", BusinessType = BusinessType.OTHER)] [Log(Title = "执行任务", BusinessType = BusinessType.OTHER)]
public async Task<IActionResult> Run(string id) public async Task<IActionResult> Run(string id)
{ {
if (!_tasksQzService.Any(m => m.ID == id)) var result = await _tasksQzService.IsAnyAsync(m => m.ID == id);
if (!result)
{ {
throw new CustomException("任务不存在"); throw new CustomException("任务不存在");
} }
var tasksQz = _tasksQzService.GetFirst(m => m.ID == id); var tasksQz = await _tasksQzService.GetFirstAsync(m => m.ID == id);
var taskResult = await _schedulerServer.RunTaskScheduleAsync(tasksQz); var taskResult = await _schedulerServer.RunTaskScheduleAsync(tasksQz);
return ToResponse(taskResult); return ToResponse(taskResult);

View File

@ -3,8 +3,10 @@ using Infrastructure.Attribute;
using Quartz; using Quartz;
using Quartz.Impl; using Quartz.Impl;
using Quartz.Impl.Triggers; using Quartz.Impl.Triggers;
using SqlSugar.IOC;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using ZR.Model.System;
using ZR.Service.System.IService; using ZR.Service.System.IService;
namespace ZR.Tasks.TaskScheduler namespace ZR.Tasks.TaskScheduler
@ -15,13 +17,13 @@ namespace ZR.Tasks.TaskScheduler
[AppService(ServiceType = typeof(Job_HttpRequest), ServiceLifetime = LifeTime.Scoped)] [AppService(ServiceType = typeof(Job_HttpRequest), ServiceLifetime = LifeTime.Scoped)]
internal class Job_HttpRequest : JobBase, IJob internal class Job_HttpRequest : JobBase, IJob
{ {
private readonly ISysTasksQzService tasksQzService; //private readonly ISysTasksQzService tasksQzService;
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public Job_HttpRequest(ISysTasksQzService tasksQzService) //public Job_HttpRequest(ISysTasksQzService tasksQzService)
{ //{
this.tasksQzService = tasksQzService; // this.tasksQzService = tasksQzService;
} //}
public async Task Execute(IJobExecutionContext context) public async Task Execute(IJobExecutionContext context)
{ {
await ExecuteJob(context, async () => await Run(context)); await ExecuteJob(context, async () => await Run(context));
@ -29,7 +31,8 @@ namespace ZR.Tasks.TaskScheduler
public async Task Run(IJobExecutionContext context) public async Task Run(IJobExecutionContext context)
{ {
AbstractTrigger trigger = (context as JobExecutionContextImpl).Trigger as AbstractTrigger; AbstractTrigger trigger = (context as JobExecutionContextImpl).Trigger as AbstractTrigger;
var info = await tasksQzService.GetByIdAsync(trigger.JobName); //var info = await tasksQzService.CopyNew().GetByIdAsync(trigger.JobName);
var info = await DbScoped.SugarScope.CopyNew().Queryable<SysTasks>().FirstAsync(f => f.ID == trigger.JobName);
if (info == null) if (info == null)
{ {
throw new CustomException($"任务{trigger?.JobName}网络请求执行失败,任务不存在"); throw new CustomException($"任务{trigger?.JobName}网络请求执行失败,任务不存在");