< Summary

Information
Class: Elsa.Alterations.Endpoints.Workflows.Retry.Retry
Assembly: Elsa.Alterations
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/Endpoints/Workflows/Retry/Endpoint.cs
Line coverage
37%
Covered lines: 9
Uncovered lines: 15
Coverable lines: 24
Total lines: 77
Line coverage: 37.5%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Configure()100%11100%
HandleAsync()0%620%
GetActivityIds(...)0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/Endpoints/Workflows/Retry/Endpoint.cs

#LineLine coverage
 1using Elsa.Abstractions;
 2using Elsa.Alterations.AlterationTypes;
 3using Elsa.Alterations.Core.Contracts;
 4using Elsa.Alterations.Core.Results;
 5using Elsa.Workflows.Management;
 6using Elsa.Workflows.Management.Entities;
 7using Elsa.Workflows.Management.Filters;
 8using Elsa.Workflows.Runtime;
 9using Elsa.Workflows.Runtime.Contracts;
 10using Elsa.Workflows.Runtime.Requests;
 11using JetBrains.Annotations;
 12
 13namespace Elsa.Alterations.Endpoints.Workflows.Retry;
 14
 15/// <summary>
 16/// Retries the specified workflow instances.
 17/// </summary>
 18[PublicAPI]
 19public class Retry : ElsaEndpoint<Request, Response>
 20{
 21    private readonly IAlterationRunner _alterationRunner;
 22    private readonly IWorkflowDispatcher _workflowDispatcher;
 23    private readonly IWorkflowInstanceStore _workflowInstanceStore;
 24
 25    /// <inheritdoc />
 126    public Retry(IAlterationRunner alterationRunner, IWorkflowDispatcher workflowDispatcher, IWorkflowInstanceStore work
 27    {
 128        _alterationRunner = alterationRunner;
 129        _workflowDispatcher = workflowDispatcher;
 130        _workflowInstanceStore = workflowInstanceStore;
 131    }
 32
 33    /// <inheritdoc />
 34    public override void Configure()
 35    {
 136        Routes("/alterations/workflows/retry");
 137        Verbs(FastEndpoints.Http.GET, FastEndpoints.Http.POST);
 138        ConfigurePermissions("run:alterations");
 139    }
 40
 41    /// <inheritdoc />
 42    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 43    {
 044        var allResults = new List<RunAlterationsResult>();
 45
 46        // Load each workflow instance.
 047        var workflowInstances = (await _workflowInstanceStore.FindManyAsync(new WorkflowInstanceFilter { Ids = request.W
 48
 049        foreach (var workflowInstance in workflowInstances)
 50        {
 51            // Setup an alteration plan.
 052            var activityIds = GetActivityIds(request, workflowInstance).ToList();
 053            var alterations = activityIds.Select(activityId => new ScheduleActivity { ActivityId = activityId }).Cast<IA
 54
 55            // Run the plan.
 056            var results = await _alterationRunner.RunAsync(request.WorkflowInstanceIds, alterations, cancellationToken);
 057            allResults.AddRange(results);
 58
 59            // Schedule updated workflow.
 060            await _workflowDispatcher.DispatchAsync(new DispatchWorkflowInstanceRequest(workflowInstance.Id), cancellati
 061        }
 62
 63        // Write response.
 064        var response = new Response(allResults);
 065        await Send.OkAsync(response, cancellationToken);
 066    }
 67
 68    private IEnumerable<string> GetActivityIds(Request request, WorkflowInstance workflowInstance)
 69    {
 70        // If activity IDs are explicitly specified, use them.
 071        if (request.ActivityIds?.Any() == true)
 072            return request.ActivityIds;
 73
 74        // Otherwise, select IDs of all faulted activities.
 075        return workflowInstance.WorkflowState.Incidents.Select(x => x.ActivityId).Distinct().ToList();
 76    }
 77}