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