Search code examples
hangfire

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


This documentation says that you can specify a queue by using the Queue attribute on the method to be invoked. This assumes that you always want execute a method on the same queue. Is there a way for the process that calls Enqueue to specify the name of the queue to put the job into (effectively putting the decision-making in the hands of the job generator, not the definition of the job).


Solution

  • With a instance of IBackgroundJobClient 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 in this way, a retry will put the job back to the default queue. You will require additional code to retry in the same queue, using a JobFilter

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