Enqueue with specified queue name

I’m trying to enqueue a job and override the queue that is set in the attribute
ie BackgroundJob.Enqueue(“new_queue”, x => x.Execute(null, x, y))

and I’m getting the error:
System.NotSupportedException: Current storage doesn’t support specifying queues directly for a specific job. Please use the QueueAttribute instead.

We’re currently using Hangfire.Pro.Redis v2.6.2.0. Is this still a limitation? or perhaps is available in a newer version?

Thanks,

Yes, storage support is required here, and your package is pretty outdated. You can just update to Hangfire.Pro.Redis of the latest version, please see Release Notes for Hangfire Pro — Hangfire for the full change log.

Hi Sergey, thanks for the response.

Although, I no longer get the error after upgrading, it’s still not doing what I expected.

I have a method:

[Queue(“queue_a”)]
public interface ISomeMethod {
void Execute(…)
}

But when I enqueue it I want to essentially override which queue it gets processed on:

var id = BackgroundJob.Enqueue<ISomeMethod>("queue_a_canary", x => x.Execute(...));

It seems no matter what I do, it still gets enqueued to the queue_a queue.
The idea is to support a “stable” and a “canary” queue with the jobs being sent dynamically to the appropriate queues.
Is this supported or am I just doing something wrong?

Thanks,

The Queue filter attribute defines an override, and not the default queue. This is a regular extension filter that affects the basic processing pipeline, and isn’t meant to set any defaults. My recommendation here is to either define the queue name when calling the Enqueue method, or create a custom attribute that implements routing, for example, based on argument value:

public sealed class CustomQueueAttribute : JobFilterAttribute, IElectStateFilter
{
    public string DefaultQueue { get; set; }

    public void OnStateElection(ElectStateContext context)
    {
        if (context.CandidateState is EnqueuedState enqueuedState)
        {
            enqueuedState.Queue = context.BackgroundJob.Job.Args[0] == "AnotherTenant"
                ? "another-tenant"
                : DefaultQueue;
        }
    }
}
1 Like