| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Workflows.Runtime.Entities; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows.Runtime; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides extension methods for <see cref="IActivityExecutionStore"/>. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class ActivityExecutionStoreExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Retrieves the execution chain for the specified activity execution record by traversing the SchedulingActivityEx |
| | | 13 | | /// Returns records ordered from root (depth 0) to the specified record. |
| | | 14 | | /// </summary> |
| | | 15 | | public static async Task<Page<ActivityExecutionRecord>> GetExecutionChainAsync( |
| | | 16 | | this IActivityExecutionStore store, |
| | | 17 | | string activityExecutionId, |
| | | 18 | | bool includeCrossWorkflowChain = true, |
| | | 19 | | int? skip = null, |
| | | 20 | | int? take = null, |
| | | 21 | | CancellationToken cancellationToken = default) |
| | | 22 | | { |
| | 10 | 23 | | var chain = new List<ActivityExecutionRecord>(); |
| | 10 | 24 | | var visited = new HashSet<string>(); |
| | 10 | 25 | | var loadedRecords = new Dictionary<string, ActivityExecutionRecord>(); |
| | 10 | 26 | | var currentId = activityExecutionId; |
| | | 27 | | |
| | 36 | 28 | | while (currentId != null && visited.Add(currentId)) |
| | | 29 | | { |
| | 28 | 30 | | if (!loadedRecords.TryGetValue(currentId, out var record)) |
| | | 31 | | { |
| | 28 | 32 | | record = await store.FindAsync(new() { Id = currentId }, cancellationToken); |
| | | 33 | | |
| | 28 | 34 | | if (record == null) |
| | | 35 | | break; |
| | | 36 | | |
| | 27 | 37 | | loadedRecords.TryAdd(currentId, record); |
| | | 38 | | } |
| | | 39 | | |
| | 27 | 40 | | chain.Add(record); |
| | | 41 | | |
| | 27 | 42 | | if (!includeCrossWorkflowChain && record.SchedulingWorkflowInstanceId != null) |
| | | 43 | | break; |
| | | 44 | | |
| | 26 | 45 | | currentId = record.SchedulingActivityExecutionId; |
| | | 46 | | } |
| | | 47 | | |
| | 10 | 48 | | chain.Reverse(); |
| | | 49 | | |
| | 10 | 50 | | var totalCount = chain.Count; |
| | | 51 | | |
| | 10 | 52 | | if (skip.HasValue) |
| | 8 | 53 | | chain = chain.Skip(skip.Value).ToList(); |
| | | 54 | | |
| | 10 | 55 | | if (take.HasValue) |
| | 2 | 56 | | chain = chain.Take(take.Value).ToList(); |
| | | 57 | | |
| | 10 | 58 | | return Page.Of(chain, totalCount); |
| | 10 | 59 | | } |
| | | 60 | | } |