Throttling: One Job Executing, One Waiting, Additional Jobs Dropped

Hello!

I have a background method that takes an int as a parameter:

[DynamicWindow("Test", "Test:{0}")]
[Throttling(ThrottlingAction.DeleteJob)]
public static void Test(Int32 appId) => Thread.Sleep(5000);

I’ve registered the Test dynamic window like this:

throttlingManager.AddOrUpdateDynamicWindow("Test", new DynamicWindowOptions(limit: 1, interval: TimeSpan.FromMinutes(1), buckets: 60));

I’m trying to configure Hangfire throttling to achieve the following:

  • Only allow a single job for a given int value to run at a time.
  • Allow a single job for a given int value to wait for the running job to finish.
  • Delete all other jobs that are enqueued.

As written above, everything appears to work as I expect except for the waiting job. Instead it works like this:

BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 1 enqueued, job takes 5s to run
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 2 deleted
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 3 deleted
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 4 deleted
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 5 deleted
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 6 enqueued, job takes 5s to run
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 7 deleted
// etc.

However, what I am looking for is this:

BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 1 enqueued, job takes 5s to run
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 2 enqueued, waits for job 1 to finish
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 3 deleted
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 4 deleted
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 5 deleted
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 6 enqueued, waits for job 2 to finish
Thread.Sleep(1000);
BackgroundJob.Enqueue(() => TestJob.Test(123456)); // job 7 deleted
// etc.

Is there an existing incantation to make this work or do I need to write some custom throttling code?

Thanks!

~ Josh