[英]How to add 2 minutes delay between jobs in a queue?
I am using Hangfire in ASP.NET Core with a server that has 20 workers, which means 20 jobs can be enqueued at the same time.我在 ASP.NET 核心中使用 Hangfire,服务器有 20 个工作人员,这意味着可以同时排队 20 个作业。
What I need is to enqueue them one by one with 2 minutes delay between each one and another.我需要的是将它们一个接一个地排队,每个队列之间有 2 分钟的延迟。 Each job can take 1-45 minutes, but I don't have a problem running jobs concurrently, but I do have a problem starting 20 jobs at the same time.每个作业可能需要 1-45 分钟,但同时运行作业没有问题,但同时启动 20 个作业时确实有问题。 That's why changing the worker count to 1 is not practical for me (this will slow the process a lot).这就是为什么将工人数更改为 1 对我来说不切实际(这会大大减慢进程)。
The idea is that I just don't want 2 jobs to run at the same second since this may make some conflicts in my logic, but if the second job started 2 minutes after the first one, then I am good.我的想法是我只是不希望 2 个作业同时运行,因为这可能会在我的逻辑中产生一些冲突,但是如果第二个作业在第一个作业之后 2 分钟开始,那么我很好。
How can I achieve that?我怎样才能做到这一点?
You can use BackgroundJob.Schedule()
to run your job run at a specific time:您可以使用BackgroundJob.Schedule()
在特定时间运行作业:
BackgroundJob.Schedule(() => Console.WriteLine("Hello"), dateTimeToExecute);
Based on that set a date for the first job to execute, and then increase this date to 2 minutes for each new job.在此基础上,为第一个作业设置一个执行日期,然后为每个新作业将此日期增加到 2 分钟。
Something like this:是这样的:
var dateStartDate = DateTime.Now;
foreach (var j in listOfjobsToExecute)
{
BackgroundJob.Schedule(() => j.Run(), dateStartDate);
dateStartDate = dateStartDate.AddMinutes(2);
}
See more here: https://docs.hangfire.io/en/latest/background-methods/calling-methods-with-delay.html?highlight=delay在此处查看更多信息: https://docs.hangfire.io/en/latest/background-methods/calling-methods-with-delay.html?highlight=delay
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.