Browse Source

Delete oldest completed jobs first in the in-memory background job store

pull/25742/head
maliming 3 months ago
parent
commit
9892971975
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 1
      framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/InMemoryBackgroundJobStore.cs
  2. 16
      framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobCleanupWorker_Tests.cs

1
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/InMemoryBackgroundJobStore.cs

@ -78,6 +78,7 @@ public class InMemoryBackgroundJobStore : IBackgroundJobStore, ISingletonDepende
var idsToDelete = _jobs.Values var idsToDelete = _jobs.Values
.Where(t => t.ApplicationName == applicationName) .Where(t => t.ApplicationName == applicationName)
.Where(t => t.CompletionTime != null && t.CompletionTime < completedBefore) .Where(t => t.CompletionTime != null && t.CompletionTime < completedBefore)
.OrderBy(t => t.CompletionTime)
.Take(maxResultCount) .Take(maxResultCount)
.Select(t => t.Id) .Select(t => t.Id)
.ToList(); .ToList();

16
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobCleanupWorker_Tests.cs

@ -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] [Fact]
public async Task Should_Not_Loop_Forever_When_MaxJobFetchCount_Is_Zero() public async Task Should_Not_Loop_Forever_When_MaxJobFetchCount_Is_Zero()
{ {

Loading…
Cancel
Save