namespace DS.Module.HangfireModule; using DS.Module.Core; using DS.Module.Core.Extensions; using Hangfire; using Hangfire.HttpJob; using Hangfire.MySql; using Microsoft.Extensions.DependencyInjection; using System.Linq; using System.Transactions; /// /// 注入Hangfire服务 /// public static class WorkServiceHangfireModuleInstall { /// /// /// /// /// public static void AddWorkServiceHangfireModuleInstall(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); services.AddHangfire(configuration => configuration .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) //设置Hangfire 存储的数据兼容性级别为 Version_170 .UseSimpleAssemblyNameTypeSerializer()//使用简单的程序集名称类型序列化器。这是一种用于序列化和反序列化 Hangfire 任务数据的方法 .UseRecommendedSerializerSettings()//使用推荐的序列化器设置。这将使用推荐的序列化器选项进行配置 .UseStorage(new MySqlStorage( AppSetting.app(new string[] { "HangfireSettings", "DbString" }), new MySqlStorageOptions { TransactionIsolationLevel = IsolationLevel.ReadCommitted,// 事务隔离级别。默认是读取已提交。 QueuePollInterval = TimeSpan.FromSeconds(15), //- 作业队列轮询间隔。默认值为15秒。 JobExpirationCheckInterval = TimeSpan.FromHours(1), //- 作业到期检查间隔(管理过期记录)。默认值为1小时。 CountersAggregateInterval = TimeSpan.FromMinutes(5), //- 聚合计数器的间隔。默认为5分钟。 PrepareSchemaIfNecessary = true, //- 如果设置为true,则创建数据库表。默认是true。 DashboardJobListLimit = 50000,//- 仪表板作业列表限制。默认值为50000。 TransactionTimeout = TimeSpan.FromMinutes(1), //- 交易超时。默认为1分钟。 TablesPrefix = "Hangfire" }))); services.AddHangfireServer(options => { options.WorkerCount = AppSetting.app(new string[] { "HangfireSettings", "WorkerCount" }).ToInt(); options.ServerName = AppSetting.app(new string[] { "HangfireSettings", "ServerName" }); options.Queues = AppSetting.app(new string[] { "HangfireSettings", "Queues" }).Split(','); }); GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 5 }); services.AddHangfireServer(); } }