| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Common; |
| | | 3 | | using Elsa.Expressions.Helpers; |
| | | 4 | | using Elsa.Expressions.Models; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Mediator.Contracts; |
| | | 7 | | using Elsa.Workflows.Memory; |
| | | 8 | | using Elsa.Workflows.Models; |
| | | 9 | | using Elsa.Workflows.Options; |
| | | 10 | | using JetBrains.Annotations; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Workflows; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Represents the context of an activity execution. |
| | | 16 | | /// </summary> |
| | | 17 | | [PublicAPI] |
| | | 18 | | public partial class ActivityExecutionContext : IExecutionContext, IDisposable |
| | | 19 | | { |
| | | 20 | | private readonly ISystemClock _systemClock; |
| | | 21 | | private ActivityStatus _status; |
| | | 22 | | private Exception? _exception; |
| | | 23 | | private long _executionCount; |
| | | 24 | | private ActivityExecutionContext? _parentActivityExecutionContext; |
| | | 25 | | |
| | | 26 | | // Bookmarks created during the lifetime of this activity. |
| | 3903 | 27 | | private List<Bookmark> _newBookmarks = []; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="ActivityExecutionContext"/> class. |
| | | 31 | | /// </summary> |
| | 3903 | 32 | | public ActivityExecutionContext( |
| | 3903 | 33 | | string id, |
| | 3903 | 34 | | WorkflowExecutionContext workflowExecutionContext, |
| | 3903 | 35 | | ActivityExecutionContext? parentActivityExecutionContext, |
| | 3903 | 36 | | IActivity activity, |
| | 3903 | 37 | | ActivityDescriptor activityDescriptor, |
| | 3903 | 38 | | DateTimeOffset startedAt, |
| | 3903 | 39 | | object? tag, |
| | 3903 | 40 | | ISystemClock systemClock, |
| | 3903 | 41 | | CancellationToken cancellationToken) |
| | | 42 | | { |
| | 3903 | 43 | | _systemClock = systemClock; |
| | 3903 | 44 | | Properties = new ChangeTrackingDictionary<string, object>(Taint); |
| | 3903 | 45 | | Metadata = new ChangeTrackingDictionary<string, object>(Taint); |
| | 3903 | 46 | | ActivityState = new ChangeTrackingDictionary<string, object>(Taint); |
| | 3903 | 47 | | ActivityInput = new ChangeTrackingDictionary<string, object>(Taint); |
| | 3903 | 48 | | WorkflowExecutionContext = workflowExecutionContext; |
| | 3903 | 49 | | ParentActivityExecutionContext = parentActivityExecutionContext; |
| | 3903 | 50 | | var expressionExecutionContextProps = ExpressionExecutionContextExtensions.CreateActivityExecutionContextPropert |
| | 3903 | 51 | | expressionExecutionContextProps[ExpressionExecutionContextExtensions.ActivityKey] = activity; |
| | 3903 | 52 | | ExpressionExecutionContext = new(workflowExecutionContext.ServiceProvider, new(), parentActivityExecutionContext |
| | 3903 | 53 | | Activity = activity; |
| | 3903 | 54 | | ActivityDescriptor = activityDescriptor; |
| | 3903 | 55 | | StartedAt = startedAt; |
| | 3903 | 56 | | Status = ActivityStatus.Pending; |
| | 3903 | 57 | | Tag = tag; |
| | 3903 | 58 | | CancellationToken = cancellationToken; |
| | 3903 | 59 | | Id = id; |
| | 3903 | 60 | | _publisher = GetRequiredService<INotificationSender>(); |
| | 3903 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// The ID of the current activity execution context. |
| | | 65 | | /// </summary> |
| | 46570 | 66 | | public string Id { get; set; } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// The time at which the activity execution context was created. |
| | | 70 | | /// </summary> |
| | 8182 | 71 | | public DateTimeOffset StartedAt { get; set; } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// An optional tag to associate with the activity execution. |
| | | 75 | | /// </summary> |
| | 7500 | 76 | | public object? Tag { get; set; } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// The time at which the activity execution context was completed. |
| | | 80 | | /// </summary> |
| | 7682 | 81 | | public DateTimeOffset? CompletedAt { get; set; } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Returns true if the activity execution context has completed. |
| | | 85 | | /// </summary> |
| | 13506 | 86 | | public bool IsCompleted => Status is ActivityStatus.Completed or ActivityStatus.Canceled; |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets or sets a value indicating whether the activity is actively executing. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <remarks> |
| | | 92 | | /// This flag is set to <c>true</c> immediately before the activity begins execution |
| | | 93 | | /// and is set to <c>false</c> once the execution is completed. |
| | | 94 | | /// It can be used to determine if an activity was in-progress in case of unexpected |
| | | 95 | | /// application termination, allowing the system to retry execution upon restarting. |
| | | 96 | | /// </remarks> |
| | 7472 | 97 | | public bool IsExecuting { get; set; } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// The number of faults encountered during the execution of the activity and its descendants. |
| | | 101 | | /// </summary> |
| | 4361 | 102 | | public int AggregateFaultCount { get; set; } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// The workflow execution context. |
| | | 106 | | /// </summary> |
| | 169175 | 107 | | public WorkflowExecutionContext WorkflowExecutionContext { get; } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// The parent activity execution context, if any. |
| | | 111 | | /// This represents the structural container activity (e.g., Flowchart contains all its children). |
| | | 112 | | /// </summary> |
| | | 113 | | public ActivityExecutionContext? ParentActivityExecutionContext |
| | | 114 | | { |
| | 62284 | 115 | | get => _parentActivityExecutionContext; |
| | | 116 | | internal set |
| | | 117 | | { |
| | 4008 | 118 | | _parentActivityExecutionContext = value; |
| | 4008 | 119 | | _parentActivityExecutionContext?.Children.Add(this); |
| | 2920 | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// The ID of the activity execution context that scheduled this activity execution. |
| | | 125 | | /// This represents the temporal/execution predecessor that directly triggered execution of this activity, |
| | | 126 | | /// distinct from <see cref="ParentActivityExecutionContext"/> which represents the structural container. |
| | | 127 | | /// For example, in a Flowchart, Activity B might complete and schedule Activity C, making B the scheduling activity |
| | | 128 | | /// </summary> |
| | 7370 | 129 | | public string? SchedulingActivityExecutionId { get; set; } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// The ID of the activity that scheduled this activity execution (denormalized for convenience). |
| | | 133 | | /// This is the Activity.Id from the activity execution context identified by <see cref="SchedulingActivityExecution |
| | | 134 | | /// </summary> |
| | 6268 | 135 | | public string? SchedulingActivityId { get; set; } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// The workflow instance ID of the workflow that scheduled this activity execution. |
| | | 139 | | /// This is set when crossing workflow boundaries (e.g., via ExecuteWorkflow or DispatchWorkflow). |
| | | 140 | | /// For activities within the same workflow instance, this will be null. |
| | | 141 | | /// </summary> |
| | 7351 | 142 | | public string? SchedulingWorkflowInstanceId { get; set; } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// The depth of this activity in the call stack (0 for root activities). |
| | | 146 | | /// </summary> |
| | 9930 | 147 | | public int CallStackDepth { get; set; } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// The expression execution context. |
| | | 151 | | /// </summary> |
| | 45738 | 152 | | public ExpressionExecutionContext ExpressionExecutionContext { get; } |
| | | 153 | | |
| | | 154 | | /// <inheritdoc /> |
| | | 155 | | public IEnumerable<Variable> Variables |
| | | 156 | | { |
| | | 157 | | get |
| | | 158 | | { |
| | 6411 | 159 | | var containerVariables = (Activity as IVariableContainer)?.Variables ?? Enumerable.Empty<Variable>(); |
| | 6411 | 160 | | var dynamicVariables = DynamicVariables; |
| | 6411 | 161 | | var mergedVariables = new Dictionary<string, Variable>(); |
| | | 162 | | |
| | 13100 | 163 | | foreach (var containerVariable in containerVariables) |
| | | 164 | | { |
| | 139 | 165 | | var name = !string.IsNullOrEmpty(containerVariable.Name) ? containerVariable.Name : containerVariable.Id |
| | 139 | 166 | | mergedVariables[name] = containerVariable; |
| | | 167 | | } |
| | | 168 | | |
| | 13192 | 169 | | foreach (var dynamicVariable in dynamicVariables) |
| | | 170 | | { |
| | 185 | 171 | | var name = !string.IsNullOrEmpty(dynamicVariable.Name) ? dynamicVariable.Name : dynamicVariable.Id; |
| | 185 | 172 | | mergedVariables[name] = dynamicVariable; |
| | | 173 | | } |
| | 6411 | 174 | | return mergedVariables.Values; |
| | | 175 | | } |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// A list of variables that are dynamically added to the activity execution context. |
| | | 180 | | /// </summary> |
| | 11310 | 181 | | public ICollection<Variable> DynamicVariables { get; set; } = new List<Variable>(); |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// The currently executing activity. |
| | | 185 | | /// </summary> |
| | 204116 | 186 | | public IActivity Activity { get; set; } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// The activity descriptor. |
| | | 190 | | /// </summary> |
| | 26102 | 191 | | public ActivityDescriptor ActivityDescriptor { get; } |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// A cancellation token to use when invoking asynchronous operations. |
| | | 195 | | /// </summary> |
| | 34525 | 196 | | public CancellationToken CancellationToken { get; } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// The current status of the activity. |
| | | 200 | | /// </summary> |
| | | 201 | | public ActivityStatus Status |
| | | 202 | | { |
| | 40408 | 203 | | get => _status; |
| | | 204 | | private set |
| | | 205 | | { |
| | 11189 | 206 | | if (value == _status) |
| | 3943 | 207 | | return; |
| | | 208 | | |
| | 7246 | 209 | | _status = value; |
| | 7246 | 210 | | Taint(); |
| | 7246 | 211 | | } |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | public IDisposable EnterExecution() |
| | | 215 | | { |
| | 3325 | 216 | | return new WorkflowExecutionState(this); |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Sets the current status of the activity. |
| | | 221 | | /// </summary> |
| | | 222 | | public void TransitionTo(ActivityStatus status) |
| | | 223 | | { |
| | 7286 | 224 | | Status = status; |
| | 7286 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Gets or sets the exception that occurred during the activity execution, if any. |
| | | 229 | | /// </summary> |
| | | 230 | | public Exception? Exception |
| | | 231 | | { |
| | 3453 | 232 | | get => _exception; |
| | | 233 | | set |
| | | 234 | | { |
| | 15 | 235 | | _exception = value; |
| | 15 | 236 | | Taint(); |
| | 15 | 237 | | } |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <inheritdoc /> |
| | 12936 | 241 | | public IDictionary<string, object> Properties { get; private set; } |
| | | 242 | | |
| | | 243 | | /// <summary> |
| | | 244 | | /// A transient dictionary of values that can be associated with this activity execution context. |
| | | 245 | | /// These properties only exist while the activity executes and are not persisted. |
| | | 246 | | /// </summary> |
| | 44701 | 247 | | public IDictionary<object, object> TransientProperties { get; set; } = new Dictionary<object, object>(); |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// A dictionary of metadata about this execution. In contrast to <see cref="Properties"/>, this metadata is never p |
| | | 251 | | /// </summary> |
| | 8237 | 252 | | public IDictionary<string, object> Metadata { get; private set; } |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// Returns the <see cref="ActivityNode"/> metadata about the current activity. |
| | | 256 | | /// </summary> |
| | 32007 | 257 | | public ActivityNode ActivityNode => WorkflowExecutionContext.FindNodeByActivity(Activity)!; |
| | | 258 | | |
| | | 259 | | /// <summary> |
| | | 260 | | /// Returns the global node ID for the current activity within the graph. |
| | | 261 | | /// </summary> |
| | | 262 | | /// <remarks>As of tool version 3.0, all activity IDs are already unique, so there's no need to construct a hierarch |
| | 11183 | 263 | | public string NodeId => ActivityNode.NodeId; |
| | | 264 | | |
| | 14707 | 265 | | public ISet<ActivityExecutionContext> Children { get; } = new HashSet<ActivityExecutionContext>(); |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// A list of bookmarks associated with the current activity. |
| | | 269 | | /// </summary> |
| | 3641 | 270 | | public IEnumerable<Bookmark> Bookmarks => WorkflowExecutionContext.Bookmarks.Where(x => x.ActivityInstanceId == Id); |
| | | 271 | | |
| | | 272 | | /// <summary> |
| | | 273 | | /// A collection of bookmarks created during the execution of the activity. |
| | | 274 | | /// </summary> |
| | 685 | 275 | | public IEnumerable<Bookmark> NewBookmarks => _newBookmarks.AsReadOnly(); |
| | | 276 | | |
| | | 277 | | /// <summary> |
| | | 278 | | /// The number of times this <see cref="ActivityExecutionContext"/> has executed. |
| | | 279 | | /// </summary> |
| | 0 | 280 | | public long ExecutionCount => _executionCount; |
| | | 281 | | |
| | | 282 | | /// <summary> |
| | | 283 | | /// A dictionary of received inputs for the current workflow. |
| | | 284 | | /// </summary> |
| | 297 | 285 | | public IDictionary<string, object> WorkflowInput => WorkflowExecutionContext.Input; |
| | | 286 | | |
| | | 287 | | /// <summary> |
| | | 288 | | /// A dictionary of inputs for the current activity. |
| | | 289 | | /// </summary> |
| | 7936 | 290 | | public IDictionary<string, object> ActivityInput { get; private set; } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Journal data will be added to the workflow execution log for the "Executed" event. |
| | | 294 | | /// </summary> |
| | | 295 | | // ReSharper disable once CollectionNeverQueried.Global |
| | 7499 | 296 | | public IDictionary<string, object> JournalData { get; } = new Dictionary<string, object>(); |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Stores the evaluated inputs, serialized, for the current activity for historical purposes. |
| | | 300 | | /// </summary> |
| | 10565 | 301 | | public IDictionary<string, object> ActivityState { get; } |
| | | 302 | | |
| | | 303 | | /// <summary> |
| | | 304 | | /// Indicates whether the state of the current activity execution context has been modified. |
| | | 305 | | /// </summary> |
| | 31449 | 306 | | public bool IsDirty { get; private set; } |
| | | 307 | | |
| | | 308 | | /// <summary> |
| | | 309 | | /// Schedules the specified activity to be executed. |
| | | 310 | | /// </summary> |
| | | 311 | | /// <param name="activity">The activity to schedule.</param> |
| | | 312 | | /// <param name="completionCallback">An optional callback to invoke when the activity completes.</param> |
| | | 313 | | /// <param name="tag">An optional tag to associate with the activity execution.</param> |
| | | 314 | | /// <param name="variables">An optional list of variables to declare with the activity execution.</param> |
| | | 315 | | public ValueTask ScheduleActivityAsync(IActivity? activity, ActivityCompletionCallback? completionCallback, object? |
| | | 316 | | { |
| | 2132 | 317 | | var options = new ScheduleWorkOptions |
| | 2132 | 318 | | { |
| | 2132 | 319 | | CompletionCallback = completionCallback, |
| | 2132 | 320 | | Tag = tag, |
| | 2132 | 321 | | Variables = variables?.ToList() |
| | 2132 | 322 | | }; |
| | 2132 | 323 | | return ScheduleActivityAsync(activity, options); |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// Schedules the specified activity to be executed. |
| | | 328 | | /// </summary> |
| | | 329 | | /// <param name="activity">The activity to schedule.</param> |
| | | 330 | | /// <param name="options">The options used to schedule the activity.</param> |
| | | 331 | | public async ValueTask ScheduleActivityAsync(IActivity? activity, ScheduleWorkOptions? options = null) |
| | | 332 | | { |
| | 2968 | 333 | | await ScheduleActivityAsync(activity, this, options); |
| | 2968 | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Schedules the specified activity to be executed. |
| | | 338 | | /// </summary> |
| | | 339 | | /// <param name="activity">The activity to schedule.</param> |
| | | 340 | | /// <param name="owner">The activity execution context that owns the scheduled activity.</param> |
| | | 341 | | /// <param name="options">The options used to schedule the activity.</param> |
| | | 342 | | public async ValueTask ScheduleActivityAsync(IActivity? activity, ActivityExecutionContext? owner, ScheduleWorkOptio |
| | | 343 | | { |
| | 2968 | 344 | | var schedulerStrategy = GetRequiredService<IActivityExecutionContextSchedulerStrategy>(); |
| | 2968 | 345 | | await schedulerStrategy.ScheduleActivityAsync(this, activity, owner, options); |
| | 2968 | 346 | | } |
| | | 347 | | |
| | | 348 | | /// <summary> |
| | | 349 | | /// Schedules the specified activity to be executed. |
| | | 350 | | /// </summary> |
| | | 351 | | /// <param name="activityNode">The activity node to schedule.</param> |
| | | 352 | | /// <param name="owner">The activity execution context that owns the scheduled activity.</param> |
| | | 353 | | /// <param name="options">The options used to schedule the activity.</param> |
| | | 354 | | public async Task ScheduleActivityAsync(ActivityNode? activityNode, ActivityExecutionContext? owner = null, Schedule |
| | | 355 | | { |
| | 0 | 356 | | var schedulerStrategy = GetRequiredService<IActivityExecutionContextSchedulerStrategy>(); |
| | 0 | 357 | | await schedulerStrategy.ScheduleActivityAsync(this, activityNode, owner, options); |
| | 0 | 358 | | } |
| | | 359 | | |
| | | 360 | | /// <summary> |
| | | 361 | | /// Schedules the specified activities to be executed. |
| | | 362 | | /// </summary> |
| | | 363 | | /// <param name="activities">The activities to schedule.</param> |
| | 0 | 364 | | public async ValueTask ScheduleActivitiesAsync(params IActivity?[] activities) => await ScheduleActivities(activitie |
| | | 365 | | |
| | | 366 | | /// <summary> |
| | | 367 | | /// Schedules the specified activities to be executed. |
| | | 368 | | /// </summary> |
| | | 369 | | /// <param name="activities">The activities to schedule.</param> |
| | | 370 | | /// <param name="completionCallback">The callback to invoke when the activities complete.</param> |
| | | 371 | | /// <param name="tag">An optional tag to associate with the activity execution.</param> |
| | | 372 | | /// <param name="variables">An optional list of variables to declare with the activity execution.</param> |
| | | 373 | | public ValueTask ScheduleActivities(IEnumerable<IActivity?> activities, ActivityCompletionCallback? completionCallba |
| | | 374 | | { |
| | 7 | 375 | | var options = new ScheduleWorkOptions |
| | 7 | 376 | | { |
| | 7 | 377 | | CompletionCallback = completionCallback, |
| | 7 | 378 | | Tag = tag, |
| | 7 | 379 | | Variables = variables?.ToList() |
| | 7 | 380 | | }; |
| | 7 | 381 | | return ScheduleActivities(activities, options); |
| | | 382 | | } |
| | | 383 | | |
| | | 384 | | /// <summary> |
| | | 385 | | /// Schedules the specified activities to be executed. |
| | | 386 | | /// </summary> |
| | | 387 | | /// <param name="activities">The activities to schedule.</param> |
| | | 388 | | /// <param name="options">The options used to schedule the activities.</param> |
| | | 389 | | public async ValueTask ScheduleActivities(IEnumerable<IActivity?> activities, ScheduleWorkOptions? options = null) |
| | | 390 | | { |
| | 42 | 391 | | foreach (var activity in activities) |
| | 14 | 392 | | await ScheduleActivityAsync(activity, options); |
| | 7 | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary> |
| | | 396 | | /// Creates a bookmark for each payload. |
| | | 397 | | /// </summary> |
| | | 398 | | /// <param name="payloads">The payloads to create bookmarks for.</param> |
| | | 399 | | /// <param name="callback">An optional callback that is invoked when the bookmark is resumed.</param> |
| | | 400 | | /// <param name="includeActivityInstanceId">Whether or not the activity instance ID should be included in the bookma |
| | | 401 | | /// <param name="bookmarkName">An optional name to use for the bookmark. Defaults to the activity type.</param> |
| | | 402 | | public void CreateBookmarks(IEnumerable<object> payloads, ExecuteActivityDelegate? callback = null, bool includeActi |
| | | 403 | | { |
| | 28 | 404 | | foreach (var payload in payloads) |
| | 7 | 405 | | CreateBookmark(new() |
| | 7 | 406 | | { |
| | 7 | 407 | | Stimulus = payload, |
| | 7 | 408 | | Callback = callback, |
| | 7 | 409 | | BookmarkName = bookmarkName, |
| | 7 | 410 | | IncludeActivityInstanceId = includeActivityInstanceId |
| | 7 | 411 | | }); |
| | 7 | 412 | | } |
| | | 413 | | |
| | | 414 | | /// <summary> |
| | | 415 | | /// Adds each bookmark to the list of bookmarks. |
| | | 416 | | /// </summary> |
| | | 417 | | /// <param name="bookmarks">The bookmarks to add.</param> |
| | | 418 | | public void AddBookmarks(IEnumerable<Bookmark> bookmarks) |
| | | 419 | | { |
| | 0 | 420 | | WorkflowExecutionContext.Bookmarks.AddRange(bookmarks); |
| | 0 | 421 | | Taint(); |
| | 0 | 422 | | } |
| | | 423 | | |
| | | 424 | | /// <summary> |
| | | 425 | | /// Adds a bookmark to the list of bookmarks. |
| | | 426 | | /// </summary> |
| | | 427 | | /// <param name="bookmark">The bookmark to add.</param> |
| | | 428 | | public void AddBookmark(Bookmark bookmark) |
| | | 429 | | { |
| | 74 | 430 | | _newBookmarks.Add(bookmark); |
| | 74 | 431 | | WorkflowExecutionContext.Bookmarks.Add(bookmark); |
| | 74 | 432 | | Taint(); |
| | 74 | 433 | | } |
| | | 434 | | |
| | | 435 | | /// <summary> |
| | | 436 | | /// Creates a bookmark so that this activity can be resumed at a later time. |
| | | 437 | | /// </summary> |
| | | 438 | | /// <param name="callback">An optional callback that is invoked when the bookmark is resumed.</param> |
| | | 439 | | /// <param name="metadata">Custom properties to associate with the bookmark.</param> |
| | | 440 | | /// <returns>The created bookmark.</returns> |
| | | 441 | | public Bookmark CreateBookmark(ExecuteActivityDelegate callback, IDictionary<string, string>? metadata = null) |
| | | 442 | | { |
| | 0 | 443 | | return CreateBookmark(new() |
| | 0 | 444 | | { |
| | 0 | 445 | | Callback = callback, |
| | 0 | 446 | | Metadata = metadata |
| | 0 | 447 | | }); |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | /// <summary> |
| | | 451 | | /// Creates a bookmark so that this activity can be resumed at a later time. |
| | | 452 | | /// </summary> |
| | | 453 | | /// <param name="stimulus">The payload to associate with the bookmark.</param> |
| | | 454 | | /// <param name="callback">An optional callback that is invoked when the bookmark is resumed.</param> |
| | | 455 | | /// <param name="includeActivityInstanceId">Whether or not the activity instance ID should be included in the bookma |
| | | 456 | | /// <param name="customProperties">Custom properties to associate with the bookmark.</param> |
| | | 457 | | /// <returns>The created bookmark.</returns> |
| | | 458 | | public Bookmark CreateBookmark(object stimulus, ExecuteActivityDelegate? callback, bool includeActivityInstanceId = |
| | | 459 | | { |
| | 0 | 460 | | return CreateBookmark(new() |
| | 0 | 461 | | { |
| | 0 | 462 | | Stimulus = stimulus, |
| | 0 | 463 | | Callback = callback, |
| | 0 | 464 | | IncludeActivityInstanceId = includeActivityInstanceId, |
| | 0 | 465 | | Metadata = customProperties |
| | 0 | 466 | | }); |
| | | 467 | | } |
| | | 468 | | |
| | | 469 | | /// <summary> |
| | | 470 | | /// Creates a bookmark for the current activity execution context. |
| | | 471 | | /// </summary> |
| | | 472 | | /// <param name="stimulus">The payload to associate with the bookmark.</param> |
| | | 473 | | /// <param name="includeActivityInstanceId">Specifies whether to include the activity instance ID in the bookmark in |
| | | 474 | | /// <param name="customProperties">Additional custom properties to associate with the bookmark. Defaults to null.</p |
| | | 475 | | /// <returns>The created bookmark.</returns> |
| | | 476 | | public Bookmark CreateBookmark(object stimulus, bool includeActivityInstanceId, IDictionary<string, string>? customP |
| | | 477 | | { |
| | 0 | 478 | | return CreateBookmark(new() |
| | 0 | 479 | | { |
| | 0 | 480 | | Stimulus = stimulus, |
| | 0 | 481 | | IncludeActivityInstanceId = includeActivityInstanceId, |
| | 0 | 482 | | Metadata = customProperties |
| | 0 | 483 | | }); |
| | | 484 | | } |
| | | 485 | | |
| | | 486 | | /// <summary> |
| | | 487 | | /// Creates a bookmark so that this activity can be resumed at a later time. |
| | | 488 | | /// </summary> |
| | | 489 | | /// <param name="stimulus">The payload to associate with the bookmark.</param> |
| | | 490 | | /// <param name="metadata">Custom properties to associate with the bookmark.</param> |
| | | 491 | | /// <returns>The created bookmark.</returns> |
| | | 492 | | public Bookmark CreateBookmark(object stimulus, IDictionary<string, string>? metadata = null) |
| | | 493 | | { |
| | 9 | 494 | | return CreateBookmark(new() |
| | 9 | 495 | | { |
| | 9 | 496 | | Stimulus = stimulus, |
| | 9 | 497 | | Metadata = metadata |
| | 9 | 498 | | }); |
| | | 499 | | } |
| | | 500 | | |
| | | 501 | | /// <summary> |
| | | 502 | | /// Creates a bookmark so that this activity can be resumed at a later time. |
| | | 503 | | /// Creating a bookmark will automatically suspend the workflow after all pending activities have executed. |
| | | 504 | | /// </summary> |
| | | 505 | | public Bookmark CreateBookmark(CreateBookmarkArgs? options = null) |
| | | 506 | | { |
| | 74 | 507 | | var payload = options?.Stimulus; |
| | 74 | 508 | | var callback = options?.Callback; |
| | 74 | 509 | | var bookmarkName = options?.BookmarkName ?? Activity.Type; |
| | 74 | 510 | | var bookmarkHasher = GetRequiredService<IStimulusHasher>(); |
| | 74 | 511 | | var identityGenerator = GetRequiredService<IIdentityGenerator>(); |
| | 74 | 512 | | var includeActivityInstanceId = options?.IncludeActivityInstanceId ?? true; |
| | 74 | 513 | | var hash = bookmarkHasher.Hash(bookmarkName, payload, includeActivityInstanceId ? Id : null); |
| | 74 | 514 | | var bookmarkId = options?.BookmarkId ?? identityGenerator.GenerateId(); |
| | | 515 | | |
| | 74 | 516 | | var bookmark = new Bookmark( |
| | 74 | 517 | | bookmarkId, |
| | 74 | 518 | | bookmarkName, |
| | 74 | 519 | | hash, |
| | 74 | 520 | | payload, |
| | 74 | 521 | | Activity.Id, |
| | 74 | 522 | | ActivityNode.NodeId, |
| | 74 | 523 | | Id, |
| | 74 | 524 | | _systemClock.UtcNow, |
| | 74 | 525 | | options?.AutoBurn ?? true, |
| | 74 | 526 | | callback?.Method.Name, |
| | 74 | 527 | | options?.AutoComplete ?? true, |
| | 74 | 528 | | options?.Metadata); |
| | | 529 | | |
| | 74 | 530 | | AddBookmark(bookmark); |
| | 74 | 531 | | return bookmark; |
| | | 532 | | } |
| | | 533 | | |
| | | 534 | | /// <summary> |
| | | 535 | | /// Clear all bookmarks. |
| | | 536 | | /// </summary> |
| | | 537 | | public void ClearBookmarks() |
| | | 538 | | { |
| | 3413 | 539 | | _newBookmarks.Clear(); |
| | 3489 | 540 | | WorkflowExecutionContext.Bookmarks.RemoveWhere(x => x.ActivityInstanceId == Id); |
| | 3413 | 541 | | Taint(); |
| | 3413 | 542 | | } |
| | | 543 | | |
| | | 544 | | /// <summary> |
| | | 545 | | /// Returns a property value associated with the current activity context. |
| | | 546 | | /// </summary> |
| | 1421 | 547 | | public T? GetProperty<T>(string key) => Properties.TryGetValue<T?>(key, out var value) ? value : default; |
| | | 548 | | |
| | | 549 | | /// <summary> |
| | | 550 | | /// Returns a property value associated with the current activity context. |
| | | 551 | | /// </summary> |
| | | 552 | | public T GetProperty<T>(string key, Func<T> defaultValue) |
| | | 553 | | { |
| | 136 | 554 | | if (Properties.TryGetValue<T?>(key, out var value)) |
| | 102 | 555 | | return value!; |
| | | 556 | | |
| | 34 | 557 | | value = defaultValue(); |
| | 34 | 558 | | Properties[key] = value!; |
| | | 559 | | |
| | 34 | 560 | | return value!; |
| | | 561 | | } |
| | | 562 | | |
| | | 563 | | /// <summary> |
| | | 564 | | /// Stores a property associated with the current activity context. |
| | | 565 | | /// </summary> |
| | 123 | 566 | | public void SetProperty<T>(string key, T? value) => Properties[key] = value!; |
| | | 567 | | |
| | | 568 | | /// <summary> |
| | | 569 | | /// Updates a property associated with the current activity context. |
| | | 570 | | /// </summary> |
| | | 571 | | public T UpdateProperty<T>(string key, Func<T?, T> updater) where T : notnull |
| | | 572 | | { |
| | 382 | 573 | | var value = GetProperty<T?>(key); |
| | 382 | 574 | | value = updater(value); |
| | 382 | 575 | | Properties[key] = value; |
| | 382 | 576 | | return value; |
| | | 577 | | } |
| | | 578 | | |
| | | 579 | | /// <summary> |
| | | 580 | | /// Removes a property associated with the current activity context. |
| | | 581 | | /// </summary> |
| | | 582 | | /// <param name="key">The property key.</param> |
| | 0 | 583 | | public void RemoveProperty(string key) => Properties.Remove(key); |
| | | 584 | | |
| | | 585 | | /// <summary> |
| | | 586 | | /// Returns a metadata value associated with the current activity context. |
| | | 587 | | /// </summary> |
| | 0 | 588 | | public T? GetMetadata<T>(string key) => Metadata.TryGetValue<T?>(key, out var value) ? value : default; |
| | | 589 | | |
| | | 590 | | /// <summary> |
| | | 591 | | /// Returns a metadata value associated with the current activity context. |
| | | 592 | | /// </summary> |
| | | 593 | | public T GetMetadata<T>(string key, Func<T> defaultValue) |
| | | 594 | | { |
| | 29 | 595 | | if (Metadata.TryGetValue<T?>(key, out var value)) |
| | 13 | 596 | | return value!; |
| | | 597 | | |
| | 16 | 598 | | value = defaultValue(); |
| | 16 | 599 | | Metadata[key] = value!; |
| | | 600 | | |
| | 16 | 601 | | return value!; |
| | | 602 | | } |
| | | 603 | | |
| | | 604 | | /// <summary> |
| | | 605 | | /// Stores a metadata value associated with the current activity context. |
| | | 606 | | /// </summary> |
| | 4 | 607 | | public void SetMetadata<T>(string key, T? value) => Metadata[key] = value!; |
| | | 608 | | |
| | | 609 | | /// <summary> |
| | | 610 | | /// Updates a metadata value associated with the current activity context. |
| | | 611 | | /// </summary> |
| | | 612 | | public T UpdateMetadata<T>(string key, Func<T?, T> updater) where T : notnull |
| | | 613 | | { |
| | 0 | 614 | | var value = GetMetadata<T?>(key); |
| | 0 | 615 | | value = updater(value); |
| | 0 | 616 | | Metadata[key] = value; |
| | 0 | 617 | | return value; |
| | | 618 | | } |
| | | 619 | | |
| | | 620 | | /// <summary> |
| | | 621 | | /// Removes a metadata value associated with the current activity context. |
| | | 622 | | /// </summary> |
| | | 623 | | /// <param name="key">The metadata key.</param> |
| | 0 | 624 | | public void RemoveMetadata(string key) => Metadata.Remove(key); |
| | | 625 | | |
| | | 626 | | /// <summary> |
| | | 627 | | /// Resolves a required service using the service provider. |
| | | 628 | | /// </summary> |
| | | 629 | | /// <typeparam name="T">The service type.</typeparam> |
| | | 630 | | /// <returns>The resolved service.</returns> |
| | 33657 | 631 | | public T GetRequiredService<T>() where T : notnull => WorkflowExecutionContext.GetRequiredService<T>(); |
| | | 632 | | |
| | | 633 | | /// <summary> |
| | | 634 | | /// Resolves a required service using the service provider. |
| | | 635 | | /// </summary> |
| | | 636 | | /// <param name="serviceType">The service type.</param> |
| | | 637 | | /// <returns>The resolved service.</returns> |
| | 3054 | 638 | | public object GetRequiredService(Type serviceType) => WorkflowExecutionContext.GetRequiredService(serviceType); |
| | | 639 | | |
| | | 640 | | /// <summary> |
| | | 641 | | /// Resolves a service using the service provider. If not found, a new instance is created. |
| | | 642 | | /// </summary> |
| | | 643 | | /// <typeparam name="T">The service type.</typeparam> |
| | | 644 | | /// <returns>The resolved service.</returns> |
| | 0 | 645 | | public T GetOrCreateService<T>() where T : notnull => WorkflowExecutionContext.GetOrCreateService<T>(); |
| | | 646 | | |
| | | 647 | | /// <summary> |
| | | 648 | | /// Resolves a service using the service provider. If not found, a new instance is created. |
| | | 649 | | /// </summary> |
| | | 650 | | /// <param name="serviceType">The service type.</param> |
| | | 651 | | /// <returns>The resolved service.</returns> |
| | 0 | 652 | | public object GetOrCreateService(Type serviceType) => WorkflowExecutionContext.GetOrCreateService(serviceType); |
| | | 653 | | |
| | | 654 | | /// <summary> |
| | | 655 | | /// Resolves a service using the service provider. |
| | | 656 | | /// </summary> |
| | | 657 | | /// <typeparam name="T">The service type.</typeparam> |
| | | 658 | | /// <returns>The resolved service.</returns> |
| | 590 | 659 | | public T? GetService<T>() where T : notnull => WorkflowExecutionContext.GetService<T>(); |
| | | 660 | | |
| | | 661 | | /// <summary> |
| | | 662 | | /// Resolves all services of the specified type using the service provider. |
| | | 663 | | /// </summary> |
| | | 664 | | /// <typeparam name="T">The service type.</typeparam> |
| | | 665 | | /// <returns>The resolved services.</returns> |
| | 266 | 666 | | public IEnumerable<T> GetServices<T>() where T : notnull => WorkflowExecutionContext.GetServices<T>(); |
| | | 667 | | |
| | | 668 | | /// <summary> |
| | | 669 | | /// Resolves a service using the service provider. |
| | | 670 | | /// </summary> |
| | | 671 | | /// <param name="serviceType">The service type.</param> |
| | | 672 | | /// <returns>The resolved service.</returns> |
| | 0 | 673 | | public object? GetService(Type serviceType) => WorkflowExecutionContext.GetService(serviceType); |
| | | 674 | | |
| | | 675 | | /// <summary> |
| | | 676 | | /// Gets the value of the specified input. |
| | | 677 | | /// </summary> |
| | | 678 | | /// <param name="input">The input.</param> |
| | | 679 | | /// <typeparam name="T">The type of the input.</typeparam> |
| | | 680 | | /// <returns>The input value.</returns> |
| | 2860 | 681 | | public T? Get<T>(Input<T>? input) => input == null ? default : Get<T>(input.MemoryBlockReference()); |
| | | 682 | | |
| | | 683 | | /// <summary> |
| | | 684 | | /// Gets the value of the specified output. |
| | | 685 | | /// </summary> |
| | | 686 | | /// <param name="output">The output.</param> |
| | | 687 | | /// <typeparam name="T">The type of the output.</typeparam> |
| | | 688 | | /// <returns>The output value.</returns> |
| | 0 | 689 | | public T? Get<T>(Output<T>? output) => output == null ? default : Get<T>(output.MemoryBlockReference()); |
| | | 690 | | |
| | | 691 | | /// <summary> |
| | | 692 | | /// Gets the value of the specified output. |
| | | 693 | | /// </summary> |
| | | 694 | | /// <param name="output">The output.</param> |
| | | 695 | | /// <returns>The output value.</returns> |
| | 4 | 696 | | public object? Get(Output? output) => output == null ? null : Get(output.MemoryBlockReference()); |
| | | 697 | | |
| | | 698 | | /// <summary> |
| | | 699 | | /// Gets the value of the specified memory block. |
| | | 700 | | /// </summary> |
| | | 701 | | /// <param name="blockReference">The memory block reference.</param> |
| | | 702 | | /// <returns>The memory block value.</returns> |
| | | 703 | | /// <exception cref="InvalidOperationException">The memory block does not exist.</exception> |
| | | 704 | | public object? Get(MemoryBlockReference blockReference) |
| | | 705 | | { |
| | 2257 | 706 | | return !TryGet(blockReference, out var value) |
| | 2257 | 707 | | ? throw new InvalidOperationException($"The memory block '{blockReference}' does not exist.") |
| | 2257 | 708 | | : value; |
| | | 709 | | } |
| | | 710 | | |
| | | 711 | | /// <summary> |
| | | 712 | | /// Gets the value of the specified memory block. |
| | | 713 | | /// </summary> |
| | | 714 | | /// <param name="blockReference">The memory block reference.</param> |
| | | 715 | | /// <typeparam name="T">The type of the memory block.</typeparam> |
| | | 716 | | /// <returns>The memory block value.</returns> |
| | | 717 | | public T? Get<T>(MemoryBlockReference blockReference) |
| | | 718 | | { |
| | 2127 | 719 | | var value = Get(blockReference); |
| | 2127 | 720 | | return value != null ? value.ConvertTo<T>() : default; |
| | | 721 | | } |
| | | 722 | | |
| | | 723 | | /// <summary> |
| | | 724 | | /// Tries to get the value of the specified memory block. |
| | | 725 | | /// </summary> |
| | | 726 | | /// <param name="blockReference">The memory block reference.</param> |
| | | 727 | | /// <param name="value">The memory block value.</param> |
| | | 728 | | /// <returns>True if the memory block exists, false otherwise.</returns> |
| | | 729 | | public bool TryGet(MemoryBlockReference blockReference, out object? value) |
| | | 730 | | { |
| | | 731 | | // First, try to get the value from the memory register |
| | 2443 | 732 | | var memoryBlock = GetMemoryBlock(blockReference); |
| | | 733 | | |
| | 2443 | 734 | | if (memoryBlock != null) |
| | | 735 | | { |
| | 2443 | 736 | | value = memoryBlock.Value; |
| | 2443 | 737 | | return true; |
| | | 738 | | } |
| | | 739 | | |
| | | 740 | | // Handle Literal references as a fallback - they can hold their value directly |
| | 0 | 741 | | if (blockReference is Literal literal) |
| | | 742 | | { |
| | 0 | 743 | | value = literal.Value; |
| | 0 | 744 | | return true; |
| | | 745 | | } |
| | | 746 | | |
| | 0 | 747 | | value = null; |
| | 0 | 748 | | return false; |
| | | 749 | | } |
| | | 750 | | |
| | | 751 | | /// <summary> |
| | | 752 | | /// Sets a value at the specified memory block. |
| | | 753 | | /// </summary> |
| | | 754 | | /// <param name="blockReference">The memory block reference.</param> |
| | | 755 | | /// <param name="value">The value to set.</param> |
| | | 756 | | /// <param name="configure">An optional callback that can be used to configure the memory block.</param> |
| | 6 | 757 | | public void Set(MemoryBlockReference blockReference, object? value, Action<MemoryBlock>? configure = null) => Expres |
| | | 758 | | |
| | | 759 | | /// <summary> |
| | | 760 | | /// Sets a value at the specified output. |
| | | 761 | | /// </summary> |
| | | 762 | | /// <param name="output">The output.</param> |
| | | 763 | | /// <param name="value">The value to set.</param> |
| | | 764 | | /// <param name="outputName">The name of the output.</param> |
| | | 765 | | /// <typeparam name="T">The type of the output.</typeparam> |
| | 1694 | 766 | | public void Set<T>(Output<T>? output, T? value, [CallerArgumentExpression("output")] string? outputName = null) => S |
| | | 767 | | |
| | | 768 | | /// <summary> |
| | | 769 | | /// Sets a value at the specified output. |
| | | 770 | | /// </summary> |
| | | 771 | | /// <param name="output">The output.</param> |
| | | 772 | | /// <param name="value">The value to set.</param> |
| | | 773 | | /// <param name="outputName">The name of the output.</param> |
| | | 774 | | public void Set(Output? output, object? value, [CallerArgumentExpression("output")] string? outputName = null) |
| | | 775 | | { |
| | | 776 | | // Store the value in the expression execution memory block. |
| | 1722 | 777 | | ExpressionExecutionContext.Set(output, value); |
| | | 778 | | |
| | | 779 | | // Also store the value in the workflow execution transient activity output register. |
| | 1722 | 780 | | WorkflowExecutionContext.RecordActivityOutput(this, outputName, value); |
| | 1722 | 781 | | } |
| | | 782 | | |
| | | 783 | | /// <summary> |
| | | 784 | | /// Removes all completion callbacks for the current activity. |
| | | 785 | | /// </summary> |
| | | 786 | | public void ClearCompletionCallbacks() |
| | | 787 | | { |
| | 4952 | 788 | | var entriesToRemove = WorkflowExecutionContext.CompletionCallbacks.Where(x => x.Owner == this).ToList(); |
| | 3413 | 789 | | WorkflowExecutionContext.RemoveCompletionCallbacks(entriesToRemove); |
| | 3413 | 790 | | } |
| | | 791 | | |
| | | 792 | | public void Taint() |
| | | 793 | | { |
| | 17318 | 794 | | if (!IsDirty) |
| | 3865 | 795 | | IsDirty = true; |
| | 17318 | 796 | | } |
| | | 797 | | |
| | | 798 | | public void ClearTaint() |
| | | 799 | | { |
| | 3422 | 800 | | if (IsDirty) |
| | 3422 | 801 | | IsDirty = false; |
| | 3422 | 802 | | } |
| | | 803 | | |
| | | 804 | | internal void IncrementExecutionCount() |
| | | 805 | | { |
| | 3312 | 806 | | _executionCount++; |
| | 3312 | 807 | | } |
| | | 808 | | |
| | | 809 | | private MemoryBlock? GetMemoryBlock(MemoryBlockReference locationBlockReference) |
| | | 810 | | { |
| | 2443 | 811 | | return ExpressionExecutionContext.TryGetBlock(locationBlockReference, out var memoryBlock) ? memoryBlock : null; |
| | | 812 | | } |
| | | 813 | | |
| | | 814 | | void IDisposable.Dispose() |
| | | 815 | | { |
| | 0 | 816 | | } |
| | | 817 | | } |
| | | 818 | | |