< 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
51%
Covered lines: 28
Uncovered lines: 26
Coverable lines: 54
Total lines: 144
Line coverage: 51.8%
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(IDictionary<string, object>? input = null, IEnumerable<Variable>? varia
 21        {
 40022            var workflow = workflowExecutionContext.Workflow;
 40023            var workItem = new ActivityWorkItem(workflow, input: input, variables: variables);
 40024            workflowExecutionContext.Scheduler.Schedule(workItem);
 40025            return workItem;
 26        }
 27
 28        /// <summary>
 29        /// Schedules the root activity of the workflow.
 30        /// </summary>
 31        public ActivityWorkItem ScheduleRoot(IDictionary<string, object>? input = null, IEnumerable<Variable>? variables
 32        {
 033            var workflow = workflowExecutionContext.Workflow;
 034            var workItem = new ActivityWorkItem(workflow.Root, input: input, variables: variables);
 035            workflowExecutionContext.Scheduler.Schedule(workItem);
 036            return workItem;
 37        }
 38
 39        /// <summary>
 40        /// Schedules the specified activity of the workflow.
 41        /// </summary>
 42        public ActivityWorkItem ScheduleActivity(IActivity activity, IDictionary<string, object>? input = null, IEnumera
 43        {
 044            var workItem = new ActivityWorkItem(activity, input: input, variables: variables);
 045            workflowExecutionContext.Scheduler.Schedule(workItem);
 046            return workItem;
 47        }
 48
 49        /// <summary>
 50        /// Schedules the specified activity execution context of the workflow.
 51        /// </summary>
 52        public ActivityWorkItem ScheduleActivityExecutionContext(ActivityExecutionContext activityExecutionContext, IDic
 53        {
 054            var workItem = new ActivityWorkItem(
 055                activityExecutionContext.Activity,
 056                input: input,
 057                variables: variables,
 058                existingActivityExecutionContext: activityExecutionContext);
 059            workflowExecutionContext.Scheduler.Schedule(workItem);
 060            return workItem;
 61        }
 62
 63        /// <summary>
 64        /// Schedules the activity of the specified bookmark.
 65        /// </summary>
 66        /// <returns>The created work item, or <c>null</c> if the specified bookmark doesn't exist in the <see cref="Wor
 67        public ActivityWorkItem? ScheduleBookmark(Bookmark bookmark, IDictionary<string, object>? input = null, IEnumera
 68        {
 69            // Get the activity execution context that owns the bookmark.
 7870            var bookmarkedActivityContext = workflowExecutionContext.ActivityExecutionContexts.FirstOrDefault(x => x.Id 
 3671            var logger = workflowExecutionContext.GetRequiredService<ILogger<WorkflowExecutionContext>>();
 72
 3673            if (bookmarkedActivityContext == null)
 74            {
 075                logger.LogWarning("Could not find activity execution context with ID {ActivityInstanceId} for bookmark {
 076                return null;
 77            }
 78
 3679            var bookmarkedActivity = bookmarkedActivityContext.Activity;
 80
 81            // Schedule the activity to resume.
 3682            var workItem = new ActivityWorkItem(bookmarkedActivity)
 3683            {
 3684                ExistingActivityExecutionContext = bookmarkedActivityContext,
 3685                Input = input ?? new Dictionary<string, object>(),
 3686                Variables = variables
 3687            };
 3688            workflowExecutionContext.Scheduler.Schedule(workItem);
 89
 90            // If no resumption point was specified, use a "noop" to prevent the regular "ExecuteAsync" method to be inv
 91            // Unless the bookmark is configured to auto-complete, in which case we'll just complete the activity.
 3692            workflowExecutionContext.ExecuteDelegate = bookmark.CallbackMethodName != null
 3693                ? bookmarkedActivity.GetResumeActivityDelegate(bookmark.CallbackMethodName)
 3694                : bookmark.AutoComplete
 3695                    ? WorkflowExecutionContext.Complete
 3696                    : WorkflowExecutionContext.Noop;
 97
 98            // Store the bookmark to resume in the context.
 3699            workflowExecutionContext.ResumedBookmarkContext = new(bookmark);
 36100            logger.LogDebug("Scheduled activity {ActivityId} to resume from bookmark {BookmarkId}", bookmarkedActivity.I
 101
 36102            return workItem;
 103        }
 104
 105        /// <summary>
 106        /// Schedules the specified activity.
 107        /// </summary>
 108        public ActivityWorkItem Schedule(ActivityNode activityNode,
 109            ActivityExecutionContext owner,
 110            ScheduleWorkOptions? options = null)
 111        {
 2707112            var schedulerStrategy = workflowExecutionContext.GetRequiredService<IWorkflowExecutionContextSchedulerStrate
 2707113            return schedulerStrategy.Schedule(workflowExecutionContext, activityNode, owner, options);
 114        }
 115
 116        /// <summary>
 117        /// Returns true if all activities have completed or canceled, false otherwise.
 118        /// </summary>
 3333119        public bool AllActivitiesCompleted() => workflowExecutionContext.ActivityExecutionContexts.All(x => x.IsComplete
 120
 121        public object? GetOutputByActivityId(string activityId, string? outputName = null)
 122        {
 11123            var outputRegister = workflowExecutionContext.GetActivityOutputRegister();
 11124            return outputRegister.FindOutputByActivityId(activityId, outputName);
 125        }
 126
 127        public IEnumerable<ActivityExecutionContext> FindActivityExecutionContexts(ActivityHandle activityHandle)
 128        {
 0129            if (activityHandle.ActivityInstanceId != null)
 0130                return workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Id == activityHandle.ActivityInst
 0131            if (activityHandle.ActivityNodeId != null)
 0132                return workflowExecutionContext.ActivityExecutionContexts.Where(x => x.NodeId == activityHandle.Activity
 0133            if (activityHandle.ActivityId != null)
 0134                return workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Activity.Id == activityHandle.Act
 0135            if (activityHandle.ActivityHash != null)
 136            {
 0137                var activity = workflowExecutionContext.FindActivityByHash(activityHandle.ActivityHash);
 0138                return activity != null ? workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Activity.NodeI
 139            }
 140
 0141            return [];
 142        }
 143    }
 144}

Methods/Properties

ScheduleWorkflow(Elsa.Workflows.WorkflowExecutionContext,System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.Collections.Generic.IEnumerable`1<Elsa.Workflows.Memory.Variable>)
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)