You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
2.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
/// <summary>
/// 注入Hangfire服务
/// </summary>
public static class WorkServiceHangfireModuleInstall
{
/// <summary>
///
/// </summary>
/// <param name="services"></param>
/// <exception cref="ArgumentNullException"></exception>
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();
}
}