Can you put reoccuring jobs in a specific queue

I have a hangfire server with 2 queue’s. And i want to tell a reoccuring job to run in a specific queue.
Is this possible?

You can do this statically – just apply the QueueAttribute to your background job method:

[Queue("critical")]
public void SomeCriticalMethod() { }

[Queue("default")]
public void RegularMethod() { }

Or dynamically, by creating a state election filter:

public class QueueRouterFilterAttribute : JobFilterAttribute, IElectStateFilter
{
    public void OnStateElection(ElectStateContext context)
    {
        var enqueuedState = context.CandidateState as EnqueuedState;
        if (enqueuedState != null)
        {
            if (context.Job.Type == typeof (Console))
            {
                enqueuedState.Queue = "critical";
            }
        }
    }
}

And then registering it to the global job filter collection in Startup.cs or Global.asax.cs (place it near Hangfire Server initialization). Please note that in this case your filter will apply to all background jobs.

GlobalJobFilters.Filters.Add(new QueueFilterAttribute());
1 Like

Sergei,

am i right that this method will work only for Fire and forget tasks like
BackgroundJob.Enqueue(() => Console.WriteLine(“Simple!”));

but not for Recurring tasks:

RecurringJob.AddOrUpdate(() => Console.WriteLine(“Transparent!”), Cron.Daily);

? I have two apps pointing the same Hangfire db, each app has its own queue, and recurrent tasks, and each app trying to execute recurrent tasks of other app. Hope all of these make sense to you.

thanks,
Mikhail

Has this issue been resolved?

I would like to set a particular job’s state to be in the Awaiting queue once it’s finished.
I’ve tried applying the QueueAttribute on the job method and also have tried creating a global filter.
None of these methods work. The job doesn’t get run.
The job is a recurring job.

Instead of:
enqueuedState.Queue = “critical”;

I have:
enqueuedState.Queue = AwaitingState.StateName;

When I step over this line, nothing happens. The job doesn’t get run either.

Please advise if this is an actual bug and if so, is there a workaround for it?

Thank you