From 99c6a0e13e260bb287438f8a37e47de63b0caf3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=86=9B?= <510423039@qq.com> Date: Fri, 28 Jan 2022 13:19:33 +0800 Subject: [PATCH] =?UTF-8?q?=E2=AC=86=EF=B8=8F=20=E5=8D=87=E7=BA=A7Abp5.1.3?= =?UTF-8?q?=20=E2=9C=A8=20Hangfire=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=BF=87=E6=9C=9F=E4=BB=BB=E5=8A=A1=20?= =?UTF-8?q?=E2=9C=A8=20Hangfire=E4=BF=AE=E6=94=B9=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E9=87=8D=E8=AF=95=E6=AC=A1=E6=95=B0=E5=92=8C=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E9=97=B4=E9=9A=94=20=E2=9C=A8=20Hangfire?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=87=8D=E8=AF=95=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aspnet-core/Directory.Build.props | 3 +- .../AbpProHttpApiHostModule.cs | 21 +++++++----- .../AutoDeleteAfterSuccessAttributer.cs | 26 +++++++++++++++ .../{Jobs => Extensions/Hangfire}/CronType.cs | 0 .../CustomHangfireAuthorizeFilter.cs | 0 .../Extensions/Hangfire/JobRetryLastFilter.cs | 28 ++++++++++++++++ .../Extensions/RecurringJobsExtensions.cs | 33 ++++++++++--------- .../Jobs/RecurringJobsExtensions.cs | 17 ---------- .../Lion.AbpPro.HttpApi.Host.csproj | 1 - .../Lion.AbpPro.HttpApi.Host/appsettings.json | 2 +- 10 files changed, 85 insertions(+), 46 deletions(-) create mode 100644 aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/AutoDeleteAfterSuccessAttributer.cs rename aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/{Jobs => Extensions/Hangfire}/CronType.cs (100%) rename aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/{Filters => Hangfire}/CustomHangfireAuthorizeFilter.cs (100%) create mode 100644 aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/JobRetryLastFilter.cs delete mode 100644 aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Jobs/RecurringJobsExtensions.cs diff --git a/aspnet-core/Directory.Build.props b/aspnet-core/Directory.Build.props index 57cd1fe2..ada8a9f0 100644 --- a/aspnet-core/Directory.Build.props +++ b/aspnet-core/Directory.Build.props @@ -1,7 +1,7 @@  - 5.1.1 + 5.1.3 6.0.0 6.0.0 6.0.0-preview.4.21253.7 @@ -19,7 +19,6 @@ 4.1.0 7.0.0 3.3.0 - 3.3.0 8.4.1 13.0.1 7.15.1 diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs index 89642dd9..5a25c66a 100644 --- a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs +++ b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs @@ -24,6 +24,8 @@ using System.Text; using System.Threading.Tasks; using Lion.AbpPro.CAP; using Lion.AbpPro.Extensions.Filters; +using Lion.AbpPro.Extensions.Hangfire; +using Lion.AbpPro.Jobs; using Lion.AbpPro.Shared.Hosting.Microservices; using Lion.AbpPro.Shared.Hosting.Microservices.Microsoft.AspNetCore.Builder; using Lion.AbpPro.Shared.Hosting.Microservices.Swaggers; @@ -64,7 +66,7 @@ namespace Lion.AbpPro public override void OnPostApplicationInitialization( ApplicationInitializationContext context) { - // context.CreateRecurringJob(); + context.CreateRecurringJob(); base.OnPostApplicationInitialization(context); } @@ -109,7 +111,7 @@ namespace Lion.AbpPro app.UseAuditing(); app.UseAbpSerilogEnrichers(); - + app.UseUnitOfWork(); app.UseConfiguredEndpoints(endpoints => { endpoints.MapHealthChecks("/health"); }); app.UseHangfireDashboard("/hangfire", new DashboardOptions() @@ -127,10 +129,7 @@ namespace Lion.AbpPro private void ConfigureHangfireMysql(ServiceConfigurationContext context) { - Configure(options => - { - options.IsJobExecutionEnabled = true; - }); + Configure(options => { options.IsJobExecutionEnabled = true; }); context.Services.AddHangfire(config => { config.UseStorage(new MySqlStorage( @@ -142,7 +141,13 @@ namespace Lion.AbpPro //QueuePollInterval = TimeSpan.Zero, //UseRecommendedIsolationLevel = true, //DisableGlobalLocks = true + JobExpirationCheckInterval = TimeSpan.FromSeconds(3) })); + var delaysInSeconds = new int[] { 10, 60, 60 * 3 }; // 重试时间间隔 + var Attempts = 3; // 重试次数 + config.UseFilter(new AutomaticRetryAttribute() { Attempts = Attempts, DelaysInSeconds = delaysInSeconds }); + config.UseFilter(new AutoDeleteAfterSuccessAttributer(TimeSpan.FromSeconds(5))); + config.UseFilter(new JobRetryLastFilter(Attempts)); }); } @@ -152,11 +157,10 @@ namespace Lion.AbpPro /// private void ConfigurationMiniProfiler(ServiceConfigurationContext context) { - context.Services.AddMiniProfiler(options => options.RouteBasePath = "/profiler") .AddEntityFramework(); } - + /// /// 配置JWT /// @@ -306,7 +310,6 @@ namespace Lion.AbpPro Type = SecuritySchemeType.Http, Scheme = JwtBearerDefaults.AuthenticationScheme, BearerFormat = "JWT" - }); options.AddSecurityRequirement(new OpenApiSecurityRequirement { diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/AutoDeleteAfterSuccessAttributer.cs b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/AutoDeleteAfterSuccessAttributer.cs new file mode 100644 index 00000000..12c7e988 --- /dev/null +++ b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/AutoDeleteAfterSuccessAttributer.cs @@ -0,0 +1,26 @@ +using System; +using Hangfire.Common; +using Hangfire.States; +using Hangfire.Storage; + +namespace Lion.AbpPro.Extensions.Filters; + +public class AutoDeleteAfterSuccessAttributer : JobFilterAttribute, IApplyStateFilter +{ + private readonly TimeSpan _deleteAfter; + + public AutoDeleteAfterSuccessAttributer(TimeSpan timeSpan) + { + _deleteAfter = timeSpan; + } + + public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction) + { + context.JobExpirationTimeout = _deleteAfter; + } + + public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction) + { + + } +} \ No newline at end of file diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Jobs/CronType.cs b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/CronType.cs similarity index 100% rename from aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Jobs/CronType.cs rename to aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/CronType.cs diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Filters/CustomHangfireAuthorizeFilter.cs b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/CustomHangfireAuthorizeFilter.cs similarity index 100% rename from aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Filters/CustomHangfireAuthorizeFilter.cs rename to aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/CustomHangfireAuthorizeFilter.cs diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/JobRetryLastFilter.cs b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/JobRetryLastFilter.cs new file mode 100644 index 00000000..03c4f60b --- /dev/null +++ b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/Hangfire/JobRetryLastFilter.cs @@ -0,0 +1,28 @@ +using Hangfire.Common; +using Hangfire.States; +using Serilog; + +namespace Lion.AbpPro.Extensions.Hangfire; + +/// +/// 重试最后一次 +/// +public class JobRetryLastFilter : JobFilterAttribute, IElectStateFilter +{ + private int RetryCount { get; } + + public JobRetryLastFilter(int retryCount) + { + RetryCount = retryCount; + } + + + public void OnStateElection(ElectStateContext context) + { + int retryAttempt = context.GetJobParameter("RetryCount"); + if (RetryCount == retryAttempt) + { + Log.Error("最后一次重试"); + } + } +} \ No newline at end of file diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/RecurringJobsExtensions.cs b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/RecurringJobsExtensions.cs index d55e94f4..a6cf0f75 100644 --- a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/RecurringJobsExtensions.cs +++ b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Extensions/RecurringJobsExtensions.cs @@ -1,16 +1,17 @@ -// using Hangfire; -// using Microsoft.Extensions.DependencyInjection; -// using System; -// using LionAbpPro.Jobs; -// -// namespace LionAbpPro.Extensions -// { -// public static class RecurringJobsExtensions -// { -// public static void CreateRecurringJob(this IServiceProvider service) -// { -// var job = service.GetService(); -// RecurringJob.AddOrUpdate("测试Job", () => job.ExecuteAsync(), CronTypeHelper.Minute(1)); -// } -// } -// } +using Hangfire; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; + +namespace Lion.AbpPro.Jobs +{ + public static class RecurringJobsExtensions + { + public static void CreateRecurringJob(this ApplicationInitializationContext context) + { + using var scope = context.ServiceProvider.CreateScope(); + var testJob = + scope.ServiceProvider.GetService(); + RecurringJob.AddOrUpdate("测试Job", () => testJob.ExecuteAsync(), CronType.Minute(1)); + } + } +} \ No newline at end of file diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Jobs/RecurringJobsExtensions.cs b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Jobs/RecurringJobsExtensions.cs deleted file mode 100644 index a6cf0f75..00000000 --- a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Jobs/RecurringJobsExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Hangfire; -using Microsoft.Extensions.DependencyInjection; -using Volo.Abp; - -namespace Lion.AbpPro.Jobs -{ - public static class RecurringJobsExtensions - { - public static void CreateRecurringJob(this ApplicationInitializationContext context) - { - using var scope = context.ServiceProvider.CreateScope(); - var testJob = - scope.ServiceProvider.GetService(); - RecurringJob.AddOrUpdate("测试Job", () => testJob.ExecuteAsync(), CronType.Minute(1)); - } - } -} \ No newline at end of file diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Lion.AbpPro.HttpApi.Host.csproj b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Lion.AbpPro.HttpApi.Host.csproj index 3ccf45cf..8076c925 100644 --- a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Lion.AbpPro.HttpApi.Host.csproj +++ b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Lion.AbpPro.HttpApi.Host.csproj @@ -30,7 +30,6 @@ - diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json index 52c30b87..5d403ef2 100644 --- a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json +++ b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json @@ -2,7 +2,7 @@ "Serilog": { "Using": [ "Serilog.Sinks.Console", - "Serilog.Sinks.RollingFile" + "Serilog.Sinks.File" ], "MinimumLevel": { "Default": "Debug",