| | | 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] |
| | 86 | 14 | | public class DefaultBookmarkQueuePurger( |
| | 86 | 15 | | IBookmarkQueueStore store, |
| | 86 | 16 | | IBookmarkQueueDeadLetterStore deadLetterStore, |
| | 86 | 17 | | IBookmarkQueueDeadLetterManager deadLetterManager, |
| | 86 | 18 | | ISystemClock systemClock, |
| | 86 | 19 | | IOptions<BookmarkQueuePurgeOptions> options, |
| | 86 | 20 | | ILogger<DefaultBookmarkQueuePurger> logger) : IBookmarkQueuePurger |
| | | 21 | | { |
| | | 22 | | public async Task PurgeAsync(CancellationToken cancellationToken = default) |
| | | 23 | | { |
| | 12 | 24 | | var now = systemClock.UtcNow; |
| | 12 | 25 | | var thresholdDate = now - options.Value.Ttl; |
| | | 26 | | |
| | 12 | 27 | | logger.LogDebug("Purging bookmark queue items older than {ThresholdDate}.", thresholdDate); |
| | | 28 | | |
| | 5 | 29 | | while (true) |
| | | 30 | | { |
| | 17 | 31 | | var pageArgs = PageArgs.FromPage(0, options.Value.BatchSize); |
| | 17 | 32 | | var filter = new BookmarkQueueFilter |
| | 17 | 33 | | { |
| | 17 | 34 | | CreatedAtLessThan = thresholdDate |
| | 17 | 35 | | }; |
| | 17 | 36 | | var order = new BookmarkQueueItemOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 17 | 37 | | var page = await store.PageAsync(pageArgs, filter, order, cancellationToken); |
| | 17 | 38 | | var items = page.Items.ToList(); |
| | | 39 | | |
| | 17 | 40 | | if (items.Count == 0) |
| | | 41 | | break; |
| | | 42 | | |
| | 14 | 43 | | var itemIds = items.Select(x => x.Id).ToList(); |
| | 6 | 44 | | var activeItemIds = (await store.FindManyAsync(new BookmarkQueueFilter |
| | 6 | 45 | | { |
| | 6 | 46 | | Ids = itemIds |
| | 11 | 47 | | }, cancellationToken)).Select(x => x.Id).ToList(); |
| | 6 | 48 | | var deadLetters = (await deadLetterManager.DeadLetterManyAsync(items, "Expired", cancellationToken: cancella |
| | 5 | 49 | | var deletedCount = activeItemIds.Count == 0 |
| | 5 | 50 | | ? 0 |
| | 5 | 51 | | : await store.DeleteAsync(new BookmarkQueueFilter |
| | 5 | 52 | | { |
| | 5 | 53 | | Ids = activeItemIds |
| | 5 | 54 | | }, cancellationToken); |
| | 5 | 55 | | var alreadyRemovedItemIds = itemIds.Except(activeItemIds).ToHashSet(); |
| | | 56 | | |
| | 5 | 57 | | if (alreadyRemovedItemIds.Count > 0) |
| | | 58 | | { |
| | 14 | 59 | | foreach (var deadLetter in deadLetters.Where(x => x.CanReplay && x.Reason == "Expired" && alreadyRemoved |
| | | 60 | | { |
| | 2 | 61 | | deadLetter.CanReplay = false; |
| | 2 | 62 | | await deadLetterStore.SaveAsync(deadLetter, cancellationToken); |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | 5 | 66 | | logger.LogInformation( |
| | 5 | 67 | | "Recorded {DeadLetterCount} expired bookmark queue dead-letter items and deleted {DeletedCount} active q |
| | 5 | 68 | | deadLetters.Count, |
| | 5 | 69 | | deletedCount); |
| | 5 | 70 | | } |
| | | 71 | | |
| | 11 | 72 | | await PurgeDeadLettersAsync(now, cancellationToken); |
| | | 73 | | |
| | 11 | 74 | | logger.LogDebug("Finished purging bookmark queue items."); |
| | 11 | 75 | | } |
| | | 76 | | |
| | | 77 | | private async Task PurgeDeadLettersAsync(DateTimeOffset now, CancellationToken cancellationToken) |
| | | 78 | | { |
| | 11 | 79 | | var thresholdDate = now - options.Value.DeadLetterTtl; |
| | | 80 | | |
| | 11 | 81 | | logger.LogDebug("Purging bookmark queue dead-letter items older than {ThresholdDate}.", thresholdDate); |
| | | 82 | | |
| | 1 | 83 | | while (true) |
| | | 84 | | { |
| | 12 | 85 | | var pageArgs = PageArgs.FromPage(0, options.Value.BatchSize); |
| | 12 | 86 | | var filter = new BookmarkQueueDeadLetterFilter |
| | 12 | 87 | | { |
| | 12 | 88 | | DeadLetteredAtLessThan = thresholdDate |
| | 12 | 89 | | }; |
| | 12 | 90 | | var order = new BookmarkQueueDeadLetterItemOrder<DateTimeOffset>(x => x.DeadLetteredAt, OrderDirection.Ascen |
| | 12 | 91 | | var page = await deadLetterStore.PageAsync(pageArgs, filter, order, cancellationToken); |
| | 12 | 92 | | var items = page.Items; |
| | | 93 | | |
| | 12 | 94 | | if (items.Count == 0) |
| | | 95 | | break; |
| | | 96 | | |
| | 1 | 97 | | await deadLetterStore.DeleteAsync(new BookmarkQueueDeadLetterFilter |
| | 1 | 98 | | { |
| | 1 | 99 | | Ids = items.Select(x => x.Id).ToList() |
| | 1 | 100 | | }, cancellationToken); |
| | | 101 | | |
| | 1 | 102 | | logger.LogInformation("Purged {Count} bookmark queue dead-letter items.", items.Count); |
| | 1 | 103 | | } |
| | 11 | 104 | | } |
| | | 105 | | } |