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",