Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background job is ignoring Queue attribute if i am using BackgroundJob.Enqueue() #2498

Open
benzmarkus opened this issue Jan 30, 2025 · 1 comment

Comments

@benzmarkus
Copy link

benzmarkus commented Jan 30, 2025

Description
I used a background job and added a queue attribute. I wrote a custom jobfilter attribute
public class DisableMultipleQueuedItemsAttribute : JobFilterAttribute, IClientFilter, IServerFilter, IElectStateFilter {}
to calculate a fingerprint to ensure a backgroundjob is not called multiple times.

In the Attribute I can read the job parameter and queue is null there.

Steps to Reproduce

------------------------- hangfire config start----------------------
var jobStorage = new SqlServerStorage(_configuration.GetConnectionString("MyConnectionString"));

// Add Hangfire services
services.AddHangfire(configuration =>
{
configuration.UseStorage(jobStorage);
});

services.AddHangfireServer((_, backgroundJobServerOptions) =>
{
backgroundJobServerOptions.Queues = new[] { "WinService", "default") };
backgroundJobServerOptions.WorkerCount = 5;
});
------------------------- hangfire config end----------------------

------------------------- Backgroundjob start----------------------
[Queue("WinService")]
[DisableMultipleQueuedItems]
public async Task SynchronizeAsync(int param1, int param2) {}
------------------------- Backgroundjob end----------------------

------------- calling the job --------------------
BackgroundJob.Enqueue(x => x.SynchronizeAsync(param1, param2);

Observed Behavior
when I set the debugger in my custom attribute the jobs queue is null

Expected Behavior
Queue is set to "WinService" but is null. The correct hangfire process is executing the job

------------------------- hangfire config start----------------------
var jobStorage = new SqlServerStorage(_configuration.GetConnectionString("MyConnectionString"));

// Add Hangfire services
services.AddHangfire(configuration =>
{
configuration.UseStorage(jobStorage);
});

services.AddHangfireServer((_, backgroundJobServerOptions) =>
{
backgroundJobServerOptions.Queues = new[] { "webui") };
backgroundJobServerOptions.WorkerCount = 5;
});
------------------------- hangfire config end----------------------

Additional Information
My setup was:

.NET 6
Hangfire 1.8.17

@odinserj
Copy link
Member

I see that the attributes are applied to some class method, e.g. to the actual implementation:

[Queue("WinService")]
[DisableMultipleQueuedItems]
public async Task SynchronizeAsync(int param1, int param2) {}

And I see that the background job is created based on the Scheduler class.

BackgroundJob.Enqueue<Scheduler>(x => x.SynchronizeAsync(param1, param2);

Please confirm that the attributes applied to the SynchornizeAsync method of the Scheduler class, because otherwise, for example when background job is created on some abstract class, base class or an interface, Hangfire will be unable to see the attributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants