Files
Yi.Admin/Yi.Abp.Net8/framework/Yi.Framework.BackgroundWorkers.Hangfire/YiFrameworkBackgroundWorkersHangfireModule.cs

84 lines
3.1 KiB
C#
Raw Normal View History

2025-02-07 17:52:38 +08:00
using System.Linq.Expressions;
using Hangfire;
using Microsoft.Extensions.Configuration;
2024-11-19 18:38:58 +08:00
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.BackgroundJobs.Hangfire;
2024-11-15 18:17:53 +08:00
using Volo.Abp.BackgroundWorkers;
using Volo.Abp.BackgroundWorkers.Hangfire;
2025-02-07 17:52:38 +08:00
using Volo.Abp.DynamicProxy;
2024-11-15 18:17:53 +08:00
namespace Yi.Framework.BackgroundWorkers.Hangfire;
/// <summary>
/// Hangfire 后台任务模块
/// </summary>
[DependsOn(typeof(AbpBackgroundWorkersHangfireModule),
typeof(AbpBackgroundJobsHangfireModule))]
public sealed class YiFrameworkBackgroundWorkersHangfireModule : AbpModule
2024-11-15 18:17:53 +08:00
{
/// <summary>
/// 配置服务前的预处理
/// </summary>
/// <param name="context">服务配置上下文</param>
2024-11-15 18:17:53 +08:00
public override void PreConfigureServices(ServiceConfigurationContext context)
{
// 添加 Hangfire 后台任务约定注册器
2024-11-15 18:17:53 +08:00
context.Services.AddConventionalRegistrar(new YiHangfireConventionalRegistrar());
}
/// <summary>
/// 应用程序初始化
/// </summary>
/// <param name="context">应用程序初始化上下文</param>
2024-11-15 18:17:53 +08:00
public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
{
// 获取后台任务管理器和所有 Hangfire 后台任务
2024-11-15 18:17:53 +08:00
var backgroundWorkerManager = context.ServiceProvider.GetRequiredService<IBackgroundWorkerManager>();
var workers = context.ServiceProvider.GetServices<IHangfireBackgroundWorker>();
2024-11-19 18:38:58 +08:00
// 获取配置
2025-02-07 17:52:38 +08:00
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
// 检查是否启用 Redis
var isRedisEnabled = configuration.GetValue<bool>("Redis:IsEnabled");
foreach (var worker in workers)
2024-11-15 18:17:53 +08:00
{
// 设置时区为本地时区(上海)
worker.TimeZone = TimeZoneInfo.Local;
if (isRedisEnabled)
2025-02-07 17:52:38 +08:00
{
// Redis 模式:使用 ABP 后台任务管理器
await backgroundWorkerManager.AddAsync(worker);
2025-02-07 17:52:38 +08:00
}
else
{
// 内存模式:直接使用 Hangfire
var unProxyWorker = ProxyHelper.UnProxy(worker);
// 添加或更新循环任务
RecurringJob.AddOrUpdate(
worker.RecurringJobId,
(Expression<Func<Task>>)(() =>
((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(default)),
worker.CronExpression,
new RecurringJobOptions
2025-02-07 17:52:38 +08:00
{
TimeZone = worker.TimeZone
2025-02-07 17:52:38 +08:00
});
}
2024-11-15 18:17:53 +08:00
}
2024-11-19 18:38:58 +08:00
}
2024-11-15 18:17:53 +08:00
/// <summary>
/// 应用程序初始化前的预处理
/// </summary>
/// <param name="context">应用程序初始化上下文</param>
2024-11-19 18:38:58 +08:00
public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
{
// 添加工作单元过滤器
2024-11-19 18:38:58 +08:00
var services = context.ServiceProvider;
GlobalJobFilters.Filters.Add(services.GetRequiredService<UnitOfWorkHangfireFilter>());
2024-11-15 18:17:53 +08:00
}
}