Blame view

Yi.Abp.Net8/framework/Yi.Framework.BackgroundWorkers.Hangfire/YiFrameworkBackgroundWorkersHangfireModule.cs 3.08 KB
515fceeb   “wangming”   框架初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  using System.Linq.Expressions;
  using Hangfire;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Volo.Abp.BackgroundJobs.Hangfire;
  using Volo.Abp.BackgroundWorkers;
  using Volo.Abp.BackgroundWorkers.Hangfire;
  using Volo.Abp.DynamicProxy;
  
  namespace Yi.Framework.BackgroundWorkers.Hangfire;
  
  /// <summary>
  /// Hangfire 后台任务模块
  /// </summary>
  [DependsOn(typeof(AbpBackgroundWorkersHangfireModule),
      typeof(AbpBackgroundJobsHangfireModule))]
  public sealed class YiFrameworkBackgroundWorkersHangfireModule : AbpModule
  {
      /// <summary>
      /// 配置服务前的预处理
      /// </summary>
      /// <param name="context">服务配置上下文</param>
      public override void PreConfigureServices(ServiceConfigurationContext context)
      {
          // 添加 Hangfire 后台任务约定注册器
          context.Services.AddConventionalRegistrar(new YiHangfireConventionalRegistrar());
      }
  
      /// <summary>
      /// 应用程序初始化
      /// </summary>
      /// <param name="context">应用程序初始化上下文</param>
      public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
      {
          // 获取后台任务管理器和所有 Hangfire 后台任务
          var backgroundWorkerManager = context.ServiceProvider.GetRequiredService<IBackgroundWorkerManager>();
          var workers = context.ServiceProvider.GetServices<IHangfireBackgroundWorker>();
  
          // 获取配置
          var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
          
          // 检查是否启用 Redis
          var isRedisEnabled = configuration.GetValue<bool>("Redis:IsEnabled");
  
          foreach (var worker in workers)
          {
              // 设置时区为本地时区(上海)
              worker.TimeZone = TimeZoneInfo.Local;
  
              if (isRedisEnabled)
              {
                  // Redis 模式:使用 ABP 后台任务管理器
                  await backgroundWorkerManager.AddAsync(worker);
              }
              else
              {
                  // 内存模式:直接使用 Hangfire
                  var unProxyWorker = ProxyHelper.UnProxy(worker);
                  
                  // 添加或更新循环任务
                  RecurringJob.AddOrUpdate(
                      worker.RecurringJobId,
                      (Expression<Func<Task>>)(() => 
                          ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(default)),
                      worker.CronExpression,
                      new RecurringJobOptions
                      {
                          TimeZone = worker.TimeZone
                      });
              }
          }
      }
  
      /// <summary>
      /// 应用程序初始化前的预处理
      /// </summary>
      /// <param name="context">应用程序初始化上下文</param>
      public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
      {
          // 添加工作单元过滤器
          var services = context.ServiceProvider;
          GlobalJobFilters.Filters.Add(services.GetRequiredService<UnitOfWorkHangfireFilter>());
      }
  }