| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.UserTasks.Contracts; |
| | | 3 | | using Elsa.UserTasks.Models; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | |
| | | 6 | | namespace Elsa.UserTasks.Services; |
| | | 7 | | |
| | 1 | 8 | | public sealed class DefaultUserTaskDueService( |
| | 1 | 9 | | IUserTaskRepository repository, |
| | 1 | 10 | | IUserTaskManager manager, |
| | 1 | 11 | | IUserTaskNotificationSink notifications, |
| | 1 | 12 | | IIdentityGenerator identityGenerator, |
| | 1 | 13 | | ISystemClock clock) : IUserTaskDueService |
| | | 14 | | { |
| | | 15 | | public async Task<int> MarkOverdueAsync(string tenantId, DateTimeOffset? now = null, CancellationToken cancellationT |
| | | 16 | | { |
| | 1 | 17 | | var scanNow = now ?? clock.UtcNow; |
| | 1 | 18 | | var cursor = (string?)null; |
| | 1 | 19 | | var marked = 0; |
| | | 20 | | |
| | | 21 | | do |
| | | 22 | | { |
| | 1 | 23 | | var due = await repository.QueryAsync(new UserTaskQuery |
| | 1 | 24 | | { |
| | 1 | 25 | | TenantId = tenantId, |
| | 1 | 26 | | DueTo = scanNow, |
| | 1 | 27 | | Cursor = cursor, |
| | 1 | 28 | | Sort = "due", |
| | 1 | 29 | | Limit = 200 |
| | 1 | 30 | | }, cancellationToken); |
| | | 31 | | |
| | 5 | 32 | | foreach (var task in due.Items.Where(x => !x.IsTerminal && x.DueAt <= scanNow)) |
| | | 33 | | { |
| | 1 | 34 | | if (task.EnableTimeoutOutcome) |
| | | 35 | | { |
| | | 36 | | try |
| | | 37 | | { |
| | 1 | 38 | | var result = await manager.TimeoutAsync(tenantId, task.Id, task.Revision, scanNow, cancellationT |
| | 1 | 39 | | if (result.Accepted) |
| | 1 | 40 | | marked++; |
| | 1 | 41 | | } |
| | 0 | 42 | | catch (Exception exception) when (exception is not OperationCanceledException) |
| | | 43 | | { |
| | | 44 | | // The accepted transition remains durable and is repaired by reconciliation if |
| | | 45 | | // bookmark delivery failed. Continue scanning other tasks. |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | continue; |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | if (task.IsOverdue || !await repository.TryMutateAsync(tenantId, task.Id, task.Revision, current => |
| | 0 | 52 | | { |
| | 0 | 53 | | if (current.IsOverdue || current.IsTerminal) |
| | 0 | 54 | | return false; |
| | 0 | 55 | | current.IsOverdue = true; |
| | 0 | 56 | | current.Events.Add(new UserTaskEvent(identityGenerator.GenerateId(), tenantId, current.Id, curre |
| | 0 | 57 | | "OverdueNotified", clock.UtcNow)); |
| | 0 | 58 | | return true; |
| | 0 | 59 | | }, cancellationToken)) |
| | | 60 | | continue; |
| | | 61 | | |
| | 0 | 62 | | marked++; |
| | 0 | 63 | | var committed = await repository.GetAsync(tenantId, task.Id, cancellationToken); |
| | 0 | 64 | | if (committed != null) |
| | 0 | 65 | | await notifications.PublishAsync(new UserTaskOverdue(tenantId, committed.Id, committed.Status, commi |
| | 0 | 66 | | } |
| | | 67 | | |
| | 1 | 68 | | cursor = due.NextCursor; |
| | 1 | 69 | | } |
| | 1 | 70 | | while (cursor != null); |
| | | 71 | | |
| | 1 | 72 | | return marked; |
| | 1 | 73 | | } |
| | | 74 | | } |