In HangFire, can I run with the queue name instead of using the Queue attribute?

This documentation states that you can specify a queue using an attribute Queue

on the method being called. This assumes that you always want to execute a method in the same queue. Is there a way for the process that calls Enqueue

to specify the name of the queue to enter the job (effectively putting the solution in the hands of the job generator, not the job definition).

+4


source to share


2 answers


With an IBackgroundJobClient instance, you can specify a queue.

IBackgroundJobClient hangFireClient = new BackgroundJobClient();
EnqueuedState myQueueState = new Hangfire.States.EnqueuedState("myQueue");
hangFireClient.Create<SomeClass>(c => c.SomeMethod(), myQueueState);

      



Note that retrying the job this way will return the job to the default queue. You need some extra code to retry on the same queue using JobFilter

http://discuss.hangfire.io/t/one-queue-for-the-whole-farm-and-one-queue-by-server/490/3

+9


source


Since adding an extra parameter seems to be very difficult for the Hangfire command; -) ....

... I've found that the most convenient way is to create two methods that just call the actual implementation and put different attributes [Queue]

on each.

Usually if I need to switch queues between dev / production and I just want to call something like RunOrder(...)

c isTestOrder=boolean

and not worry about queues at that level.



public void RunOrder(int orderId, bool isTestOrder) 
{
   if (isTestOrder) 
   {
      BackgroundJob.Enqueue(() => _RunTestOrder(orderId));
   } 
   else 
   {
      BackgroundJob.Enqueue(() => _RunOrder(orderId));
   }
}

[Queue("dev")]
public void _RunTestOrder(int orderId) {
  OrderProcessor.RunOrder(orderId); // actual code to call processor
}

[Queue("production")]'
public void _RunProductionOrder(int orderId) {
  OrderProcessor.RunOrder(orderId); // is the same in both 'hangfire proxies'
}

      

Note the usage _

to indicate that they should not be called directly. I don't remember if hangfire methods should be public or not, but if they should be, then _

more important.

0


source







All Articles