简体   繁体   English

Hangfire如何为Web场工作?

[英]How does Hangfire work for a web farm?

I'm trying to understand exactly how Hangfire will behave if used in a web farm, where each ASP.NET application is configured identically, and there are N instances using the same shared SQL Server database for Hangfire storage. 我试图准确理解如果在Web场中使用Hangfire将如何表现,其中每个ASP.NET应用程序配置相同,并且有N个实例使用相同的共享SQL Server数据库进行Hangfire存储。

The documentation just says that distributed locks are used to prevent race conditions, but this is a bit low-level, I need to understand what this means in practice. 文档只是说分布式锁用于防止竞争条件,但这有点低级,我需要了解这在实践中意味着什么。

Example: 例:

If I have 5 web server instances, and I create a background job with a schedule that will run once a day at 5pm, does this mean that the first instance to obtain a 'lock' on the job will end up running it, and all other instances will ignore the job while it is locked? 如果我有5个Web服务器实例,并且我创建了一个后台作业,其日程安排将在每天下午5点运行一次,这是否意味着在作业上获得“锁定”的第一个实例将最终运行它,并且所有其他实例会在锁定时忽略该作业?

I'm assuming that Hangfire will only allow one instance to process a job at a time, but I haven't confirmed it. 我假设Hangfire一次只允许一个实例处理一个作业,但我还没有确认。

What about if I actually wanted to run a job on each server instance at the same time? 如果我真的想同时在每个服务器实例上运行一个作业呢?

If anyone has any practical experience with Hangfire in a web farm, I'm all ears. 如果有人在网络农场有任何Hangfire的实践经验,我会全力以赴。

These are the basic rules that I've established after more research and testing: 这些是我在经过更多研究和测试后建立的基本规则:

  • Hangfire will execute a background job on the first Hangfire server that has available capacity based on the number of worker threads on that server Hangfire将在第一个Hangfire服务器上执行后台作业,该服务器具有基于该服务器上的工作线程数的可用容量

  • Hangfire will continue to execute background jobs on that server until the worker pool is saturated, at which point it will move to the next available server in the farm, and so on Hangfire将继续在该服务器上执行后台作业,直到工作池达到饱和状态,此时它将移动到服务器场中的下一个可用服务器,依此类推

  • Hangfire servers are automatically included in a web farm if they use the same Hangfire storage instance, so generally speaking no extra configuration is required. 如果Hangfire服务器使用相同的Hangfire存储实例,它们将自动包含在Web场中,因此通常不需要额外的配置。

  • If you want to run specific background jobs on specific servers or use different workload distribution algorithms, you can use named queues, where a queue is given a name and potentially assigned to a specific server instance, and background jobs must be scheduled to run on that queue as well. 如果要在特定服务器上运行特定后台作业或使用不同的工作负载分配算法,则可以使用命名队列,其中队列具有名称并可能分配给特定服务器实例,后台作业必须安排在该队列上运行排队也是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM