| | | 1 | | using Anichron.Core.Data; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | |
| | | 5 | | namespace Anichron.Core.Work; |
| | | 6 | | |
| | | 7 | | public interface IWorkQueue |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Adds a queued item, or returns the id of the queued item that already holds the payload's |
| | | 11 | | /// dedup key. Saves at once, so call it on a context with no other pending changes. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <typeparam name="TPayload">The payload type, which names the kind.</typeparam> |
| | | 14 | | /// <param name="payload">What the handler receives.</param> |
| | | 15 | | /// <param name="requestedBy">The requesting admin, or null when scheduled.</param> |
| | | 16 | | /// <param name="ct">Cancels the enqueue.</param> |
| | | 17 | | /// <returns>The id of the queued item.</returns> |
| | | 18 | | Task<Guid> EnqueueAsync<TPayload>(TPayload payload, Guid? requestedBy, CancellationToken ct) |
| | | 19 | | where TPayload : IWorkPayload; |
| | | 20 | | } |
| | | 21 | | |
| | 20 | 22 | | public sealed class EfWorkQueue(AnichronDbContext db, IClock clock, IGuidFactory guids) : IWorkQueue |
| | | 23 | | { |
| | | 24 | | internal const int MaxInsertAttempts = 3; |
| | | 25 | | |
| | | 26 | | public async Task<Guid> EnqueueAsync<TPayload>(TPayload payload, Guid? requestedBy, CancellationToken ct) |
| | | 27 | | where TPayload : IWorkPayload |
| | 19 | 28 | | { |
| | 19 | 29 | | var dedupKey = payload.DedupKey; |
| | 48 | 30 | | for (var attempt = 1; attempt <= MaxInsertAttempts; attempt++) |
| | 23 | 31 | | { |
| | 23 | 32 | | if (dedupKey is not null) |
| | 19 | 33 | | { |
| | 19 | 34 | | var waiting = await db.WorkItems |
| | 19 | 35 | | .Where(w => w.DedupKey == dedupKey && w.Status == WorkItemStatus.Queued) |
| | 19 | 36 | | .Select(w => (Guid?)w.Id) |
| | 19 | 37 | | .FirstOrDefaultAsync(ct); |
| | 19 | 38 | | if (waiting is { } id) |
| | 1 | 39 | | return id; |
| | 18 | 40 | | } |
| | | 41 | | |
| | 22 | 42 | | var now = clock.GetCurrentInstant(); |
| | 22 | 43 | | var item = new WorkItem |
| | 22 | 44 | | { |
| | 22 | 45 | | Id = guids.NewGuid(), |
| | 22 | 46 | | Kind = TPayload.Kind, |
| | 22 | 47 | | Payload = WorkJson.Serialize(payload), |
| | 22 | 48 | | DedupKey = dedupKey, |
| | 22 | 49 | | StorageConfigId = payload.StorageConfigId, |
| | 22 | 50 | | RequestedBy = requestedBy, |
| | 22 | 51 | | RequestedAt = now, |
| | 22 | 52 | | Status = WorkItemStatus.Queued, |
| | 22 | 53 | | UpdatedAt = now, |
| | 22 | 54 | | }; |
| | 22 | 55 | | if (await TryInsertAsync(item, ct)) |
| | 17 | 56 | | return item.Id; |
| | 5 | 57 | | } |
| | | 58 | | |
| | 1 | 59 | | throw new InvalidOperationException( |
| | 1 | 60 | | $"Enqueueing work with dedup key '{dedupKey}' lost the insert race {MaxInsertAttempts} times."); |
| | 18 | 61 | | } |
| | | 62 | | |
| | | 63 | | // Stryker disable all |
| | | 64 | | private async Task<bool> TryInsertAsync(WorkItem item, CancellationToken ct) |
| | 22 | 65 | | { |
| | 22 | 66 | | db.WorkItems.Add(item); |
| | | 67 | | try |
| | 22 | 68 | | { |
| | 22 | 69 | | await db.SaveChangesAsync(ct); |
| | 17 | 70 | | return true; |
| | | 71 | | } |
| | 5 | 72 | | catch (DbUpdateException ex) when (ex.IsUniqueViolation(WorkItemIndexNames.QueuedDedupKeyUnique)) |
| | 5 | 73 | | { |
| | 5 | 74 | | db.Entry(item).State = EntityState.Detached; |
| | 5 | 75 | | return false; |
| | | 76 | | } |
| | 22 | 77 | | } |
| | | 78 | | } |