| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Common.Entities; |
| | | 3 | | using Elsa.Common.Models; |
| | | 4 | | using Elsa.Workflows.Runtime.Filters; |
| | | 5 | | using Elsa.Workflows.Runtime.Options; |
| | | 6 | | using Elsa.Workflows.Runtime.OrderDefinitions; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using Microsoft.Extensions.Options; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Runtime; |
| | | 12 | | |
| | | 13 | | [UsedImplicitly] |
| | 5 | 14 | | public class DefaultBookmarkQueuePurger(IBookmarkQueueStore store, ISystemClock systemClock, IOptions<BookmarkQueuePurge |
| | | 15 | | { |
| | | 16 | | public async Task PurgeAsync(CancellationToken cancellationToken = default) |
| | | 17 | | { |
| | 0 | 18 | | var currentPage = 0; |
| | 0 | 19 | | var now = systemClock.UtcNow; |
| | 0 | 20 | | var thresholdDate = now - options.Value.Ttl; |
| | | 21 | | |
| | 0 | 22 | | logger.LogDebug("Purging bookmark queue items older than {ThresholdDate}.", thresholdDate); |
| | | 23 | | |
| | 0 | 24 | | while (true) |
| | | 25 | | { |
| | 0 | 26 | | var pageArgs = PageArgs.FromPage(currentPage, options.Value.BatchSize); |
| | 0 | 27 | | var filter = new BookmarkQueueFilter |
| | 0 | 28 | | { |
| | 0 | 29 | | CreatedAtLessThan = thresholdDate |
| | 0 | 30 | | }; |
| | 0 | 31 | | var order = new BookmarkQueueItemOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 0 | 32 | | var page = await store.PageAsync(pageArgs, filter, order, cancellationToken); |
| | 0 | 33 | | var items = page.Items; |
| | | 34 | | |
| | 0 | 35 | | if (items.Count == 0) |
| | | 36 | | break; |
| | | 37 | | |
| | 0 | 38 | | var ids = items.Select(x => x.Id).ToList(); |
| | 0 | 39 | | await store.DeleteAsync(new BookmarkQueueFilter |
| | 0 | 40 | | { |
| | 0 | 41 | | Ids = ids |
| | 0 | 42 | | }, cancellationToken); |
| | | 43 | | |
| | 0 | 44 | | logger.LogInformation("Purged {Count} bookmark queue items.", items.Count); |
| | | 45 | | |
| | 0 | 46 | | currentPage++; |
| | 0 | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | logger.LogDebug("Finished purging bookmark queue items."); |
| | 0 | 50 | | } |
| | | 51 | | } |