| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using Elsa.Common; |
| | | 3 | | using Elsa.Expressions.Helpers; |
| | | 4 | | using Elsa.Expressions.Models; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Workflows.Activities; |
| | | 7 | | using Elsa.Workflows.CommitStates; |
| | | 8 | | using Elsa.Workflows.Exceptions; |
| | | 9 | | using Elsa.Workflows.Helpers; |
| | | 10 | | using Elsa.Workflows.Memory; |
| | | 11 | | using Elsa.Workflows.Models; |
| | | 12 | | using Elsa.Workflows.Options; |
| | | 13 | | using Elsa.Workflows.State; |
| | | 14 | | using JetBrains.Annotations; |
| | | 15 | | using Microsoft.Extensions.DependencyInjection; |
| | | 16 | | |
| | | 17 | | namespace Elsa.Workflows; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// A delegate entry that is used by activities to be notified when the activities they scheduled are completed. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="Owner">The activity scheduling the <see cref="Child"/> activity.</param> |
| | | 23 | | /// <param name="Child">The child <see cref="IActivity"/> being scheduled.</param> |
| | | 24 | | /// <param name="CompletionCallback">The <see cref="ActivityCompletionCallback"/> delegate to invoke when the scheduled |
| | | 25 | | /// <param name="Tag">An optional tag.</param> |
| | | 26 | | public record ActivityCompletionCallbackEntry(ActivityExecutionContext Owner, ActivityNode Child, ActivityCompletionCall |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Provides context to the currently executing workflow. |
| | | 30 | | /// </summary> |
| | | 31 | | [PublicAPI] |
| | | 32 | | public partial class WorkflowExecutionContext : IExecutionContext |
| | | 33 | | { |
| | 5 | 34 | | private static readonly object ActivityOutputRegistryKey = new(); |
| | 5 | 35 | | private static readonly object LastActivityResultKey = new(); |
| | 16 | 36 | | internal static ValueTask Complete(ActivityExecutionContext context) => context.CompleteActivityAsync(); |
| | 0 | 37 | | internal static ValueTask Noop(ActivityExecutionContext context) => default; |
| | 855 | 38 | | private readonly IList<ActivityCompletionCallbackEntry> _completionCallbackEntries = new List<ActivityCompletionCall |
| | | 39 | | private IList<ActivityExecutionContext> _activityExecutionContexts; |
| | | 40 | | private readonly IHasher _hasher; |
| | | 41 | | private readonly ICommitStateHandler _commitStateHandler; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Initializes a new instance of <see cref="WorkflowExecutionContext"/>. |
| | | 45 | | /// </summary> |
| | 855 | 46 | | private WorkflowExecutionContext( |
| | 855 | 47 | | IServiceProvider serviceProvider, |
| | 855 | 48 | | WorkflowGraph workflowGraph, |
| | 855 | 49 | | string id, |
| | 855 | 50 | | string? correlationId, |
| | 855 | 51 | | string? parentWorkflowInstanceId, |
| | 855 | 52 | | IDictionary<string, object>? input, |
| | 855 | 53 | | IDictionary<string, object>? properties, |
| | 855 | 54 | | ExecuteActivityDelegate? executeDelegate, |
| | 855 | 55 | | string? triggerActivityId, |
| | 855 | 56 | | IEnumerable<ActivityIncident> incidents, |
| | 855 | 57 | | IEnumerable<Bookmark> originalBookmarks, |
| | 855 | 58 | | DateTimeOffset createdAt, |
| | 855 | 59 | | CancellationToken cancellationToken) |
| | | 60 | | { |
| | 855 | 61 | | ServiceProvider = serviceProvider; |
| | 855 | 62 | | SystemClock = serviceProvider.GetRequiredService<ISystemClock>(); |
| | 855 | 63 | | ActivityRegistry = serviceProvider.GetRequiredService<IActivityRegistry>(); |
| | 855 | 64 | | ActivityRegistryLookup = serviceProvider.GetRequiredService<IActivityRegistryLookupService>(); |
| | 855 | 65 | | _hasher = serviceProvider.GetRequiredService<IHasher>(); |
| | 855 | 66 | | _commitStateHandler = serviceProvider.GetRequiredService<ICommitStateHandler>(); |
| | 855 | 67 | | SubStatus = WorkflowSubStatus.Pending; |
| | 855 | 68 | | Id = id; |
| | 855 | 69 | | CorrelationId = correlationId; |
| | 855 | 70 | | ParentWorkflowInstanceId = parentWorkflowInstanceId; |
| | 855 | 71 | | _activityExecutionContexts = new List<ActivityExecutionContext>(); |
| | 855 | 72 | | Scheduler = serviceProvider.GetRequiredService<IActivitySchedulerFactory>().CreateScheduler(); |
| | 855 | 73 | | IdentityGenerator = serviceProvider.GetRequiredService<IIdentityGenerator>(); |
| | 855 | 74 | | Input = input != null ? new(input, StringComparer.OrdinalIgnoreCase) : new Dictionary<string, object>(StringComp |
| | 855 | 75 | | Properties = properties != null ? new(properties, StringComparer.OrdinalIgnoreCase) : new Dictionary<string, obj |
| | 855 | 76 | | ExecuteDelegate = executeDelegate; |
| | 855 | 77 | | TriggerActivityId = triggerActivityId; |
| | 855 | 78 | | CreatedAt = createdAt; |
| | 855 | 79 | | UpdatedAt = createdAt; |
| | 855 | 80 | | CancellationToken = cancellationToken; |
| | 855 | 81 | | Incidents = incidents.ToList(); |
| | 855 | 82 | | OriginalBookmarks = originalBookmarks.ToList(); |
| | 855 | 83 | | WorkflowGraph = workflowGraph; |
| | 855 | 84 | | var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 855 | 85 | | _cancellationTokenSources.Add(linkedCancellationTokenSource); |
| | 855 | 86 | | _cancellationRegistrations.Add(linkedCancellationTokenSource.Token.Register(CancelWorkflow)); |
| | 855 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Creates a new <see cref="WorkflowExecutionContext"/> for the specified workflow. |
| | | 91 | | /// </summary> |
| | | 92 | | public static async Task<WorkflowExecutionContext> CreateAsync( |
| | | 93 | | IServiceProvider serviceProvider, |
| | | 94 | | WorkflowGraph workflowGraph, |
| | | 95 | | string id, |
| | | 96 | | CancellationToken cancellationToken = default) |
| | | 97 | | { |
| | 414 | 98 | | var systemClock = serviceProvider.GetRequiredService<ISystemClock>(); |
| | | 99 | | |
| | 414 | 100 | | return await CreateAsync( |
| | 414 | 101 | | serviceProvider, |
| | 414 | 102 | | workflowGraph, |
| | 414 | 103 | | id, |
| | 414 | 104 | | new List<ActivityIncident>(), |
| | 414 | 105 | | new List<Bookmark>(), |
| | 414 | 106 | | systemClock.UtcNow, |
| | 414 | 107 | | cancellationToken: cancellationToken |
| | 414 | 108 | | ); |
| | 414 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Creates a new <see cref="WorkflowExecutionContext"/> for the specified workflow. |
| | | 113 | | /// </summary> |
| | | 114 | | public static async Task<WorkflowExecutionContext> CreateAsync( |
| | | 115 | | IServiceProvider serviceProvider, |
| | | 116 | | WorkflowGraph workflowGraph, |
| | | 117 | | string id, |
| | | 118 | | string? correlationId = null, |
| | | 119 | | string? parentWorkflowInstanceId = null, |
| | | 120 | | IDictionary<string, object>? input = null, |
| | | 121 | | IDictionary<string, object>? properties = null, |
| | | 122 | | ExecuteActivityDelegate? executeDelegate = null, |
| | | 123 | | string? triggerActivityId = null, |
| | | 124 | | CancellationToken cancellationToken = default) |
| | | 125 | | { |
| | 315 | 126 | | var systemClock = serviceProvider.GetRequiredService<ISystemClock>(); |
| | | 127 | | |
| | 315 | 128 | | return await CreateAsync( |
| | 315 | 129 | | serviceProvider, |
| | 315 | 130 | | workflowGraph, |
| | 315 | 131 | | id, |
| | 315 | 132 | | new List<ActivityIncident>(), |
| | 315 | 133 | | new List<Bookmark>(), |
| | 315 | 134 | | systemClock.UtcNow, |
| | 315 | 135 | | correlationId, |
| | 315 | 136 | | parentWorkflowInstanceId, |
| | 315 | 137 | | input, |
| | 315 | 138 | | properties, |
| | 315 | 139 | | executeDelegate, |
| | 315 | 140 | | triggerActivityId, |
| | 315 | 141 | | cancellationToken |
| | 315 | 142 | | ); |
| | 315 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Creates a new <see cref="WorkflowExecutionContext"/> for the specified workflow. |
| | | 147 | | /// </summary> |
| | | 148 | | public static async Task<WorkflowExecutionContext> CreateAsync( |
| | | 149 | | IServiceProvider serviceProvider, |
| | | 150 | | WorkflowGraph workflowGraph, |
| | | 151 | | WorkflowState workflowState, |
| | | 152 | | string? correlationId = null, |
| | | 153 | | string? parentWorkflowInstanceId = null, |
| | | 154 | | IDictionary<string, object>? input = null, |
| | | 155 | | IDictionary<string, object>? properties = null, |
| | | 156 | | ExecuteActivityDelegate? executeDelegate = null, |
| | | 157 | | string? triggerActivityId = null, |
| | | 158 | | CancellationToken cancellationToken = default) |
| | | 159 | | { |
| | 126 | 160 | | var workflowExecutionContext = await CreateAsync( |
| | 126 | 161 | | serviceProvider, |
| | 126 | 162 | | workflowGraph, |
| | 126 | 163 | | workflowState.Id, |
| | 126 | 164 | | workflowState.Incidents, |
| | 126 | 165 | | workflowState.Bookmarks, |
| | 126 | 166 | | workflowState.CreatedAt, |
| | 126 | 167 | | correlationId, |
| | 126 | 168 | | parentWorkflowInstanceId, |
| | 126 | 169 | | input, |
| | 126 | 170 | | properties, |
| | 126 | 171 | | executeDelegate, |
| | 126 | 172 | | triggerActivityId, |
| | 126 | 173 | | cancellationToken); |
| | | 174 | | |
| | 126 | 175 | | var workflowStateExtractor = serviceProvider.GetRequiredService<IWorkflowStateExtractor>(); |
| | 126 | 176 | | await workflowStateExtractor.ApplyAsync(workflowExecutionContext, workflowState); |
| | | 177 | | |
| | 126 | 178 | | return workflowExecutionContext; |
| | 126 | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Creates a new <see cref="WorkflowExecutionContext"/> for the specified workflow. |
| | | 183 | | /// </summary> |
| | | 184 | | public static async Task<WorkflowExecutionContext> CreateAsync( |
| | | 185 | | IServiceProvider serviceProvider, |
| | | 186 | | WorkflowGraph workflowGraph, |
| | | 187 | | string id, |
| | | 188 | | IEnumerable<ActivityIncident> incidents, |
| | | 189 | | IEnumerable<Bookmark> originalBookmarks, |
| | | 190 | | DateTimeOffset createdAt, |
| | | 191 | | string? correlationId = null, |
| | | 192 | | string? parentWorkflowInstanceId = null, |
| | | 193 | | IDictionary<string, object>? input = null, |
| | | 194 | | IDictionary<string, object>? properties = null, |
| | | 195 | | ExecuteActivityDelegate? executeDelegate = null, |
| | | 196 | | string? triggerActivityId = null, |
| | | 197 | | CancellationToken cancellationToken = default) |
| | | 198 | | { |
| | | 199 | | // Set up a workflow execution context. |
| | 855 | 200 | | var workflowExecutionContext = new WorkflowExecutionContext( |
| | 855 | 201 | | serviceProvider, |
| | 855 | 202 | | workflowGraph, |
| | 855 | 203 | | id, |
| | 855 | 204 | | correlationId, |
| | 855 | 205 | | parentWorkflowInstanceId, |
| | 855 | 206 | | input, |
| | 855 | 207 | | properties, |
| | 855 | 208 | | executeDelegate, |
| | 855 | 209 | | triggerActivityId, |
| | 855 | 210 | | incidents, |
| | 855 | 211 | | originalBookmarks, |
| | 855 | 212 | | createdAt, |
| | 855 | 213 | | cancellationToken) |
| | 855 | 214 | | { |
| | 855 | 215 | | MemoryRegister = workflowGraph.Workflow.CreateRegister() |
| | 855 | 216 | | }; |
| | | 217 | | |
| | 855 | 218 | | workflowExecutionContext.ExpressionExecutionContext = new(serviceProvider, workflowExecutionContext.MemoryRegist |
| | | 219 | | |
| | 855 | 220 | | await workflowExecutionContext.SetWorkflowGraphAsync(workflowGraph); |
| | 855 | 221 | | return workflowExecutionContext; |
| | 855 | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Assigns the specified workflow to this workflow execution context. |
| | | 226 | | /// </summary> |
| | | 227 | | /// <param name="workflowGraph">The workflow graph to assign.</param> |
| | | 228 | | public async Task SetWorkflowGraphAsync(WorkflowGraph workflowGraph) |
| | | 229 | | { |
| | 855 | 230 | | WorkflowGraph = workflowGraph; |
| | 855 | 231 | | var nodes = workflowGraph.Nodes; |
| | | 232 | | |
| | | 233 | | // Register activity types. |
| | | 234 | | var activityTypes = nodes.Select(x => x.Activity.GetType()).Distinct().ToList(); |
| | 855 | 235 | | await ActivityRegistry.RegisterAsync(activityTypes, CancellationToken); |
| | | 236 | | |
| | | 237 | | // Update the activity execution contexts with the actual activity instances. |
| | 1710 | 238 | | foreach (var activityExecutionContext in ActivityExecutionContexts) |
| | 0 | 239 | | activityExecutionContext.Activity = workflowGraph.NodeIdLookup[activityExecutionContext.Activity.NodeId].Act |
| | 855 | 240 | | } |
| | | 241 | | |
| | | 242 | | /// Gets the <see cref="IServiceProvider"/>. |
| | 39405 | 243 | | public IServiceProvider ServiceProvider { get; } |
| | | 244 | | |
| | | 245 | | /// Gets the <see cref="IActivityRegistry"/>. |
| | 855 | 246 | | public IActivityRegistry ActivityRegistry { get; } |
| | | 247 | | |
| | | 248 | | /// Gets the <see cref="IActivityRegistryLookupService"/>. |
| | 3544 | 249 | | public IActivityRegistryLookupService ActivityRegistryLookup { get; } |
| | | 250 | | |
| | | 251 | | /// Gets the workflow graph. |
| | 68704 | 252 | | public WorkflowGraph WorkflowGraph { get; private set; } |
| | | 253 | | |
| | | 254 | | /// The <see cref="Workflow"/> associated with the execution context. |
| | 32035 | 255 | | public Workflow Workflow => WorkflowGraph.Workflow; |
| | | 256 | | |
| | | 257 | | /// A graph of the workflow structure. |
| | 0 | 258 | | public ActivityNode Graph => WorkflowGraph.Root; |
| | | 259 | | |
| | | 260 | | /// The current status of the workflow. |
| | 9557 | 261 | | public WorkflowStatus Status => GetMainStatus(SubStatus); |
| | | 262 | | |
| | | 263 | | /// The current sub status of the workflow. |
| | 14885 | 264 | | public WorkflowSubStatus SubStatus { get; internal set; } |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Gets or sets a value indicating whether the workflow instance is actively executing. |
| | | 268 | | /// </summary> |
| | | 269 | | /// <remarks> |
| | | 270 | | /// This flag is set to <c>true</c> immediately before the workflow begins execution |
| | | 271 | | /// and is set to <c>false</c> once the execution is completed. |
| | | 272 | | /// It can be used to determine if a workflow instance was in-progress in case of unexpected |
| | | 273 | | /// application termination, allowing the system to retry execution upon restarting. |
| | | 274 | | /// </remarks> |
| | 6604 | 275 | | public bool IsExecuting { get; set; } |
| | | 276 | | |
| | | 277 | | /// The root <see cref="MemoryRegister"/> associated with the execution context. |
| | 1919 | 278 | | public MemoryRegister MemoryRegister { get; private set; } = null!; |
| | | 279 | | |
| | | 280 | | /// A unique ID of the execution context. |
| | 17739 | 281 | | public string Id { get; set; } |
| | | 282 | | |
| | | 283 | | /// <inheritdoc /> |
| | 0 | 284 | | public IActivity Activity => Workflow; |
| | | 285 | | |
| | | 286 | | /// An application-specific identifier associated with the execution context. |
| | 1909 | 287 | | public string? CorrelationId { get; set; } |
| | | 288 | | |
| | | 289 | | /// Gets or sets the name of the workflow instance. |
| | 585 | 290 | | public string? Name { get; set; } |
| | | 291 | | |
| | | 292 | | /// The ID of the workflow instance that triggered this instance. |
| | 1421 | 293 | | public string? ParentWorkflowInstanceId { get; set; } |
| | | 294 | | |
| | | 295 | | /// The date and time the workflow execution context was created. |
| | 1421 | 296 | | public DateTimeOffset CreatedAt { get; set; } |
| | | 297 | | |
| | | 298 | | /// The date and time the workflow execution context was last updated. |
| | 3107 | 299 | | public DateTimeOffset UpdatedAt { get; set; } |
| | | 300 | | |
| | | 301 | | /// The date and time the workflow execution context has finished. |
| | 964 | 302 | | public DateTimeOffset? FinishedAt { get; set; } |
| | | 303 | | |
| | | 304 | | /// Gets the clock used to determine the current time. |
| | 11495 | 305 | | public ISystemClock SystemClock { get; } |
| | | 306 | | |
| | | 307 | | /// A flattened list of <see cref="ActivityNode"/>s from the <see cref="Graph"/>. |
| | 9 | 308 | | public IReadOnlyCollection<ActivityNode> Nodes => WorkflowGraph.Nodes.ToList(); |
| | | 309 | | |
| | | 310 | | /// A map between activity IDs and <see cref="ActivityNode"/>s in the workflow graph. |
| | 225 | 311 | | public IDictionary<string, ActivityNode> NodeIdLookup => WorkflowGraph.NodeIdLookup; |
| | | 312 | | |
| | | 313 | | /// A map between hashed activity node IDs and <see cref="ActivityNode"/>s in the workflow graph. |
| | 0 | 314 | | public IDictionary<string, ActivityNode> NodeHashLookup => WorkflowGraph.NodeHashLookup; |
| | | 315 | | |
| | | 316 | | /// A map between <see cref="IActivity"/>s and <see cref="ActivityNode"/>s in the workflow graph. |
| | 34725 | 317 | | public IDictionary<IActivity, ActivityNode> NodeActivityLookup => WorkflowGraph.NodeActivityLookup; |
| | | 318 | | |
| | | 319 | | /// The <see cref="IActivityScheduler"/> for the execution context. |
| | 5317 | 320 | | public IActivityScheduler Scheduler { get; } |
| | | 321 | | |
| | | 322 | | /// Gets the <see cref="IIdentityGenerator"/>. |
| | 3544 | 323 | | public IIdentityGenerator IdentityGenerator { get; } |
| | | 324 | | |
| | | 325 | | /// Gets the collection of original bookmarks associated with the workflow execution context. |
| | 1291 | 326 | | public ICollection<Bookmark> OriginalBookmarks { get; set; } |
| | | 327 | | |
| | | 328 | | /// A collection of collected bookmarks during workflow execution. |
| | 16485 | 329 | | public ICollection<Bookmark> Bookmarks { get; set; } = new List<Bookmark>(); |
| | | 330 | | |
| | | 331 | | /// A diff between the original bookmarks and the current bookmarks. |
| | 436 | 332 | | public Diff<Bookmark> BookmarksDiff => Diff.For(OriginalBookmarks, Bookmarks); |
| | | 333 | | |
| | | 334 | | /// <summary> |
| | | 335 | | /// A dictionary of inputs provided at the start of the current workflow execution. |
| | | 336 | | /// </summary> |
| | 5150 | 337 | | public IDictionary<string, object> Input { get; set; } |
| | | 338 | | |
| | | 339 | | /// <summary> |
| | | 340 | | /// A dictionary of outputs provided by the current workflow execution. |
| | | 341 | | /// </summary> |
| | 1455 | 342 | | public IDictionary<string, object> Output { get; set; } = new Dictionary<string, object>(); |
| | | 343 | | |
| | | 344 | | /// <inheritdoc /> |
| | 1739 | 345 | | public IDictionary<string, object> Properties { get; set; } |
| | | 346 | | |
| | | 347 | | /// <summary> |
| | | 348 | | /// A dictionary that can be used by application code and middleware to store information and even services. Values |
| | | 349 | | /// All data will be gone once workflow execution completes. |
| | | 350 | | /// </summary> |
| | 5381 | 351 | | public IDictionary<object, object> TransientProperties { get; set; } = new Dictionary<object, object>(); |
| | | 352 | | |
| | | 353 | | /// <summary> |
| | | 354 | | /// A collection of incidents that may have occurred during execution. |
| | | 355 | | /// </summary> |
| | 1310 | 356 | | public ICollection<ActivityIncident> Incidents { get; set; } |
| | | 357 | | |
| | | 358 | | /// <summary> |
| | | 359 | | /// The current <see cref="ExecuteActivityDelegate"/> delegate to invoke when executing the next activity. |
| | | 360 | | /// </summary> |
| | 9935 | 361 | | public ExecuteActivityDelegate? ExecuteDelegate { get; set; } |
| | | 362 | | |
| | | 363 | | /// <summary> |
| | | 364 | | /// Provides context about the bookmark that was used to resume workflow execution, if any. |
| | | 365 | | /// </summary> |
| | 3042 | 366 | | public ResumedBookmarkContext? ResumedBookmarkContext { get; set; } |
| | | 367 | | |
| | | 368 | | /// <summary> |
| | | 369 | | /// The ID of the activity associated with the trigger that caused this workflow execution, if any. |
| | | 370 | | /// </summary> |
| | 2942 | 371 | | public string? TriggerActivityId { get; set; } |
| | | 372 | | |
| | | 373 | | /// <summary> |
| | | 374 | | /// A set of cancellation tokens that can be used to cancel the workflow execution without cancelling system-level o |
| | | 375 | | /// </summary> |
| | 9604 | 376 | | public CancellationToken CancellationToken { get; } |
| | | 377 | | |
| | | 378 | | /// <summary> |
| | | 379 | | /// A list of <see cref="ActivityCompletionCallbackEntry"/> callbacks that are invoked when the associated child act |
| | | 380 | | /// </summary> |
| | 4008 | 381 | | public ICollection<ActivityCompletionCallbackEntry> CompletionCallbacks => new ReadOnlyCollection<ActivityCompletion |
| | | 382 | | |
| | | 383 | | /// <summary> |
| | | 384 | | /// A list of <see cref="ActivityExecutionContext"/>s that are currently active. |
| | | 385 | | /// </summary> |
| | | 386 | | public IReadOnlyCollection<ActivityExecutionContext> ActivityExecutionContexts |
| | | 387 | | { |
| | 7417 | 388 | | get => _activityExecutionContexts.AsReadOnly(); |
| | 126 | 389 | | internal set => _activityExecutionContexts = value.ToList(); |
| | | 390 | | } |
| | | 391 | | |
| | | 392 | | /// <summary> |
| | | 393 | | /// The last execution log sequence number. This number is incremented every time a new entry is added to the execut |
| | | 394 | | /// </summary> |
| | 12990 | 395 | | public long ExecutionLogSequence { get; set; } |
| | | 396 | | |
| | | 397 | | /// <summary> |
| | | 398 | | /// A collection of execution log entries. This collection is flushed when the workflow execution context ends. |
| | | 399 | | /// </summary> |
| | 8375 | 400 | | public ICollection<WorkflowExecutionLogEntry> ExecutionLog { get; } = new List<WorkflowExecutionLogEntry>(); |
| | | 401 | | |
| | | 402 | | /// <summary> |
| | | 403 | | /// The expression execution context for the current workflow execution. |
| | | 404 | | /// </summary> |
| | 1816 | 405 | | public ExpressionExecutionContext ExpressionExecutionContext { get; private set; } = null!; |
| | | 406 | | |
| | | 407 | | /// <inheritdoc /> |
| | 0 | 408 | | public IEnumerable<Variable> Variables => Workflow.Variables; |
| | | 409 | | |
| | | 410 | | /// <summary> |
| | | 411 | | /// Resolves the specified service type from the service provider. |
| | | 412 | | /// </summary> |
| | 32516 | 413 | | public T GetRequiredService<T>() where T : notnull => ServiceProvider.GetRequiredService<T>(); |
| | | 414 | | |
| | | 415 | | /// <summary> |
| | | 416 | | /// Resolves the specified service type from the service provider. |
| | | 417 | | /// </summary> |
| | 2564 | 418 | | public object GetRequiredService(Type serviceType) => ServiceProvider.GetRequiredService(serviceType); |
| | | 419 | | |
| | | 420 | | /// <summary> |
| | | 421 | | /// Resolves the specified service type from the service provider, or creates a new instance if the service type was |
| | | 422 | | /// </summary> |
| | 0 | 423 | | public T GetOrCreateService<T>() where T : notnull => ActivatorUtilities.GetServiceOrCreateInstance<T>(ServiceProvid |
| | | 424 | | |
| | | 425 | | /// <summary> |
| | | 426 | | /// Resolves the specified service type from the service provider, or creates a new instance if the service type was |
| | | 427 | | /// </summary> |
| | 0 | 428 | | public object GetOrCreateService(Type serviceType) => ActivatorUtilities.GetServiceOrCreateInstance(ServiceProvider, |
| | | 429 | | |
| | | 430 | | /// <summary> |
| | | 431 | | /// Resolves the specified service type from the service provider. |
| | | 432 | | /// </summary> |
| | 566 | 433 | | public T? GetService<T>() where T : notnull => ServiceProvider.GetService<T>(); |
| | | 434 | | |
| | | 435 | | /// <summary> |
| | | 436 | | /// Resolves the specified service type from the service provider. |
| | | 437 | | /// </summary> |
| | 0 | 438 | | public object? GetService(Type serviceType) => ServiceProvider.GetService(serviceType); |
| | | 439 | | |
| | | 440 | | /// <summary> |
| | | 441 | | /// Resolves multiple implementations of the specified service type from the service provider. |
| | | 442 | | /// </summary> |
| | 214 | 443 | | public IEnumerable<T> GetServices<T>() where T : notnull => ServiceProvider.GetServices<T>(); |
| | | 444 | | |
| | | 445 | | /// <summary> |
| | | 446 | | /// Registers a completion callback for the specified activity. |
| | | 447 | | /// </summary> |
| | | 448 | | internal void AddCompletionCallback(ActivityExecutionContext owner, ActivityNode child, ActivityCompletionCallback? |
| | | 449 | | { |
| | 2675 | 450 | | var entry = new ActivityCompletionCallbackEntry(owner, child, completionCallback, tag); |
| | 2675 | 451 | | _completionCallbackEntries.Add(entry); |
| | 2675 | 452 | | } |
| | | 453 | | |
| | | 454 | | /// <summary> |
| | | 455 | | /// Unregisters the completion callback for the specified owner and child activity. |
| | | 456 | | /// </summary> |
| | | 457 | | internal ActivityCompletionCallbackEntry? PopCompletionCallback(ActivityExecutionContext owner, ActivityNode child) |
| | | 458 | | { |
| | 32093 | 459 | | var entry = _completionCallbackEntries.FirstOrDefault(x => x.Owner == owner && x.Child == child); |
| | | 460 | | |
| | 12171 | 461 | | if (entry == null) |
| | 9632 | 462 | | return null; |
| | | 463 | | |
| | 2539 | 464 | | RemoveCompletionCallback(entry); |
| | 2539 | 465 | | return entry; |
| | | 466 | | } |
| | | 467 | | |
| | 2539 | 468 | | internal void RemoveCompletionCallback(ActivityCompletionCallbackEntry entry) => _completionCallbackEntries.Remove(e |
| | | 469 | | |
| | | 470 | | internal void RemoveCompletionCallbacks(IEnumerable<ActivityCompletionCallbackEntry> entries) |
| | | 471 | | { |
| | 6260 | 472 | | foreach (var entry in entries.ToList()) |
| | 2 | 473 | | _completionCallbackEntries.Remove(entry); |
| | 3128 | 474 | | } |
| | | 475 | | |
| | | 476 | | /// <summary> |
| | | 477 | | /// Finds the activity based on the provided <paramref name="handle"/>. |
| | | 478 | | /// </summary> |
| | | 479 | | /// <param name="handle">The handle containing the identification parameters for the activity.</param> |
| | | 480 | | /// <returns>The activity found based on the handle, or null if no activity is found.</returns> |
| | | 481 | | public IActivity? FindActivity(ActivityHandle handle) |
| | | 482 | | { |
| | 0 | 483 | | return handle.ActivityId != null |
| | 0 | 484 | | ? FindActivityById(handle.ActivityId) |
| | 0 | 485 | | : handle.ActivityNodeId != null |
| | 0 | 486 | | ? FindActivityByNodeId(handle.ActivityNodeId) |
| | 0 | 487 | | : handle.ActivityInstanceId != null |
| | 0 | 488 | | ? FindActivityByInstanceId(handle.ActivityInstanceId) |
| | 0 | 489 | | : handle.ActivityHash != null |
| | 0 | 490 | | ? FindActivityByHash(handle.ActivityHash) |
| | 0 | 491 | | : null; |
| | | 492 | | } |
| | | 493 | | |
| | | 494 | | /// <summary> |
| | | 495 | | /// Returns the <see cref="ActivityNode"/> with the specified activity ID from the workflow graph. |
| | | 496 | | /// </summary> |
| | 225 | 497 | | public ActivityNode? FindNodeById(string nodeId) => NodeIdLookup.TryGetValue(nodeId, out var node) ? node : null; |
| | | 498 | | |
| | | 499 | | /// <summary> |
| | | 500 | | /// Returns the <see cref="ActivityNode"/> with the specified hash of the activity node ID from the workflow graph. |
| | | 501 | | /// </summary> |
| | | 502 | | /// <param name="hash">The hash of the activity node ID.</param> |
| | | 503 | | /// <returns>The <see cref="ActivityNode"/> with the specified hash of the activity node ID.</returns> |
| | 0 | 504 | | public ActivityNode? FindNodeByHash(string hash) => NodeHashLookup.TryGetValue(hash, out var node) ? node : null; |
| | | 505 | | |
| | | 506 | | /// Returns the <see cref="ActivityNode"/> containing the specified activity from the workflow graph. |
| | | 507 | | public ActivityNode? FindNodeByActivity(IActivity activity) |
| | | 508 | | { |
| | 32142 | 509 | | return NodeActivityLookup.TryGetValue(activity, out var node) ? node : null; |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | /// Returns the <see cref="ActivityNode"/> associated with the specified activity ID. |
| | 0 | 513 | | public ActivityNode? FindNodeByActivityId(string activityId) => Nodes.FirstOrDefault(x => x.Activity.Id == activityI |
| | | 514 | | |
| | | 515 | | /// Returns the <see cref="IActivity"/> with the specified ID from the workflow graph. |
| | 133 | 516 | | public IActivity? FindActivityByNodeId(string nodeId) => FindNodeById(nodeId)?.Activity; |
| | | 517 | | |
| | | 518 | | /// Returns the <see cref="IActivity"/> with the specified ID from the workflow graph. |
| | 0 | 519 | | public IActivity? FindActivityById(string activityId) => FindNodeById(NodeIdLookup.SingleOrDefault(n => n.Key.EndsWi |
| | | 520 | | |
| | | 521 | | /// Returns the <see cref="IActivity"/> with the specified hash of the activity node ID from the workflow graph. |
| | | 522 | | /// <param name="hash">The hash of the activity node ID.</param> |
| | | 523 | | /// <returns>The <see cref="IActivity"/> with the specified hash of the activity node ID.</returns> |
| | 0 | 524 | | public IActivity? FindActivityByHash(string hash) => FindNodeByHash(hash)?.Activity; |
| | | 525 | | |
| | | 526 | | /// Returns the <see cref="ActivityExecutionContext"/> with the specified activity instance ID. |
| | 0 | 527 | | public IActivity? FindActivityByInstanceId(string activityInstanceId) => ActivityExecutionContexts.FirstOrDefault(x |
| | | 528 | | |
| | | 529 | | /// Returns a custom property with the specified key from the <see cref="Properties"/> dictionary. |
| | 203 | 530 | | public T? GetProperty<T>(string key) => Properties.TryGetValue(key, out var value) ? value.ConvertTo<T>() : default; |
| | | 531 | | |
| | | 532 | | /// Sets a custom property with the specified key on the <see cref="Properties"/> dictionary. |
| | 0 | 533 | | public void SetProperty<T>(string key, T value) => Properties[key] = value!; |
| | | 534 | | |
| | | 535 | | /// Updates a custom property with the specified key on the <see cref="Properties"/> dictionary. |
| | | 536 | | public T UpdateProperty<T>(string key, Func<T?, T> updater) |
| | | 537 | | { |
| | 0 | 538 | | var value = GetProperty<T?>(key); |
| | 0 | 539 | | value = updater(value); |
| | 0 | 540 | | Properties[key] = value!; |
| | 0 | 541 | | return value; |
| | | 542 | | } |
| | | 543 | | |
| | | 544 | | /// Returns true if the <see cref="Properties"/> dictionary contains the specified key. |
| | 0 | 545 | | public bool HasProperty(string name) => Properties.ContainsKey(name); |
| | | 546 | | |
| | 17 | 547 | | internal bool CanTransitionTo(WorkflowSubStatus targetSubStatus) => ValidateStatusTransition(); |
| | | 548 | | |
| | | 549 | | internal void TransitionTo(WorkflowSubStatus subStatus) |
| | | 550 | | { |
| | 1278 | 551 | | if (!ValidateStatusTransition()) |
| | 0 | 552 | | throw new($"Cannot transition from {SubStatus} to {subStatus}"); |
| | | 553 | | |
| | 1278 | 554 | | SubStatus = subStatus; |
| | 1278 | 555 | | UpdatedAt = SystemClock.UtcNow; |
| | | 556 | | |
| | 1278 | 557 | | if (Status == WorkflowStatus.Finished) |
| | 398 | 558 | | FinishedAt = UpdatedAt; |
| | | 559 | | |
| | 1278 | 560 | | if (Status == WorkflowStatus.Finished || SubStatus == WorkflowSubStatus.Suspended) |
| | | 561 | | { |
| | 1768 | 562 | | foreach (var registration in _cancellationRegistrations) |
| | 442 | 563 | | registration.Dispose(); |
| | | 564 | | } |
| | 1278 | 565 | | } |
| | | 566 | | |
| | | 567 | | /// Creates a new <see cref="ActivityExecutionContext"/> for the specified activity. |
| | | 568 | | public async Task<ActivityExecutionContext> CreateActivityExecutionContextAsync(IActivity activity, ActivityInvocati |
| | | 569 | | { |
| | 3544 | 570 | | var activityDescriptor = await ActivityRegistryLookup.FindAsync(activity) ?? throw new ActivityNotFoundException |
| | 3544 | 571 | | var tag = options?.Tag; |
| | 3544 | 572 | | var parentContext = options?.Owner; |
| | 3544 | 573 | | var now = SystemClock.UtcNow; |
| | 3544 | 574 | | var id = IdentityGenerator.GenerateId(); |
| | 3544 | 575 | | var activityExecutionContext = new ActivityExecutionContext(id, this, parentContext, activity, activityDescripto |
| | 3544 | 576 | | var variablesToDeclare = options?.Variables ?? []; |
| | 3544 | 577 | | var variableContainer = new[] |
| | 3544 | 578 | | { |
| | 3544 | 579 | | activityExecutionContext.ActivityNode |
| | | 580 | | }.Concat(activityExecutionContext.ActivityNode.Ancestors()).FirstOrDefault(x => x.Activity is IVariableContainer |
| | 3544 | 581 | | activityExecutionContext.ExpressionExecutionContext.TransientProperties[ExpressionExecutionContextExtensions.Act |
| | | 582 | | |
| | 3544 | 583 | | if (variableContainer != null) |
| | | 584 | | { |
| | 7262 | 585 | | foreach (var variable in variablesToDeclare) |
| | | 586 | | { |
| | | 587 | | // Declare a dynamic variable on the activity execution context. |
| | 129 | 588 | | activityExecutionContext.DynamicVariables.RemoveWhere(x => x.Name == variable.Name); |
| | 87 | 589 | | activityExecutionContext.DynamicVariables.Add(variable); |
| | | 590 | | |
| | | 591 | | // Assign the variable to the expression execution context. |
| | 87 | 592 | | activityExecutionContext.ExpressionExecutionContext.CreateVariable(variable.Name, variable.Value); |
| | | 593 | | } |
| | | 594 | | } |
| | | 595 | | |
| | 3544 | 596 | | var activityInput = options?.Input ?? new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); |
| | 3544 | 597 | | activityExecutionContext.ActivityInput.Merge(activityInput); |
| | | 598 | | |
| | 3544 | 599 | | return activityExecutionContext; |
| | 3544 | 600 | | } |
| | | 601 | | |
| | | 602 | | /// Returns a register of recorded activity output. |
| | 3890 | 603 | | public ActivityOutputRegister GetActivityOutputRegister() => TransientProperties.GetOrAdd(ActivityOutputRegistryKey, |
| | | 604 | | |
| | | 605 | | /// Returns the last activity result. |
| | 0 | 606 | | public object? GetLastActivityResult() => TransientProperties.TryGetValue(LastActivityResultKey, out var value) ? va |
| | | 607 | | |
| | | 608 | | /// Adds the specified <see cref="ActivityExecutionContext"/> to the workflow execution context. |
| | 2983 | 609 | | public void AddActivityExecutionContext(ActivityExecutionContext context) => _activityExecutionContexts.Add(context) |
| | | 610 | | |
| | | 611 | | /// Removes the specified <see cref="ActivityExecutionContext"/> from the workflow execution context. |
| | | 612 | | public void RemoveActivityExecutionContext(ActivityExecutionContext context) |
| | | 613 | | { |
| | 2541 | 614 | | _activityExecutionContexts.Remove(context); |
| | 2541 | 615 | | context.ParentActivityExecutionContext?.Children.Remove(context); |
| | 2541 | 616 | | } |
| | | 617 | | |
| | | 618 | | /// Removes the specified <see cref="ActivityExecutionContext"/> from the workflow execution context. |
| | | 619 | | /// <param name="predicate">The predicate used to filter the activity execution contexts to remove.</param> |
| | | 620 | | public void RemoveActivityExecutionContexts(Func<ActivityExecutionContext, bool> predicate) |
| | | 621 | | { |
| | 436 | 622 | | var itemsToRemove = _activityExecutionContexts.Where(predicate).ToList(); |
| | 5954 | 623 | | foreach (var item in itemsToRemove) |
| | 2541 | 624 | | RemoveActivityExecutionContext(item); |
| | 436 | 625 | | } |
| | | 626 | | |
| | | 627 | | /// <summary> |
| | | 628 | | /// Removes all completed activity execution contexts that have a parent activity execution context. |
| | | 629 | | /// </summary> |
| | | 630 | | public void ClearCompletedActivityExecutionContexts() |
| | | 631 | | { |
| | 3539 | 632 | | RemoveActivityExecutionContexts(x => x is { IsCompleted: true, ParentActivityExecutionContext: not null }); |
| | 436 | 633 | | } |
| | | 634 | | |
| | | 635 | | public IEnumerable<ActivityExecutionContext> GetActiveActivityExecutionContexts() |
| | | 636 | | { |
| | | 637 | | // Filter out completed activity execution contexts, except for the root Workflow activity context, which stores |
| | | 638 | | // This will currently break scripts accessing activity output directly, but there's a workaround for that via v |
| | | 639 | | // We may ultimately restore direct output access, but differently. |
| | 7110 | 640 | | return ActivityExecutionContexts.Where(x => !x.IsCompleted || x.ParentActivityExecutionContext == null); |
| | | 641 | | } |
| | | 642 | | |
| | | 643 | | /// <summary> |
| | | 644 | | /// Records the output of the specified activity into the current workflow execution context. |
| | | 645 | | /// </summary> |
| | | 646 | | /// <param name="activityExecutionContext">The <see cref="ActivityExecutionContext"/> of the activity.</param> |
| | | 647 | | /// <param name="outputName">The name of the output.</param> |
| | | 648 | | /// <param name="value">The value of the output.</param> |
| | | 649 | | internal void RecordActivityOutput(ActivityExecutionContext activityExecutionContext, string? outputName, object? va |
| | | 650 | | { |
| | 1451 | 651 | | var register = GetActivityOutputRegister(); |
| | 1451 | 652 | | register.Record(activityExecutionContext, outputName, value); |
| | | 653 | | |
| | | 654 | | // If the output name is the default output name, record the value as the last activity result. |
| | 1451 | 655 | | if (outputName == ActivityOutputRegister.DefaultOutputName) |
| | 334 | 656 | | TransientProperties[LastActivityResultKey] = value!; |
| | 1451 | 657 | | } |
| | | 658 | | |
| | | 659 | | private WorkflowStatus GetMainStatus(WorkflowSubStatus subStatus) => |
| | 10852 | 660 | | subStatus switch |
| | 10852 | 661 | | { |
| | 402 | 662 | | WorkflowSubStatus.Pending => WorkflowStatus.Running, |
| | 12 | 663 | | WorkflowSubStatus.Cancelled => WorkflowStatus.Finished, |
| | 2943 | 664 | | WorkflowSubStatus.Executing => WorkflowStatus.Running, |
| | 71 | 665 | | WorkflowSubStatus.Faulted => WorkflowStatus.Finished, |
| | 6650 | 666 | | WorkflowSubStatus.Finished => WorkflowStatus.Finished, |
| | 774 | 667 | | WorkflowSubStatus.Suspended => WorkflowStatus.Running, |
| | 0 | 668 | | _ => throw new ArgumentOutOfRangeException(nameof(subStatus), subStatus, null) |
| | 10852 | 669 | | }; |
| | | 670 | | |
| | | 671 | | // TODO: Check if we should not use the target subStatus here instead. |
| | | 672 | | private bool ValidateStatusTransition() |
| | | 673 | | { |
| | 1295 | 674 | | var currentMainStatus = GetMainStatus(SubStatus); |
| | 1295 | 675 | | return currentMainStatus != WorkflowStatus.Finished; |
| | | 676 | | } |
| | | 677 | | |
| | | 678 | | public Task CommitAsync() |
| | | 679 | | { |
| | 0 | 680 | | return _commitStateHandler.CommitAsync(this, CancellationToken); |
| | | 681 | | } |
| | | 682 | | } |