diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml index 726f6ee9..7c90e33d 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml @@ -602,6 +602,12 @@ + + + job异常处理 + + + 树形结构构建测试 diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs index 0d1e30ad..e218d8e4 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs @@ -1,4 +1,5 @@ using Hangfire; +using Hangfire.MemoryStorage.Database; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; @@ -221,6 +222,17 @@ namespace Yi.Framework.ApiMicroservice.Controllers return Result.Success("http://localhost:19001/hangfire"); } + /// + /// job异常处理 + /// + /// + [HttpGet] + public async Task ErrorJob() + { + await _quartzInvoker.StartAsync("*/5 * * * * ?", "ErrorJob"); + return Result.Success(); + } + /// /// 树形结构构建测试 /// diff --git a/Yi.Framework.Net6/Yi.Framework.Task/ErrorJob.cs b/Yi.Framework.Net6/Yi.Framework.Task/ErrorJob.cs new file mode 100644 index 00000000..663b74d8 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Task/ErrorJob.cs @@ -0,0 +1,42 @@ +using Microsoft.Extensions.Logging; +using Quartz; +using Quartz.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Common.Models; + +namespace Yi.Framework.Job +{ + public class ErrorJob : IJob + { + private ILogger _logger; + public ErrorJob(ILogger logger) + { + _logger = logger; + } + + public Task Execute(IJobExecutionContext context) + { + try + { + Random random = new Random(); + var p = random.Next(0, 2); + //这里可能会抛出异常 + var o = 1 / p; + } + catch (Exception ex) + { + + JobExecutionException exception = new JobExecutionException(ex); + exception.Source = context.JobDetail.Key.Name; + exception.UnscheduleFiringTrigger = true; + _logger.LogError(exception, $"{exception.Source}错误"); + throw exception; + } + return Task.CompletedTask; + } + } +}