| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using Elsa.Common; |
| | | 5 | | using Elsa.Expressions.Contracts; |
| | | 6 | | using Elsa.Expressions.Helpers; |
| | | 7 | | using Elsa.Expressions.Models; |
| | | 8 | | using Elsa.Mediator.Contracts; |
| | | 9 | | using Elsa.Workflows; |
| | | 10 | | using Elsa.Workflows.Attributes; |
| | | 11 | | using Elsa.Workflows.Memory; |
| | | 12 | | using Elsa.Workflows.Models; |
| | | 13 | | using Elsa.Workflows.Notifications; |
| | | 14 | | using Elsa.Workflows.Signals; |
| | | 15 | | using Elsa.Workflows.State; |
| | | 16 | | using JetBrains.Annotations; |
| | | 17 | | using Microsoft.Extensions.Logging; |
| | | 18 | | |
| | | 19 | | // ReSharper disable once CheckNamespace |
| | | 20 | | namespace Elsa.Extensions; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Provides extension methods for <see cref="ActivityExecutionContext"/>. |
| | | 24 | | /// </summary> |
| | | 25 | | [PublicAPI] |
| | | 26 | | public static partial class ActivityExecutionContextExtensions |
| | | 27 | | { |
| | | 28 | | private const string ExtensionsMetadataKey = "Extensions"; |
| | | 29 | | |
| | | 30 | | /// <param name="context">The <see cref="ActivityExecutionContext"/></param> |
| | | 31 | | extension(ActivityExecutionContext context) |
| | | 32 | | { |
| | | 33 | | /// <summary> |
| | | 34 | | /// Attempts to get a value from the input provided via <see cref="WorkflowExecutionContext"/>. If a value was f |
| | | 35 | | /// </summary> |
| | | 36 | | public bool TryGetWorkflowInput<T>(string key, out T value, JsonSerializerOptions? serializerOptions = null) |
| | | 37 | | { |
| | 14 | 38 | | var wellKnownTypeRegistry = context.GetRequiredService<IWellKnownTypeRegistry>(); |
| | | 39 | | |
| | 14 | 40 | | if (context.WorkflowInput.TryGetValue(key, out var v)) |
| | | 41 | | { |
| | 13 | 42 | | value = v.ConvertTo<T>(new(serializerOptions, wellKnownTypeRegistry))!; |
| | 13 | 43 | | return true; |
| | | 44 | | } |
| | | 45 | | |
| | 1 | 46 | | value = default!; |
| | 1 | 47 | | return false; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets a value from the input provided via <see cref="WorkflowExecutionContext"/>. If a value was found, an at |
| | | 52 | | /// </summary> |
| | 1 | 53 | | public T GetWorkflowInput<T>(JsonSerializerOptions? serializerOptions = null) => context.GetWorkflowInput<T>(typ |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets a value from the input provided via <see cref="WorkflowExecutionContext"/>. If a value was found, an at |
| | | 57 | | /// </summary> |
| | | 58 | | public T GetWorkflowInput<T>(string key, JsonSerializerOptions? serializerOptions = null) |
| | | 59 | | { |
| | 263 | 60 | | var wellKnownTypeRegistry = context.GetRequiredService<IWellKnownTypeRegistry>(); |
| | 263 | 61 | | return context.WorkflowInput[key].ConvertTo<T>(new(serializerOptions, wellKnownTypeRegistry))!; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Sets the Result property of the specified activity. |
| | | 66 | | /// </summary> |
| | | 67 | | /// being extended. |
| | | 68 | | /// <param name="value">The value to set.</param> |
| | | 69 | | /// <exception cref="Exception">Thrown when the specified activity does not implement <see cref="IActivityWithRe |
| | | 70 | | public void SetResult(object? value) |
| | | 71 | | { |
| | 22 | 72 | | var activity = context.Activity as IActivityWithResult ?? throw new($"Cannot set result on activity {context |
| | 21 | 73 | | context.Set(activity.Result, value, "Result"); |
| | 21 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Returns true if this activity is triggered for the first time and not being resumed. |
| | | 78 | | /// </summary> |
| | 307 | 79 | | public bool IsTriggerOfWorkflow() => context.WorkflowExecutionContext.TriggerActivityId == context.Activity.Id; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Creates a workflow variable by name and optionally sets the value. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name="name">The name of the variable.</param> |
| | | 85 | | /// <param name="value">The value of the variable.</param> |
| | | 86 | | /// <param name="storageDriverType">The type of storage driver to use for the variable.</param> |
| | | 87 | | /// <param name="configure">A callback to configure the memory block.</param> |
| | | 88 | | /// <returns>The created <see cref="Variable"/>.</returns> |
| | | 89 | | public Variable CreateVariable(string name, object? value, Type? storageDriverType = null, Action<MemoryBlock>? |
| | | 90 | | { |
| | 1 | 91 | | return context.ExpressionExecutionContext.CreateVariable(name, value, storageDriverType, configure); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Sets a workflow variable by name. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="name">The name of the variable.</param> |
| | | 98 | | /// <param name="value">The value of the variable.</param> |
| | | 99 | | /// <param name="configure">A callback to configure the memory block.</param> |
| | | 100 | | /// <returns>The created <see cref="Variable"/>.</returns> |
| | | 101 | | public Variable SetVariable(string name, object? value, Action<MemoryBlock>? configure = null) => |
| | 10 | 102 | | context.ExpressionExecutionContext.SetVariable(name, value, configure); |
| | | 103 | | |
| | | 104 | | public Variable SetDynamicVariable<T>(string name, T value, Action<MemoryBlock>? configure = null) |
| | | 105 | | { |
| | | 106 | | // Check if a predefined variable already exists. |
| | 0 | 107 | | var predefinedVariable = context.ExpressionExecutionContext.GetVariable(name); |
| | | 108 | | |
| | 0 | 109 | | if (predefinedVariable != null) |
| | | 110 | | { |
| | 0 | 111 | | context.SetVariable(name, value); |
| | 0 | 112 | | return predefinedVariable; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | // No predefined variable exists, so we will add a dynamic variable to the current container in scope. |
| | 0 | 116 | | var container = context.FindParentWithVariableContainer(); |
| | | 117 | | |
| | 0 | 118 | | if (container == null) |
| | 0 | 119 | | throw new("No parent variable container found"); |
| | | 120 | | |
| | 0 | 121 | | var existingVariable = container.DynamicVariables.FirstOrDefault(x => x.Name == name); |
| | | 122 | | |
| | 0 | 123 | | if (existingVariable == null) |
| | 0 | 124 | | container.DynamicVariables.Add(new Variable<T>(name, value)); |
| | | 125 | | |
| | 0 | 126 | | return context.SetVariable(name, value); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets a workflow variable by name. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <param name="name">The name of the variable.</param> |
| | | 133 | | /// <typeparam name="T">The type of the variable.</typeparam> |
| | | 134 | | /// <returns>The variable if found, otherwise null.</returns> |
| | 9 | 135 | | public T? GetVariable<T>(string name) => context.ExpressionExecutionContext.GetVariable<T?>(name); |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Returns a dictionary of variable keys and their values across scopes. |
| | | 139 | | /// </summary> |
| | 1 | 140 | | public IDictionary<string, object> GetVariableValues() => context.ExpressionExecutionContext.ReadAndFlattenMemor |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Returns a set of tuples containing the activity and its descriptor for all activities with outputs. |
| | | 144 | | /// </summary> |
| | | 145 | | public async IAsyncEnumerable<(IActivity Activity, ActivityDescriptor ActivityDescriptor)> GetActivitiesWithOutp |
| | | 146 | | { |
| | | 147 | | // Get current container. |
| | 0 | 148 | | var currentContainerNode = context.FindParentWithVariableContainer()?.ActivityNode; |
| | | 149 | | |
| | 0 | 150 | | if (currentContainerNode == null) |
| | 0 | 151 | | yield break; |
| | | 152 | | |
| | | 153 | | // Get all nodes in the current container |
| | 0 | 154 | | var workflowExecutionContext = context.WorkflowExecutionContext; |
| | 0 | 155 | | var containedNodes = workflowExecutionContext.Nodes.Where(x => x.Parents.Contains(currentContainerNode)).Dis |
| | | 156 | | |
| | | 157 | | // Select activities with outputs. |
| | 0 | 158 | | var activityRegistry = workflowExecutionContext.GetRequiredService<IActivityRegistryLookupService>(); |
| | 0 | 159 | | var activitiesWithOutputs = containedNodes.GetActivitiesWithOutputs(activityRegistry); |
| | | 160 | | |
| | 0 | 161 | | await foreach (var (activity, activityDescriptor) in activitiesWithOutputs) |
| | 0 | 162 | | yield return (activity, activityDescriptor); |
| | 0 | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Returns a set of tuples containing the activity and its descriptor for all activities with outputs. |
| | | 168 | | /// </summary> |
| | | 169 | | public static async IAsyncEnumerable<(IActivity activity, ActivityDescriptor activityDescriptor)> GetActivitiesWithO |
| | | 170 | | { |
| | 0 | 171 | | foreach (var node in nodes) |
| | | 172 | | { |
| | 0 | 173 | | var activity = node.Activity; |
| | 0 | 174 | | var activityDescriptor = await activityRegistryLookup.FindAsync(activity.Type, activity.Version); |
| | 0 | 175 | | if (activityDescriptor != null && activityDescriptor.Outputs.Any()) |
| | 0 | 176 | | yield return (activity, activityDescriptor); |
| | 0 | 177 | | } |
| | 0 | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <param name="activityExecutionContext">The <see cref="ActivityExecutionContext"/> being extended.</param> |
| | | 181 | | extension(ActivityExecutionContext activityExecutionContext) |
| | | 182 | | { |
| | | 183 | | /// <summary> |
| | | 184 | | /// Returns the activity with the specified ID or name. |
| | | 185 | | /// </summary> |
| | | 186 | | public IActivity? FindActivityByIdOrName(string idOrName) |
| | | 187 | | { |
| | | 188 | | // Get current container. |
| | 0 | 189 | | var currentContainerNode = activityExecutionContext.FindParentWithVariableContainer()?.ActivityNode; |
| | | 190 | | |
| | 0 | 191 | | if (currentContainerNode == null) |
| | 0 | 192 | | return null; |
| | | 193 | | |
| | | 194 | | // Get all nodes in the current container |
| | 0 | 195 | | var workflowExecutionContext = activityExecutionContext.WorkflowExecutionContext; |
| | 0 | 196 | | var containedNodes = workflowExecutionContext.Nodes.Where(x => x.Parents.Contains(currentContainerNode)).Dis |
| | 0 | 197 | | var node = containedNodes.FirstOrDefault(x => x.Activity.Name == idOrName || x.Activity.Id == idOrName); |
| | 0 | 198 | | return node?.Activity; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Returns the outcome name for the specified port property name. |
| | | 203 | | /// </summary> |
| | | 204 | | /// <param name="portPropertyName">The name of the port property.</param> |
| | | 205 | | /// <returns>The outcome name.</returns> |
| | | 206 | | public string GetOutcomeName(string portPropertyName) |
| | | 207 | | { |
| | 2 | 208 | | var owner = activityExecutionContext.Activity; |
| | 28 | 209 | | var ports = owner.GetType().GetProperties().Where(x => typeof(IActivity).IsAssignableFrom(x.PropertyType)).T |
| | | 210 | | |
| | 2 | 211 | | var portQuery = |
| | 2 | 212 | | from p in ports |
| | 2 | 213 | | where p.Name == portPropertyName |
| | 2 | 214 | | select p; |
| | | 215 | | |
| | 2 | 216 | | var portProperty = portQuery.First(); |
| | 2 | 217 | | return portProperty.GetCustomAttribute<PortAttribute>()?.Name ?? portProperty.Name; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// Send a signal up the current hierarchy of ancestors. |
| | | 222 | | /// </summary> |
| | | 223 | | public async ValueTask SendSignalAsync(object signal) |
| | | 224 | | { |
| | 3423 | 225 | | await activityExecutionContext.TrySendSignalAsync(signal); |
| | 3423 | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Send a signal up the current hierarchy of ancestors and report whether a receiver claimed it. |
| | | 230 | | /// </summary> |
| | | 231 | | /// <param name="signal">The signal to send.</param> |
| | | 232 | | /// <returns> |
| | | 233 | | /// <c>true</c> if a receiver called <see cref="SignalContext.StopPropagation"/>; otherwise, <c>false</c>. |
| | | 234 | | /// </returns> |
| | | 235 | | internal async ValueTask<bool> TrySendSignalAsync(object signal) |
| | | 236 | | { |
| | 3437 | 237 | | var receivingContexts = new[] |
| | 3437 | 238 | | { |
| | 3437 | 239 | | activityExecutionContext |
| | 3437 | 240 | | }.Concat(activityExecutionContext.GetAncestors()).ToList(); |
| | 3437 | 241 | | var logger = activityExecutionContext.GetRequiredService<ILogger<ActivityExecutionContext>>(); |
| | 3437 | 242 | | var signalType = signal.GetType(); |
| | 3437 | 243 | | var signalTypeName = signalType.Name; |
| | | 244 | | |
| | | 245 | | // Let all ancestors receive the signal. |
| | 32669 | 246 | | foreach (var ancestorContext in receivingContexts) |
| | | 247 | | { |
| | 12899 | 248 | | var signalContext = new SignalContext(ancestorContext, activityExecutionContext, activityExecutionContex |
| | | 249 | | |
| | 12899 | 250 | | if (ancestorContext.Activity is not ISignalHandler handler) |
| | | 251 | | continue; |
| | | 252 | | |
| | 12899 | 253 | | logger.LogDebug("Receiving signal {SignalType} on activity {ActivityId} of type {ActivityType}", signalT |
| | 12899 | 254 | | await handler.ReceiveSignalAsync(signal, signalContext); |
| | | 255 | | |
| | 12899 | 256 | | if (signalContext.StopPropagationRequested) |
| | | 257 | | { |
| | 3 | 258 | | logger.LogDebug("Propagation of signal {SignalType} on activity {ActivityId} of type {ActivityType} |
| | 3 | 259 | | return true; |
| | | 260 | | } |
| | 12896 | 261 | | } |
| | | 262 | | |
| | 3434 | 263 | | return false; |
| | 3437 | 264 | | } |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Complete the current activity. This should only be called by activities that explicitly suppress automatic-c |
| | | 268 | | /// </summary> |
| | | 269 | | public async ValueTask ScheduleOutcomesAsync(params string[] outcomes) |
| | | 270 | | { |
| | | 271 | | // Record the outcomes, if any. |
| | 0 | 272 | | activityExecutionContext.JournalData["Outcomes"] = outcomes; |
| | | 273 | | |
| | | 274 | | // Record the output, if any. |
| | 0 | 275 | | var activity = activityExecutionContext.Activity; |
| | 0 | 276 | | var expressionExecutionContext = activityExecutionContext.ExpressionExecutionContext; |
| | 0 | 277 | | var activityDescriptor = activityExecutionContext.ActivityDescriptor; |
| | 0 | 278 | | var outputDescriptors = activityDescriptor.Outputs; |
| | 0 | 279 | | var outputs = outputDescriptors.ToDictionary(x => x.Name, x => activity.GetOutput(expressionExecutionContext |
| | 0 | 280 | | var serializer = activityExecutionContext.GetRequiredService<ISafeSerializer>(); |
| | | 281 | | |
| | 0 | 282 | | foreach (var output in outputs) |
| | | 283 | | { |
| | 0 | 284 | | var outputName = output.Key; |
| | 0 | 285 | | var outputValue = output.Value; |
| | | 286 | | |
| | 0 | 287 | | if (outputValue == null!) |
| | | 288 | | continue; |
| | | 289 | | |
| | 0 | 290 | | var serializedOutputValue = serializer.Serialize(outputValue); |
| | 0 | 291 | | activityExecutionContext.JournalData[outputName] = serializedOutputValue; |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | // Send a signal. |
| | 0 | 295 | | await activityExecutionContext.SendSignalAsync(new ScheduleActivityOutcomes(outcomes)); |
| | 0 | 296 | | } |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Cancel the activity. For blocking activities, it means their bookmarks will be removed. For job activities, |
| | | 300 | | /// </summary> |
| | | 301 | | /// <remarks> |
| | | 302 | | /// An activity that is still <see cref="ActivityStatus.Pending"/> is cancelled too, rather than skipped: it has |
| | | 303 | | /// been scheduled and would otherwise be invoked after the container that scheduled it decided it must not run. |
| | | 304 | | /// Cancelling it also withdraws the work item that would have started it. |
| | | 305 | | /// </remarks> |
| | | 306 | | public async Task CancelActivityAsync() |
| | | 307 | | { |
| | | 308 | | // If the activity has already reached a terminal state, do nothing. |
| | 161 | 309 | | if (activityExecutionContext.Status is ActivityStatus.Completed or ActivityStatus.Canceled) |
| | 156 | 310 | | return; |
| | | 311 | | |
| | | 312 | | // Withdraw work that has been scheduled but not yet invoked, so that cancelling a branch cannot leave an |
| | | 313 | | // activity from it running afterwards. |
| | 5 | 314 | | activityExecutionContext.WithdrawScheduledWork(); |
| | | 315 | | |
| | | 316 | | // Select all child contexts. |
| | 5 | 317 | | var childContexts = activityExecutionContext.Children.ToList(); |
| | | 318 | | |
| | 10 | 319 | | foreach (var childContext in childContexts) |
| | 0 | 320 | | await childContext.CancelActivityAsync(); |
| | | 321 | | |
| | 5 | 322 | | var publisher = activityExecutionContext.GetRequiredService<INotificationSender>(); |
| | 5 | 323 | | activityExecutionContext.TransitionTo(ActivityStatus.Canceled); |
| | 5 | 324 | | activityExecutionContext.ClearBookmarks(); |
| | 5 | 325 | | activityExecutionContext.ClearCompletionCallbacks(); |
| | 5 | 326 | | activityExecutionContext.WorkflowExecutionContext.Bookmarks.RemoveWhere(x => x.ActivityNodeId == activityExe |
| | | 327 | | |
| | | 328 | | // Add an execution log entry. |
| | 5 | 329 | | activityExecutionContext.AddExecutionLogEntry("Canceled"); |
| | | 330 | | |
| | 5 | 331 | | await activityExecutionContext.SendSignalAsync(new CancelSignal()); |
| | 5 | 332 | | await publisher.SendAsync(new ActivityCancelled(activityExecutionContext)); |
| | 161 | 333 | | } |
| | | 334 | | |
| | | 335 | | /// <summary> |
| | | 336 | | /// Marks the current activity as faulted, records an exception, and transitions its status to <see cref="Activi |
| | | 337 | | /// An incident is logged, and the fault count for the activity and its ancestor activities is incremented. |
| | | 338 | | /// </summary> |
| | | 339 | | /// <param name="e">The exception to be recorded as the cause of the fault.</param> |
| | | 340 | | public void Fault(Exception e) |
| | | 341 | | { |
| | 29 | 342 | | activityExecutionContext.Exception = e; |
| | 29 | 343 | | activityExecutionContext.TransitionTo(ActivityStatus.Faulted); |
| | 29 | 344 | | var activity = activityExecutionContext.Activity; |
| | 29 | 345 | | var exceptionState = ExceptionState.FromException(e); |
| | 29 | 346 | | var systemClock = activityExecutionContext.GetRequiredService<ISystemClock>(); |
| | 29 | 347 | | var now = systemClock.UtcNow; |
| | 29 | 348 | | var incident = new ActivityIncident(activity.Id, activity.NodeId, activity.Type, e.Message, exceptionState, |
| | 29 | 349 | | activityExecutionContext.WorkflowExecutionContext.Incidents.Add(incident); |
| | 29 | 350 | | activityExecutionContext.AggregateFaultCount++; |
| | | 351 | | |
| | 29 | 352 | | var ancestors = activityExecutionContext.GetAncestors(); |
| | | 353 | | |
| | 132 | 354 | | foreach (var ancestor in ancestors) |
| | 37 | 355 | | ancestor.AggregateFaultCount++; |
| | 29 | 356 | | } |
| | | 357 | | |
| | | 358 | | /// <summary> |
| | | 359 | | /// Recovers the current activity from a faulted state: undoes the fault counts, discards the incident and the r |
| | | 360 | | /// exception, and transitions the activity back to the running status. |
| | | 361 | | /// </summary> |
| | | 362 | | /// <remarks> |
| | | 363 | | /// <para> |
| | | 364 | | /// This is the inverse of <c>Fault</c>, and is meant to leave no trace of a fault an enclosing container claime |
| | | 365 | | /// particular it removes the <see cref="ActivityIncident"/> that <c>Fault</c> appended. A handled fault must no |
| | | 366 | | /// incident, because plenty of code treats a non-empty <see cref="WorkflowExecutionContext.Incidents"/> as "thi |
| | | 367 | | /// failed" without looking further - the HTTP endpoint fault handler is one, and it would answer a caller with |
| | | 368 | | /// response for a workflow that caught its error and completed normally. The execution log still records the fa |
| | | 369 | | /// nothing is hidden from anyone reading the journal. |
| | | 370 | | /// </para> |
| | | 371 | | /// <para> |
| | | 372 | | /// The incident is matched on this <i>execution's</i> id rather than on the activity's node id, because a node |
| | | 373 | | /// a loop, retried, or run concurrently has several executions that all raise incidents under the same node id, |
| | | 374 | | /// recovering one of them must not remove another's. Within a single execution the most recent is taken, which |
| | | 375 | | /// an activity that faults, recovers, and faults again correct: each recovery removes its own incident rather t |
| | | 376 | | /// the whole execution's history. That last part relies on <see cref="WorkflowExecutionContext.Incidents"/> pre |
| | | 377 | | /// insertion order, which it does because it is list-backed; it is typed as a plain collection, so a future cha |
| | | 378 | | /// an unordered one would silently pick an arbitrary incident of that execution rather than its newest. |
| | | 379 | | /// </para> |
| | | 380 | | /// <para> |
| | | 381 | | /// Clearing the exception also clears it from the activity's execution record, which maps it from the live cont |
| | | 382 | | /// That is intended and follows from the same reasoning as the incident: an execution whose failure a container |
| | | 383 | | /// claimed is not a failed execution. The journal keeps the evidence either way, since the <c>Faulted</c> entry |
| | | 384 | | /// written by <c>ExecutionLogMiddleware</c>, which sits inside this middleware and logs on the way past. |
| | | 385 | | /// </para> |
| | | 386 | | /// <para> |
| | | 387 | | /// It remains asymmetric with <c>Fault</c> in one respect: it <i>sets</i> the current context's fault count to |
| | | 388 | | /// idempotent, but <i>decrements</i> the count on every ancestor, which is not. Calling it more than once per f |
| | | 389 | | /// ancestor counts negative. The status transition only applies to an activity that is still |
| | | 390 | | /// <see cref="ActivityStatus.Faulted"/>, so that a <see cref="Elsa.Workflows.Signals.FaultSignal"/> handler tha |
| | | 391 | | /// terminalized the activity does not see its decision undone. |
| | | 392 | | /// </para> |
| | | 393 | | /// </remarks> |
| | | 394 | | public void RecoverFromFault() |
| | | 395 | | { |
| | 10 | 396 | | activityExecutionContext.AggregateFaultCount = 0; |
| | | 397 | | |
| | 10 | 398 | | var ancestors = activityExecutionContext.GetAncestors(); |
| | | 399 | | |
| | 40 | 400 | | foreach (var ancestor in ancestors) |
| | 10 | 401 | | ancestor.AggregateFaultCount--; |
| | | 402 | | |
| | 10 | 403 | | var incidents = activityExecutionContext.WorkflowExecutionContext.Incidents; |
| | 19 | 404 | | var ownIncident = incidents.LastOrDefault(x => x.ActivityInstanceId == activityExecutionContext.Id); |
| | | 405 | | |
| | 10 | 406 | | if (ownIncident != null) |
| | 8 | 407 | | incidents.Remove(ownIncident); |
| | | 408 | | |
| | 10 | 409 | | activityExecutionContext.Exception = null; |
| | | 410 | | |
| | 10 | 411 | | if (activityExecutionContext.Status == ActivityStatus.Faulted) |
| | 7 | 412 | | activityExecutionContext.TransitionTo(ActivityStatus.Running); |
| | 10 | 413 | | } |
| | | 414 | | |
| | | 415 | | /// <summary> |
| | | 416 | | /// Returns the first context that is associated with a <see cref="IVariableContainer"/>. |
| | | 417 | | /// </summary> |
| | | 418 | | public ActivityExecutionContext? FindParentWithVariableContainer() |
| | | 419 | | { |
| | 144 | 420 | | return activityExecutionContext.FindParent(x => x.Activity is IVariableContainer); |
| | | 421 | | } |
| | | 422 | | |
| | | 423 | | /// <summary> |
| | | 424 | | /// Returns the first context in the hierarchy that matches the specified predicate. |
| | | 425 | | /// </summary> |
| | | 426 | | /// <param name="predicate">The predicate to match.</param> |
| | | 427 | | /// <returns>The first context that matches the predicate or <c>null</c> if no match was found.</returns> |
| | | 428 | | public ActivityExecutionContext? FindParent(Func<ActivityExecutionContext, bool> predicate) |
| | | 429 | | { |
| | 73 | 430 | | var currentContext = activityExecutionContext; |
| | | 431 | | |
| | 74 | 432 | | while (currentContext != null) |
| | | 433 | | { |
| | 73 | 434 | | if (predicate(currentContext)) |
| | 72 | 435 | | return currentContext; |
| | | 436 | | |
| | 1 | 437 | | currentContext = currentContext.ParentActivityExecutionContext; |
| | | 438 | | } |
| | | 439 | | |
| | 1 | 440 | | return null; |
| | | 441 | | } |
| | | 442 | | |
| | | 443 | | /// <summary> |
| | | 444 | | /// Returns a flattened list of the current context's ancestors. |
| | | 445 | | /// </summary> |
| | | 446 | | public IEnumerable<ActivityExecutionContext> GetAncestors() |
| | | 447 | | { |
| | 10191 | 448 | | var current = activityExecutionContext.ParentActivityExecutionContext; |
| | | 449 | | |
| | 31874 | 450 | | while (current != null) |
| | | 451 | | { |
| | 24590 | 452 | | yield return current; |
| | 21683 | 453 | | current = current.ParentActivityExecutionContext; |
| | | 454 | | } |
| | 7284 | 455 | | } |
| | | 456 | | |
| | | 457 | | /// <summary> |
| | | 458 | | /// Returns a flattened list of the current context's descendants. |
| | | 459 | | /// </summary> |
| | | 460 | | public IEnumerable<ActivityExecutionContext> GetDescendents() |
| | | 461 | | { |
| | 0 | 462 | | var children = activityExecutionContext.Children.ToList(); |
| | | 463 | | |
| | 0 | 464 | | foreach (var child in children) |
| | | 465 | | { |
| | 0 | 466 | | yield return child; |
| | | 467 | | |
| | 0 | 468 | | foreach (var descendent in child.GetDescendents()) |
| | 0 | 469 | | yield return descendent; |
| | 0 | 470 | | } |
| | 0 | 471 | | } |
| | | 472 | | |
| | | 473 | | /// <summary> |
| | | 474 | | /// Returns a flattened list of the current context's immediate active children. |
| | | 475 | | /// </summary> |
| | | 476 | | public IEnumerable<ActivityExecutionContext> GetActiveChildren() |
| | | 477 | | { |
| | 0 | 478 | | return activityExecutionContext.Children; |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | /// <summary> |
| | | 482 | | /// Returns a flattened list of the current context's immediate children. |
| | | 483 | | /// </summary> |
| | | 484 | | public IEnumerable<ActivityExecutionContext> GetChildren() |
| | | 485 | | { |
| | 0 | 486 | | return activityExecutionContext.Children; |
| | | 487 | | } |
| | | 488 | | |
| | | 489 | | /// <summary> |
| | | 490 | | /// Returns a flattened list of the current context's descendants. |
| | | 491 | | /// </summary> |
| | | 492 | | public IEnumerable<ActivityExecutionContext> GetDescendants() |
| | | 493 | | { |
| | 1 | 494 | | var children = activityExecutionContext.Children.ToList(); |
| | | 495 | | |
| | 2 | 496 | | foreach (var child in children) |
| | | 497 | | { |
| | 0 | 498 | | yield return child; |
| | | 499 | | |
| | 0 | 500 | | foreach (var descendant in child.GetDescendants()) |
| | 0 | 501 | | yield return descendant; |
| | 0 | 502 | | } |
| | 1 | 503 | | } |
| | | 504 | | |
| | | 505 | | /// <summary> |
| | | 506 | | /// Sets extension data in the metadata. Represents specific data that is exposed generically for an activity. |
| | | 507 | | /// </summary> |
| | | 508 | | public void SetExtensionsMetadata(string key, object? value) |
| | | 509 | | { |
| | 4 | 510 | | var extensionsDictionary = activityExecutionContext.GetExtensionsMetadata() ?? new Dictionary<string, object |
| | | 511 | | |
| | 4 | 512 | | extensionsDictionary[key] = value; |
| | | 513 | | |
| | 4 | 514 | | activityExecutionContext.Metadata[ExtensionsMetadataKey] = extensionsDictionary; |
| | 4 | 515 | | } |
| | | 516 | | |
| | | 517 | | /// <summary> |
| | | 518 | | /// Retrieves the extension data from the metadata. Represents specific data that is exposed generically for an |
| | | 519 | | /// </summary> |
| | | 520 | | public Dictionary<string, object?>? GetExtensionsMetadata() |
| | | 521 | | { |
| | 7 | 522 | | return activityExecutionContext.Metadata.TryGetValue(ExtensionsMetadataKey, out var value) ? value as Dictio |
| | | 523 | | } |
| | | 524 | | |
| | | 525 | | /// <summary> |
| | | 526 | | /// Gets the output of an activity even when the context is not the same as the activity's context. |
| | | 527 | | /// This is useful when you want to get the output of a previously executed activity. |
| | | 528 | | /// </summary> |
| | | 529 | | /// <param name="property"></param> |
| | | 530 | | /// <typeparam name="TProp"></typeparam> |
| | | 531 | | /// <returns></returns> |
| | | 532 | | public object? GetActivityOutput<TProp>(Expression<Func<TProp>> property) |
| | | 533 | | { |
| | 71 | 534 | | if (property.Body is not MemberExpression memberExpr) |
| | | 535 | | { |
| | 0 | 536 | | return null; |
| | | 537 | | } |
| | | 538 | | |
| | 71 | 539 | | var propertyName = memberExpr.Member.Name; |
| | 71 | 540 | | var value = activityExecutionContext.Get(propertyName); |
| | | 541 | | |
| | 71 | 542 | | if (value != null) |
| | 0 | 543 | | return value; |
| | | 544 | | |
| | 71 | 545 | | var registry = activityExecutionContext.WorkflowExecutionContext.GetActivityOutputRegister(); |
| | | 546 | | |
| | 71 | 547 | | return registry.FindOutputByActivityInstanceId(activityExecutionContext.Id, propertyName); |
| | | 548 | | } |
| | | 549 | | |
| | 8811 | 550 | | public bool GetHasEvaluatedProperties() => activityExecutionContext.TransientProperties.TryGetValue<bool>("HasEv |
| | 3799 | 551 | | public void SetHasEvaluatedProperties() => activityExecutionContext.TransientProperties["HasEvaluatedProperties" |
| | | 552 | | |
| | | 553 | | /// <summary> |
| | | 554 | | /// Retrieves the complete execution chain for this activity execution context by traversing the SchedulingActiv |
| | | 555 | | /// Returns contexts ordered from root (depth 0) to this context. |
| | | 556 | | /// </summary> |
| | | 557 | | /// <returns>A collection of activity execution contexts representing the complete call stack, ordered from root |
| | | 558 | | public IEnumerable<ActivityExecutionContext> GetExecutionChain() |
| | | 559 | | { |
| | 2 | 560 | | var chain = new List<ActivityExecutionContext>(); |
| | 2 | 561 | | var visited = new HashSet<string>(); |
| | 2 | 562 | | var currentContext = activityExecutionContext; |
| | | 563 | | |
| | | 564 | | // Traverse the chain backwards from this context to the root |
| | 6 | 565 | | while (currentContext != null && visited.Add(currentContext.Id)) |
| | | 566 | | { |
| | 5 | 567 | | chain.Add(currentContext); |
| | | 568 | | |
| | | 569 | | // Find the parent in the workflow execution context |
| | 5 | 570 | | if (currentContext.SchedulingActivityExecutionId != null) |
| | | 571 | | { |
| | 4 | 572 | | currentContext = currentContext.WorkflowExecutionContext.ActivityExecutionContexts |
| | 12 | 573 | | .FirstOrDefault(x => x.Id == currentContext.SchedulingActivityExecutionId); |
| | | 574 | | } |
| | | 575 | | else |
| | | 576 | | { |
| | | 577 | | break; |
| | | 578 | | } |
| | | 579 | | } |
| | | 580 | | |
| | | 581 | | // Reverse to get root-to-leaf order |
| | 2 | 582 | | chain.Reverse(); |
| | 2 | 583 | | return chain; |
| | | 584 | | } |
| | | 585 | | } |
| | | 586 | | } |