André Haupt
André Haupt

Reputation: 3524

AspNet Boilerplate: Hangfire Queues

I'm trying to use Hangfire Queues in my AspNet Zero project.

I configured the backend to only process specific queues as illustrated in this example:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHangfireServer(options =>
    {
        options.Queues = new[] { "alpha", "beta", "default" };
    });
}

However, applying the QueueAttribute to the Execute() method, does not seem to specify which queue the job should be processed on. The entry in the Hangfire database is always default.

public class TestJob : BackgroundJob<int>, ITransientDependency
{
    [Queue("alpha")]
    public override void Execute(int number)
    {
        // Do something
    }
}

The job is enqueued according to the ABP documentation:

public class MyService
{
    private readonly IBackgroundJobManager _backgroundJobManager;

    public MyService(IBackgroundJobManager backgroundJobManager)
    {
        _backgroundJobManager = backgroundJobManager;
    }

    public void Test()
    {
        _backgroundJobManager.Enqueue<TestJob, int>(42);
    }
}

How would one specify the job queue when enqueuing it?

Upvotes: 0

Views: 989

Answers (1)

Andr&#233; Haupt
Andr&#233; Haupt

Reputation: 3524

One cannot specify the queue name using ABP as the framework will use dependency injection to resolve the type IBackgroundJob<TArgs> int the HangfireBackgroundJobManager.

When using DI, the interface must be decorated with the [Queue("your_queue"] attribute, and we don't have access to the IBackgroundJob interface at this level for obvious reasons.

I solved this by using Hangfire directly. The code:

public interface ITestJob : ITransientDependency
{
    [Queue("alpha")]
    public void Execute(int number);
}
public class TestJob : ITestJob, ITransientDependency
{
    public void Execute(int number)
    {
        // Do something
    }
}
using Hangfire;

public class MyService
{
    public MyService()
    {
    }

    public void Test()
    {
        BackgroundJob.Enqueue<ITestJob>(x => x.Execute(42));
    }
}

Upvotes: 1

Related Questions