< Summary

Information
Class: Elsa.Alterations.AlterationHandlers.ScheduleActivityHandler
Assembly: Elsa.Alterations
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/AlterationHandlers/ScheduleActivityHandler.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 85
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
HandleAsync()0%210140%
GetActivityExecutionContext(...)0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/AlterationHandlers/ScheduleActivityHandler.cs

#LineLine coverage
 1using Elsa.Alterations.AlterationTypes;
 2using Elsa.Alterations.Core.Abstractions;
 3using Elsa.Alterations.Core.Contexts;
 4using Elsa.Extensions;
 5using Elsa.Workflows;
 6using Elsa.Workflows.Signals;
 7using JetBrains.Annotations;
 8
 9namespace Elsa.Alterations.AlterationHandlers;
 10
 11/// <summary>
 12/// Schedules an activity for execution.
 13/// </summary>
 14[UsedImplicitly]
 15public class ScheduleActivityHandler : AlterationHandlerBase<ScheduleActivity>
 16{
 17    /// <inheritdoc />
 18    protected override async ValueTask HandleAsync(AlterationContext context, ScheduleActivity alteration)
 19    {
 020        if (alteration.ActivityInstanceId == null && alteration.ActivityId == null)
 21        {
 022            context.Fail("Either ActivityInstanceId or ActivityId must be specified");
 023            return;
 24        }
 25
 026        var workflowExecutionContext = context.WorkflowExecutionContext;
 027        var existingActivityExecutionContext = GetActivityExecutionContext(context, alteration);
 28
 029        if (existingActivityExecutionContext != null)
 30        {
 31            // If the activity is in a faulted state, reset it to Running.
 032            if (existingActivityExecutionContext.Status == ActivityStatus.Faulted)
 033                existingActivityExecutionContext.TransitionTo(ActivityStatus.Running);
 34
 35            // Schedule the activity execution context.
 036            var parentContext = existingActivityExecutionContext.ParentActivityExecutionContext;
 037            await parentContext!.SendSignalAsync(new ScheduleChildActivity(existingActivityExecutionContext));
 038            context.Succeed();
 039            return;
 40        }
 41
 42        // Schedule a new activity instance.
 043        var activityId = alteration.ActivityId;
 44
 045        if (activityId == null)
 46        {
 047            context.Fail("No existing activity execution context was found and no activity ID was specified");
 048            return;
 49        }
 50
 051        var activityNode = workflowExecutionContext.FindNodeByActivityId(activityId);
 52
 053        if (activityNode == null)
 54        {
 055            context.Fail($"Activity with ID {activityId} not found");
 056            return;
 57        }
 58
 59        // Find the parent activity execution context within which to schedule the activity.
 060        var parentActivityContexts = workflowExecutionContext.ActivityExecutionContexts.Reverse().ToList();
 61
 062        var parentExecutionContext =
 063            (from ancestorNode in activityNode.Ancestors()
 064                from parentActivityContext in parentActivityContexts
 065                where parentActivityContext.Activity.Id == ancestorNode.Activity.Id
 066                select parentActivityContext).FirstOrDefault();
 67
 068        if (parentExecutionContext == null)
 69        {
 070            context.Fail($"Could not find parent activity execution context for activity with ID {activityId}");
 071            return;
 72        }
 73
 074        await parentExecutionContext.SendSignalAsync(new ScheduleChildActivity(activityNode.Activity));
 075        context.Succeed();
 076    }
 77
 78    private static ActivityExecutionContext? GetActivityExecutionContext(AlterationContext context, ScheduleActivity alt
 79    {
 080        if (alteration.ActivityInstanceId != null)
 081            return context.WorkflowExecutionContext.ActivityExecutionContexts.FirstOrDefault(x => x.Id == alteration.Act
 82
 083        return null;
 84    }
 85}