< Summary

Information
Class: Elsa.Extensions.WorkflowExecutionContextExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/WorkflowExecutionContextExtensions.cs
Line coverage
56%
Covered lines: 34
Uncovered lines: 26
Coverable lines: 60
Total lines: 155
Line coverage: 56.6%
Branch coverage
33%
Covered branches: 6
Total branches: 18
Branch coverage: 33.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ScheduleWorkflow(...)100%11100%
ScheduleRoot(...)100%210%
ScheduleActivity(...)100%210%
ScheduleActivityExecutionContext(...)100%210%
ScheduleBookmark(...)75%8890.47%
Schedule(...)100%11100%
AllActivitiesCompleted(...)100%11100%
GetOutputByActivityId(...)100%11100%
FindActivityExecutionContexts(...)0%110100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/WorkflowExecutionContextExtensions.cs

#LineLine coverage
 1using Elsa.Workflows;
 2using Elsa.Workflows.Memory;
 3using Elsa.Workflows.Models;
 4using Elsa.Workflows.Options;
 5using Microsoft.Extensions.Logging;
 6
 7// ReSharper disable once CheckNamespace
 8namespace Elsa.Extensions;
 9
 10/// <summary>
 11/// Adds extension methods to <see cref="ActivityExecutionContext"/>.
 12/// </summary>
 13public static class WorkflowExecutionContextExtensions
 14{
 15    extension(WorkflowExecutionContext workflowExecutionContext)
 16    {
 17        /// <summary>
 18        /// Schedules the workflow for execution.
 19        /// </summary>
 20        public ActivityWorkItem ScheduleWorkflow(
 21            IDictionary<string, object>? input = null,
 22            IEnumerable<Variable>? variables = null,
 23            string? schedulingActivityExecutionId = null,
 24            string? schedulingWorkflowInstanceId = null,
 25            int? schedulingCallStackDepth = null)
 26        {
 47127            var workflow = workflowExecutionContext.Workflow;
 47128            var workItem = new ActivityWorkItem(
 47129                workflow,
 47130                input: input,
 47131                variables: variables,
 47132                schedulingActivityExecutionId: schedulingActivityExecutionId,
 47133                schedulingWorkflowInstanceId: schedulingWorkflowInstanceId,
 47134                schedulingCallStackDepth: schedulingCallStackDepth);
 47135            workflowExecutionContext.Scheduler.Schedule(workItem);
 47136            return workItem;
 37        }
 38
 39        /// <summary>
 40        /// Schedules the root activity of the workflow.
 41        /// </summary>
 42        public ActivityWorkItem ScheduleRoot(IDictionary<string, object>? input = null, IEnumerable<Variable>? variables
 43        {
 044            var workflow = workflowExecutionContext.Workflow;
 045            var workItem = new ActivityWorkItem(workflow.Root, input: input, variables: variables);
 046            workflowExecutionContext.Scheduler.Schedule(workItem);
 047            return workItem;
 48        }
 49
 50        /// <summary>
 51        /// Schedules the specified activity of the workflow.
 52        /// </summary>
 53        public ActivityWorkItem ScheduleActivity(IActivity activity, IDictionary<string, object>? input = null, IEnumera
 54        {
 055            var workItem = new ActivityWorkItem(activity, input: input, variables: variables);
 056            workflowExecutionContext.Scheduler.Schedule(workItem);
 057            return workItem;
 58        }
 59
 60        /// <summary>
 61        /// Schedules the specified activity execution context of the workflow.
 62        /// </summary>
 63        public ActivityWorkItem ScheduleActivityExecutionContext(ActivityExecutionContext activityExecutionContext, IDic
 64        {
 065            var workItem = new ActivityWorkItem(
 066                activityExecutionContext.Activity,
 067                input: input,
 068                variables: variables,
 069                existingActivityExecutionContext: activityExecutionContext);
 070            workflowExecutionContext.Scheduler.Schedule(workItem);
 071            return workItem;
 72        }
 73
 74        /// <summary>
 75        /// Schedules the activity of the specified bookmark.
 76        /// </summary>
 77        /// <returns>The created work item, or <c>null</c> if the specified bookmark doesn't exist in the <see cref="Wor
 78        public ActivityWorkItem? ScheduleBookmark(Bookmark bookmark, IDictionary<string, object>? input = null, IEnumera
 79        {
 80            // Get the activity execution context that owns the bookmark.
 8881            var bookmarkedActivityContext = workflowExecutionContext.ActivityExecutionContexts.FirstOrDefault(x => x.Id 
 3982            var logger = workflowExecutionContext.GetRequiredService<ILogger<WorkflowExecutionContext>>();
 83
 3984            if (bookmarkedActivityContext == null)
 85            {
 086                logger.LogWarning("Could not find activity execution context with ID {ActivityInstanceId} for bookmark {
 087                return null;
 88            }
 89
 3990            var bookmarkedActivity = bookmarkedActivityContext.Activity;
 91
 92            // Schedule the activity to resume.
 3993            var workItem = new ActivityWorkItem(bookmarkedActivity)
 3994            {
 3995                ExistingActivityExecutionContext = bookmarkedActivityContext,
 3996                Input = input ?? new Dictionary<string, object>(),
 3997                Variables = variables
 3998            };
 3999            workflowExecutionContext.Scheduler.Schedule(workItem);
 100
 101            // If no resumption point was specified, use a "noop" to prevent the regular "ExecuteAsync" method to be inv
 102            // Unless the bookmark is configured to auto-complete, in which case we'll just complete the activity.
 39103            workflowExecutionContext.ExecuteDelegate = bookmark.CallbackMethodName != null
 39104                ? bookmarkedActivity.GetResumeActivityDelegate(bookmark.CallbackMethodName)
 39105                : bookmark.AutoComplete
 39106                    ? WorkflowExecutionContext.Complete
 39107                    : WorkflowExecutionContext.Noop;
 108
 109            // Store the bookmark to resume in the context.
 39110            workflowExecutionContext.ResumedBookmarkContext = new(bookmark);
 39111            logger.LogDebug("Scheduled activity {ActivityId} to resume from bookmark {BookmarkId}", bookmarkedActivity.I
 112
 39113            return workItem;
 114        }
 115
 116        /// <summary>
 117        /// Schedules the specified activity.
 118        /// </summary>
 119        public ActivityWorkItem Schedule(ActivityNode activityNode,
 120            ActivityExecutionContext owner,
 121            ScheduleWorkOptions? options = null)
 122        {
 2940123            var schedulerStrategy = workflowExecutionContext.GetRequiredService<IWorkflowExecutionContextSchedulerStrate
 2940124            return schedulerStrategy.Schedule(workflowExecutionContext, activityNode, owner, options);
 125        }
 126
 127        /// <summary>
 128        /// Returns true if all activities have completed or canceled, false otherwise.
 129        /// </summary>
 3667130        public bool AllActivitiesCompleted() => workflowExecutionContext.ActivityExecutionContexts.All(x => x.IsComplete
 131
 132        public object? GetOutputByActivityId(string activityId, string? outputName = null)
 133        {
 11134            var outputRegister = workflowExecutionContext.GetActivityOutputRegister();
 11135            return outputRegister.FindOutputByActivityId(activityId, outputName);
 136        }
 137
 138        public IEnumerable<ActivityExecutionContext> FindActivityExecutionContexts(ActivityHandle activityHandle)
 139        {
 0140            if (activityHandle.ActivityInstanceId != null)
 0141                return workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Id == activityHandle.ActivityInst
 0142            if (activityHandle.ActivityNodeId != null)
 0143                return workflowExecutionContext.ActivityExecutionContexts.Where(x => x.NodeId == activityHandle.Activity
 0144            if (activityHandle.ActivityId != null)
 0145                return workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Activity.Id == activityHandle.Act
 0146            if (activityHandle.ActivityHash != null)
 147            {
 0148                var activity = workflowExecutionContext.FindActivityByHash(activityHandle.ActivityHash);
 0149                return activity != null ? workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Activity.NodeI
 150            }
 151
 0152            return [];
 153        }
 154    }
 155}

Methods/Properties

ScheduleWorkflow(Elsa.Workflows.WorkflowExecutionContext,System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.Collections.Generic.IEnumerable`1<Elsa.Workflows.Memory.Variable>,System.String,System.String,System.Nullable`1<System.Int32>)
ScheduleRoot(Elsa.Workflows.WorkflowExecutionContext,System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.Collections.Generic.IEnumerable`1<Elsa.Workflows.Memory.Variable>)
ScheduleActivity(Elsa.Workflows.WorkflowExecutionContext,Elsa.Workflows.IActivity,System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.Collections.Generic.IEnumerable`1<Elsa.Workflows.Memory.Variable>)
ScheduleActivityExecutionContext(Elsa.Workflows.WorkflowExecutionContext,Elsa.Workflows.ActivityExecutionContext,System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.Collections.Generic.IEnumerable`1<Elsa.Workflows.Memory.Variable>)
ScheduleBookmark(Elsa.Workflows.WorkflowExecutionContext,Elsa.Workflows.Models.Bookmark,System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.Collections.Generic.IEnumerable`1<Elsa.Workflows.Memory.Variable>)
Schedule(Elsa.Workflows.WorkflowExecutionContext,Elsa.Workflows.Models.ActivityNode,Elsa.Workflows.ActivityExecutionContext,Elsa.Workflows.Options.ScheduleWorkOptions)
AllActivitiesCompleted(Elsa.Workflows.WorkflowExecutionContext)
GetOutputByActivityId(Elsa.Workflows.WorkflowExecutionContext,System.String,System.String)
FindActivityExecutionContexts(Elsa.Workflows.WorkflowExecutionContext,Elsa.Workflows.Models.ActivityHandle)