Browse Source
Delete oldest completed jobs first in the in-memory background job store
pull/25742/head
maliming
3 months ago
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4
2 changed files with
17 additions and
0 deletions
-
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/InMemoryBackgroundJobStore.cs
-
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobCleanupWorker_Tests.cs
|
|
|
@ -78,6 +78,7 @@ public class InMemoryBackgroundJobStore : IBackgroundJobStore, ISingletonDepende |
|
|
|
var idsToDelete = _jobs.Values |
|
|
|
.Where(t => t.ApplicationName == applicationName) |
|
|
|
.Where(t => t.CompletionTime != null && t.CompletionTime < completedBefore) |
|
|
|
.OrderBy(t => t.CompletionTime) |
|
|
|
.Take(maxResultCount) |
|
|
|
.Select(t => t.Id) |
|
|
|
.ToList(); |
|
|
|
|
|
|
|
@ -117,6 +117,22 @@ public class BackgroundJobCleanupWorker_Tests : AbpIntegratedTest<AbpBackgroundJ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_Delete_Oldest_Completed_Jobs_First_When_Limited_By_MaxResultCount() |
|
|
|
{ |
|
|
|
var oldest = await InsertCompletedJobAsync(_clock.Now.Subtract(TimeSpan.FromDays(5))); |
|
|
|
var middle = await InsertCompletedJobAsync(_clock.Now.Subtract(TimeSpan.FromDays(3))); |
|
|
|
var newest = await InsertCompletedJobAsync(_clock.Now.Subtract(TimeSpan.FromDays(1))); |
|
|
|
|
|
|
|
// All three are completed before now, but a single call may delete only two.
|
|
|
|
var deletedCount = await _store.DeleteAsync(null, _clock.Now, maxResultCount: 2); |
|
|
|
|
|
|
|
deletedCount.ShouldBe(2); |
|
|
|
(await _store.FindAsync(oldest)).ShouldBeNull(); |
|
|
|
(await _store.FindAsync(middle)).ShouldBeNull(); |
|
|
|
(await _store.FindAsync(newest)).ShouldNotBeNull(); // newest survives, proving oldest-first deletion
|
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task Should_Not_Loop_Forever_When_MaxJobFetchCount_Is_Zero() |
|
|
|
{ |
|
|
|
|