2024-11-19 18:38:58 +08:00
|
|
|
|
using Hangfire;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-11-15 18:17:53 +08:00
|
|
|
|
using Volo.Abp.BackgroundWorkers;
|
|
|
|
|
|
using Volo.Abp.BackgroundWorkers.Hangfire;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.BackgroundWorkers.Hangfire;
|
|
|
|
|
|
|
|
|
|
|
|
[DependsOn(typeof(AbpBackgroundWorkersHangfireModule))]
|
2024-11-19 18:38:58 +08:00
|
|
|
|
public class YiFrameworkBackgroundWorkersHangfireModule : AbpModule
|
2024-11-15 18:17:53 +08:00
|
|
|
|
{
|
|
|
|
|
|
public override void PreConfigureServices(ServiceConfigurationContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Services.AddConventionalRegistrar(new YiHangfireConventionalRegistrar());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
//定时任务自动注入,Abp默认只有在Quartz才实现
|
|
|
|
|
|
var backgroundWorkerManager = context.ServiceProvider.GetRequiredService<IBackgroundWorkerManager>();
|
|
|
|
|
|
var works = context.ServiceProvider.GetServices<IHangfireBackgroundWorker>();
|
2024-11-19 18:38:58 +08:00
|
|
|
|
|
2024-11-15 18:17:53 +08:00
|
|
|
|
foreach (var work in works)
|
|
|
|
|
|
{
|
2024-11-16 11:01:06 +08:00
|
|
|
|
//如果为空,默认使用服务器本地utc时间
|
2024-11-19 18:38:58 +08:00
|
|
|
|
work.TimeZone ??= TimeZoneInfo.Local;
|
2024-11-15 18:17:53 +08:00
|
|
|
|
await backgroundWorkerManager.AddAsync(work);
|
|
|
|
|
|
}
|
2024-11-19 18:38:58 +08:00
|
|
|
|
}
|
2024-11-15 18:17:53 +08:00
|
|
|
|
|
2024-11-19 18:38:58 +08:00
|
|
|
|
public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var services = context.ServiceProvider;
|
|
|
|
|
|
GlobalJobFilters.Filters.Add(services.GetRequiredService<UnitOfWorkHangfireFilter>());
|
2024-11-15 18:17:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|