Vipin
Vipin

Reputation: 938

Sending hangfire job to specific queue

I am currently using hangfire to en-queue jobs. The conventional way to en-queue a hangfire job is by using something like this:

 BackgroundJob.Enqueue(() => DoWork());

which will then intern en-queue and execute this job in the "DEFAULT" hangfire queue.

I can however add a attribute to the method which will intern be used to determine which queue it will be placed in and executed:

    [Queue("SECONDARY")]
    public void DoWork()
    {

    }

My question: Is there a way to to dynamically/programmatically en-queue a hangfire job in a specified queue with out using the above mentioned method attribute?

Upvotes: 3

Views: 5296

Answers (1)

Pieter Alberts
Pieter Alberts

Reputation: 879

Here is some pseudo-code for you.

https://api.hangfire.io/html/M_Hangfire_BackgroundJobClient_Create.htm

class Program
{
    static void Main(string[] args)
    {
        EnqueuedState queue = new EnqueuedState("myQueueName");
        new BackgroundJobClient().Create<Program>(c => c.DoWork(), queue);
    }

    public void DoWork()
    {

    }
}

The other option I am aware of is to make use of an interface and indirectly make use of the attribute. See pseudo-code below:

 {
     interface IHangfireJob
     {
         [Queue("secondary")]
         void Execute();
     }
 }

 class Program : IHangfireJob
 {
     static void SomeMainMethod()
     {
        BackgroundJob.Enqueue(() => Execute());
     }

     public void Execute()
     {
        Console.WriteLine("Fire-and-forget!");
     }
 }

Upvotes: 2

Related Questions