使用ContinueWith时,Hangfire中的排队儿童工作有什么优先级?

时间:2016-08-18 08:56:23

标签: c# .net background-process hangfire

我的应用程序正在使用Hangfire进行后台作业处理。 我按如下方式创建后台作业:

var parentJobId = _backgroundJobClient.Enqueue<IMyService>(x => x.ParentMethod(id));
_backgroundJobClient.ContinueWith<IMyService>(parentJobId, x => x.ChildMethod(id));

_backgroundJobClient.Enqueue<IMyService>(x => x.OtherMethod1(id));
_backgroundJobClient.Enqueue<IMyService>(x => x.OtherMethod2(id));

这些方法在服务中定义如下:

public interface IMyService
{
    [Queue(HangfireQueuePriority.Default)]
    void ParentMethod(int id);

    [Queue(HangfireQueuePriority.Default)]
    void ChildMethod(int id);       

    [Queue(HangfireQueuePriority.Default)]
    void OtherMethod1(int id);  

    [Queue(HangfireQueuePriority.Critical)]
    void OtherMethod2(int id);      
}

我希望在父作业完成后ChildMethod立即运行。 我对ContinueWith的理解是,子作业在父作业之后运行,但是在Hangfire文档中没有指定子作业运行的时间。

我的问题是,子任务的“优先级”与队列中的其他任务相比是什么? 队列中定义的任何任务是否有可能在ParentMethod和ChildMethod之间运行?例如OtherMethod1或OtherMethod2

2 个答案:

答案 0 :(得分:0)

  

我的问题是,儿童任务的“优先级”是什么?   队列中的任务?

无论并发级别如何,

All jobs in critical queues will be fetched first before default queues

  

队列中定义的任何任务是否有可能在其间运行   ParentMethod和ChildMethod?例如OtherMethod1或   OtherMethod2

从技术上讲,根据我对您的第一个队列的第一个答案,将首先获取关键队列中的所有作业,其中包括您的OtherMethod2。现在所有剩余的作业都在Defaults Queue,默认情况下,Hangfire设置为在Worker的可用数量的基础上同时执行作业。这意味着OtherMethod1可以在ParentMethodChildMethod之间运行,因为它们都在Default Queues下。

答案 1 :(得分:0)

对于这个问题,我使用了2个hangfire服务器。 第一个是使用像这样的队列“继续,默认” 第二个像这样“默认,继续”

你的继续必须在继续队列中。

因此,当一个任务进入继续队列时,如果第一个被占用,第二个服务器就会接受它,即使它被默认作业占用。

我使用这个是因为当我有一个包含数千个入队和数千个继续的批处理时,我希望这两个队列并行运行。

相关问题