From e960db0d3e2afb48d4a9c8fe846a37539083b831 Mon Sep 17 00:00:00 2001 From: chenchun Date: Tue, 19 Nov 2024 18:38:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90hangfire=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=B7=A5=E4=BD=9C=E5=8D=95=E5=85=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UnitOfWorkHangfireFilter.cs | 45 +++++++++++++++++++ ...rameworkBackgroundWorkersHangfireModule.cs | 14 ++++-- Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs | 2 +- 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 Yi.Abp.Net8/framework/Yi.Framework.BackgroundWorkers.Hangfire/UnitOfWorkHangfireFilter.cs diff --git a/Yi.Abp.Net8/framework/Yi.Framework.BackgroundWorkers.Hangfire/UnitOfWorkHangfireFilter.cs b/Yi.Abp.Net8/framework/Yi.Framework.BackgroundWorkers.Hangfire/UnitOfWorkHangfireFilter.cs new file mode 100644 index 00000000..47748c22 --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.BackgroundWorkers.Hangfire/UnitOfWorkHangfireFilter.cs @@ -0,0 +1,45 @@ +using Hangfire.Server; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Threading; +using Volo.Abp.Uow; + +namespace Yi.Framework.BackgroundWorkers.Hangfire; + +public class UnitOfWorkHangfireFilter : IServerFilter, ISingletonDependency +{ + private const string CurrentJobUow = "HangfireUnitOfWork"; + private readonly IUnitOfWorkManager _unitOfWorkManager; + + public UnitOfWorkHangfireFilter(IUnitOfWorkManager unitOfWorkManager) + { + _unitOfWorkManager = unitOfWorkManager; + } + + public void OnPerforming(PerformingContext context) + { + var uow = _unitOfWorkManager.Begin(); + context.Items.Add(CurrentJobUow, uow); + } + + public void OnPerformed(PerformedContext context) + { + AsyncHelper.RunSync(()=>OnPerformedAsync(context)); + } + + private async Task OnPerformedAsync(PerformedContext context) + { + if (context.Items.TryGetValue(CurrentJobUow, out var obj) + && obj is IUnitOfWork uow) + { + if (context.Exception == null && !uow.IsCompleted) + { + await uow.CompleteAsync(); + } + else + { + await uow.RollbackAsync(); + } + uow.Dispose(); + } + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.BackgroundWorkers.Hangfire/YiFrameworkBackgroundWorkersHangfireModule.cs b/Yi.Abp.Net8/framework/Yi.Framework.BackgroundWorkers.Hangfire/YiFrameworkBackgroundWorkersHangfireModule.cs index d5378d3f..fce1a566 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.BackgroundWorkers.Hangfire/YiFrameworkBackgroundWorkersHangfireModule.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.BackgroundWorkers.Hangfire/YiFrameworkBackgroundWorkersHangfireModule.cs @@ -1,11 +1,12 @@ -using Microsoft.Extensions.DependencyInjection; +using Hangfire; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.BackgroundWorkers; using Volo.Abp.BackgroundWorkers.Hangfire; namespace Yi.Framework.BackgroundWorkers.Hangfire; [DependsOn(typeof(AbpBackgroundWorkersHangfireModule))] -public class YiFrameworkBackgroundWorkersHangfireModule:AbpModule +public class YiFrameworkBackgroundWorkersHangfireModule : AbpModule { public override void PreConfigureServices(ServiceConfigurationContext context) { @@ -17,13 +18,18 @@ public class YiFrameworkBackgroundWorkersHangfireModule:AbpModule //定时任务自动注入,Abp默认只有在Quartz才实现 var backgroundWorkerManager = context.ServiceProvider.GetRequiredService(); var works = context.ServiceProvider.GetServices(); - + foreach (var work in works) { //如果为空,默认使用服务器本地utc时间 - work.TimeZone = work.TimeZone ?? TimeZoneInfo.Local; + work.TimeZone ??= TimeZoneInfo.Local; await backgroundWorkerManager.AddAsync(work); } + } + public override void OnPreApplicationInitialization(ApplicationInitializationContext context) + { + var services = context.ServiceProvider; + GlobalJobFilters.Filters.Add(services.GetRequiredService()); } } \ No newline at end of file diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs index 35106cc5..50211fa2 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs @@ -180,7 +180,7 @@ namespace Yi.Abp.Web //配置Hangfire定时任务存储,开启redis后,优先使用redis var redisConfiguration = configuration["Redis:Configuration"]; var redisEnabled = configuration["Redis:IsEnabled"]; - context.Services.AddHangfire(config => + context.Services.AddHangfire(config=> { if (redisEnabled.IsNullOrEmpty() || bool.Parse(redisEnabled)) {