简体   繁体   English

如何在队列中的作业之间添加 2 分钟的延迟?

[英]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.

相关问题 如何在两个计时器之间创建延迟? - How to create delay between two timers? 每个会话的时间延迟队列 - Delay Queue for amount of time per session 如何使用 RabbitMQ 将两个进程添加到单独的队列中 - How to add two process into separate queue using RabbitMQ 为 Azure Message Queue 的现有 CloudQueueMessage 扩展 initialVisibility Delay - Extend the initialVisibility Delay for an existing CloudQueueMessage for Azure Message Queue 如何在Task.Delay的.net Framework 4和.net标准1.5之间切换上下文 - How to switch context between .net framework 4 and .net standard 1.5 for Task.Delay 请求和操作之间的 Asp.net 核心中间件路由延迟 - Asp.net core middleware routing delay between request and action ASP.NET Core:两次调用之间的空闲超时有延迟 - ASP.NET Core: Idle timeout between the calls with a delay Azure 中带有 ICollector 和 QueueClient 的 Queue 属性有什么区别? - What is the difference between Queue attribute with ICollector and QueueClient in Azure? 如何在hangfire中停止和删除所有正在处理的后台作业? - How to stop and delete all processing background jobs in hangfire? 如何在 Quartz.net 中注册 N 个 Job - How do I register N Jobs in Quartz.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM