【发布时间】:2017-06-29 17:16:05
【问题描述】:
是否有人使用相同 SQL DB 的多个 Hangfire 实例(在不同的应用程序中)进行配置。因此,我不想为每个 hangfire 实例创建新的 SQL 数据库,而是想与多个实例共享同一个数据库。
根据hangfire documentation here,它从 v1.5 开始受支持但是论坛讨论 here 和 here 显示我们仍然在使用相同 db 运行多个实例时遇到问题
更新 1
所以根据建议和documentation 我配置hangfire 使用队列
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
app.UseHangfireServer(new BackgroundJobServerOptions()
{
Queues = new string[] { "instance1" }
});
}
调用方法
[Queue("instance1")]
public async Task Start(int requestID)
{
}
这就是我排队工作的方式
_backGroundJobClient.Enqueue<IPrepareService>(x => x.Start(request.ID));
但是,当我检查 [JobQueue] 表时,新作业的队列名称为 default,因此,hangfire 将永远不会拾取该作业,因为它会拾取队列的作业。
我认为是一个错误
更新 2
又发现了一件事。我正在使用IBackgroundJobClient 的实例。该实例由 .Net Core 的内置容器自动注入。
因此,如果我使用实例将作业排入队列,那么 hangfire 会使用 default 队列名称创建新作业
_backGroundJobClient.Enqueue<IPrepareService>(x => x.Start(request.ID));
但是,如果我使用静态方法,那么 hangfire 会使用配置的队列名称 instance1 创建新作业
BackgroundJob.Enqueue<IPrepareService>(x => x.Start(prepareRequest.ID));
如何在 .Net Core 中配置 hangfire,以便 IBackgroundJobClient 的实例将使用配置队列名称?
【问题讨论】:
-
您是否尝试过为不同的实例使用不同的队列名称?如果是,还有同样的问题吗?我没有尝试过,但认为对不同的实例使用不同的队列名称不应该混淆。您也可以发布您尝试过的代码吗?
-
这样做没有问题。你有什么问题?
-
不,我还没用过。但这就是混乱。你是说使用不同的队列名称,但hangfire文档说
You aren’t required to have additional configuration to support multiple background processing servers in the same process since Hangfire 1.5, just skip the article. Server identifiers are now generated using GUIDs, so all the instance names are uniquedocs.hangfire.io/en/latest/background-processing/… -
..还有我没有看到的生成的 SQL 表
[Server].[Id]与任何其他表有外键关系。那么hangfire如何识别作业属于特定实例呢? -
我在一个数据库上运行多个实例。无需排队。
标签: hangfire