c# - 如何在所有 Azure 可用实例上分派(dispatch) Hangfire 作业?

标签 c# azure asp.net-web-api azure-web-app-service hangfire

我有一个在 Azure 上运行的 Web Api 应用程序。我每天早上 2:00 运行一个调度程序作业,这会创建大量的hangfire 作业。

目前,Web Api 应用只有一个可用实例,该实例配置为根据 CPU 指标进行横向扩展。这些正在运行的hangfire作业将CPU百分比增加到极限,并创建api应用程序的新的可用实例。

不幸的是,在我看来,给定的hangfire作业在创建它的实例上运行。如何配置 Hangfire 在所有可用实例上分派(dispatch)作业?

最佳答案

Unfortunately, it seems to me a given hangfire job runs on the instance that created it. How can I configure Hangfire to dispatch jobs on all the available instances ?

据我所知,默认情况下该作业将位于default队列中。如果您运行多个服务器实例,作业将分发到可用实例。为了进行测试,我只是添加我的任务,如下所示:

Enumerable.Range(11, 30).ForEach(i =>
{
    BackgroundJob.Enqueue(() => Console.WriteLine($"[Task-{i}] Getting Started with HangFire!"));
});

然后,在控制台应用程序中处理作业,如下所示:

using (var server = new BackgroundJobServer())
{
    Console.WriteLine("Hangfire Server started. Press any key to exit...");
    Console.ReadKey();
}

我启动了两个服务器实例,得到了以下结果:

enter image description here

此外,要配置作业队列,您可以遵循 here 。另外,这里有一个issue这与仅在一个服务器实例上运行作业的问题相反。

关于c# - 如何在所有 Azure 可用实例上分派(dispatch) Hangfire 作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47595352/

相关文章:

azure - 缓存与 Azure Blob 存储的连接

c# - 如何使用 Postman 在 Web API 中调用函数?

c# - 来自代码的 Windows Azure 服务状态

c# - 远程桌面 "can' t 连接到远程计算机”

c# - 如何在附加行为中使用单例计时器来更改 ListView 项目的背景颜色?

azure - Azure 目录中应用程序的回调 URL

c# - 忽略空值 - 序列化

c# - 在 AppHarbor 上的 MVC3 内运行 WCF WebAPI Prev 6,404 错误

c# - 使用 C# 导入 Excel 文档时如何防止前导零被删除

c# - C#中的字符串表