Hangfire 具有多个数据库并定义队列名称

问题描述 投票:0回答:1

我有一个 .NET Core 6 Hangfire 作业项目,并且我使用两个数据库,这没有问题。但是两个仪表板具有相同的队列名称; “默认”。

我需要将每个服务器定义为不同的队列名称,但我不能。我需要帮助。

这是我的代码:

var hangfireUserStorageOptions = new SqlServerStorageOptions
{
    CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
    SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
    QueuePollInterval = TimeSpan.Zero,
    UseRecommendedIsolationLevel = true,
    DisableGlobalLocks = true,
    SchemaName = "HangFireUser"
};

var hangfireSystemStorageOptions = new SqlServerStorageOptions
{
    CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
    SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
    QueuePollInterval = TimeSpan.Zero,
    UseRecommendedIsolationLevel = true,
    DisableGlobalLocks = true,
    SchemaName = "HangFireSystem"
};

var hangfireUserStorage = new SqlServerStorage(_configuration.GetSection("HangfireSettings:HangfireConnection").Value, hangfireUserStorageOptions);
var hangfireSystemStorage = new SqlServerStorage(_configuration.GetSection("HangfireSettings:HangfireConnection").Value, hangfireSystemStorageOptions);

builder.Services.AddHangfire(configuration => configuration
    .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
    .UseSimpleAssemblyNameTypeSerializer()
    .UseRecommendedSerializerSettings()
    .UseSqlServerStorage(_configuration.GetSection("HangfireSettings:HangfireConnection").Value, hangfireSystemStorageOptions));

builder.Services.AddHangfire(configuration => configuration
    .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
    .UseSimpleAssemblyNameTypeSerializer()
    .UseRecommendedSerializerSettings()
    .UseSqlServerStorage(_configuration.GetSection("HangfireSettings:HangfireConnection").Value, hangfireUserStorageOptions));

builder.Services.AddHangfireServer(hangfireSystemStorage);   // I need to define queue name "Q1"
builder.Services.AddHangfireServer(hangfireUserStorage);     // I need to define queue name "Q2" 
c# asp.net-core asp.net-core-webapi hangfire
1个回答
0
投票

要实现此目的,您应该在作业方法上设置

QueueAttribute
,然后为这些作业指定所需的队列名称。

 [Queue("Q1")]
这样,您就可以显式地设置作业的队列名称。当您使用不同的队列名称对作业进行排队时,
Hangfire
将确保作业由配置了相应队列名称的服务器处理。 当您将作业入队时,请务必指定相应的队列名称

© www.soinside.com 2019 - 2024. All rights reserved.