gpt4 book ai didi

hangfire - 将多个hangfire实例与单个数据库一起使用

转载 作者:行者123 更新时间:2023-12-02 09:18:22 49 4
gpt4 key购买 nike

有没有人使用过 Hangfire 的多个实例? (在不同的应用程序中)使用相同的 SQL DB 进行配置。因此,我不想为每个 hangfire 实例创建新的 SQL 数据库,而是想与多个实例共享同一个数据库。

根据hangfire documentation here从 v1.5 开始支持但是论坛讨论 herehere表明我们仍然在使用相同的数据库运行多个实例时遇到问题

更新 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 的内置容器自动注入(inject)。

因此,如果我使用实例将作业排入队列,则 hangfire 会使用 default 创建新作业队列名称
 _backGroundJobClient.Enqueue<IPrepareService>(x => x.Start(request.ID));

但是,如果我使用静态方法,那么 hangfire 会使用配置的队列名称创建新作业 instance1
  BackgroundJob.Enqueue<IPrepareService>(x => x.Start(prepareRequest.ID));

如何在 .Net Core 中配置 hangfire,以便 IBackgroundJobClient 的实例将使用配置队列名称?

最佳答案

这可以通过简单地为每个实例设置具有不同架构名称的 SQL 服务器选项来实现。

实例 1:

configuration.UseSqlServerStorage(
configuration.GetConnectionString("Hangfire"),
new SqlServerStorageOptions { SchemaName = "PrefixOne" }
);

实例 2:
configuration.UseSqlServerStorage(
configuration.GetConnectionString("Hangfire"),
new SqlServerStorageOptions { SchemaName = "PrefixTwo" }
);

两个实例都使用相同的连接字符串,并将使用设置中指定的前缀创建所有必需表的两个实例。

队列用于在同一个 Hangfire 实例中拥有不同的队列。如果要使用不同的队列,则需要指定希望 IBackgroundJobClient 监听的队列,然后在创建作业时指定该队列。这听起来不像你想要完成的事情。

关于hangfire - 将多个hangfire实例与单个数据库一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44831338/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com