gpt4 book ai didi

azure - 使用 Hangfire 和 Azure 服务总线在 1 分钟后重试作业

转载 作者:行者123 更新时间:2023-12-03 00:12:58 24 4
gpt4 key购买 nike

我正在使用由 SQL Server 支持的 Hangfire 和 Azure 服务总线

我的 Hangfire 服务器配置如下所示:

var hangfireServiceBusQueuePrefix = ConfigurationManager.AppSettings["HangfireServiceBusQueuePrefix"];
var azureServiceBusConnectionString = ConfigurationManager.ConnectionStrings["AzureServiceBus"].ConnectionString;

var serviceBusQueueOptions = new ServiceBusQueueOptions
{
ConnectionString = azureServiceBusConnectionString,

Queues = new[] { EnqueuedState.DefaultQueue },

QueuePrefix = (hangfireServiceBusQueuePrefix.Equals("MachineName")
? Environment.MachineName
: hangfireServiceBusQueuePrefix)
+ "-hangfire-"
};

var storage = new SqlServerStorage("Hangfire")
.UseServiceBusQueues(serviceBusQueueOptions);

_hangfireConfiguration = GlobalConfiguration.Configuration
.UseStorage(storage);

我注意到,如果我的作业在 1 分钟多一点后还没有完成,它就会被重播 - 即使原始作业仍在运行......

例如,当我对这样的作业进行排队时:

BackgroundJob.Enqueue(MyMethod);

如果我们想象 MyMethod 看起来像这样

public void MyMethod()
{
Console.WriteLine("MyMethod Started");
Thread.Sleep(TimeSpan.FromSeconds(90));
Console.WriteLine("MyMethod Finished");
}

...发生的情况是,1 分钟后,该方法将“重新启动”30 秒后,第一个实例将完成,然后第二个实例将在 60 秒后完成。

我需要完成这项工作,而不是在一分钟后重新尝试

最佳答案

当您收到消息时,消息会被锁定一段时间。这是为了避免其他接收者收到相同的消息。

您可以配置LockDuration当您创建队列时。来自文档:

The maximum value for LockDuration is 5 minutes; the default value is 1 minute.

如果您需要锁定消息超过5分钟,可以使用RenewLock手动增加锁定。方法..

关于azure - 使用 Hangfire 和 Azure 服务总线在 1 分钟后重试作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39897128/

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