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

Add UniqueEnqueue methods #1437

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kogratte
Copy link

Allow users to enqueue a job replacing an existing one.

Usage:

Background.UniqueEnqueue<RegisterDeviceFunction>(x => x.ExecuteAsync( new RegisterPlateNumberOfDeviceFunctionInput { DeviceId = request.DeviceId, LastConnection = DateTime.Now, PlateNumber = request.PlateNumber } ), args => { if (args[0] is RegisterPlateNumberOfDeviceFunctionInput input) { return input.DeviceId == request.DeviceId; } return false; } );

All scheduled and enqueued job who match the comparison are destroyed before the new one is enqueued.

@Odonno
Copy link

Odonno commented May 17, 2019

+1

Tested and approved.

@odinserj
Copy link
Member

Thanks for your work, @kogratte, but I can't accept this PR due to the following problems. Consider the following concurrent schedule, where two threads simultaneously call the UniqueEnqueue method with the same comparer:

Step Thread 1 Thread 2
1 FilterExistingJobs(comparer);  
2   FilterExistingJobs(comparer);
3 return Enqueue(methodCall);  
4   return Enqueue(methodCall);

As a result, Thread 2 will not see that Thread 1 is creating the same background job (and vice versa), because background job still wasn't created, and as a result of this race condition, we'll have non-unique behavior. So we simply can't call this method UniqueEnqueue.

Another problem is related with the filtering itself – there may be thousands or millions of background jobs at some point of time. By querying all of them we'll kill our storage.

@kogratte
Copy link
Author

Thanks for the feedback!

One step a time :)

First of all, is there a way to get the job queue that is thread safe?
I imagine another option based on attribute usage. Other jobs would be removed on runtime, but there is still the problematic with multiple threads, or even with a big queue.

I think I'm not the first one to meet this need, so, if I'm ready to make it come true, do you see a way to do it without any of the trouble you've identified?

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

Successfully merging this pull request may close these issues.

None yet

3 participants