< 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: 78
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.Authorization;
 2using Elsa.Abstractions;
 3using Elsa.Alterations.AlterationTypes;
 4using Elsa.Alterations.Core.Contracts;
 5using Elsa.Alterations.Core.Results;
 6using Elsa.Workflows.Management;
 7using Elsa.Workflows.Management.Entities;
 8using Elsa.Workflows.Management.Filters;
 9using Elsa.Workflows.Runtime;
 10using Elsa.Workflows.Runtime.Contracts;
 11using Elsa.Workflows.Runtime.Requests;
 12using JetBrains.Annotations;
 13
 14namespace Elsa.Alterations.Endpoints.Workflows.Retry;
 15
 16/// <summary>
 17/// Retries the specified workflow instances.
 18/// </summary>
 19[PublicAPI]
 20public class Retry : ElsaEndpoint<Request, Response>
 21{
 22    private readonly IAlterationRunner _alterationRunner;
 23    private readonly IWorkflowDispatcher _workflowDispatcher;
 24    private readonly IWorkflowInstanceStore _workflowInstanceStore;
 25
 26    /// <inheritdoc />
 327    public Retry(IAlterationRunner alterationRunner, IWorkflowDispatcher workflowDispatcher, IWorkflowInstanceStore work
 28    {
 329        _alterationRunner = alterationRunner;
 330        _workflowDispatcher = workflowDispatcher;
 331        _workflowInstanceStore = workflowInstanceStore;
 332    }
 33
 34    /// <inheritdoc />
 35    public override void Configure()
 36    {
 337        Routes("/alterations/workflows/retry");
 338        Verbs(FastEndpoints.Http.GET, FastEndpoints.Http.POST);
 339        RequirePermission(Elsa.Alterations.Permissions.AlterationPermissions.Alterations, CoreVerbs.Execute);
 340    }
 41
 42    /// <inheritdoc />
 43    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 44    {
 045        var allResults = new List<RunAlterationsResult>();
 46
 47        // Load each workflow instance.
 048        var workflowInstances = (await _workflowInstanceStore.FindManyAsync(new WorkflowInstanceFilter { Ids = request.W
 49
 050        foreach (var workflowInstance in workflowInstances)
 51        {
 52            // Setup an alteration plan.
 053            var activityIds = GetActivityIds(request, workflowInstance).ToList();
 054            var alterations = activityIds.Select(activityId => new ScheduleActivity { ActivityId = activityId }).Cast<IA
 55
 56            // Run the plan.
 057            var results = await _alterationRunner.RunAsync(request.WorkflowInstanceIds, alterations, cancellationToken);
 058            allResults.AddRange(results);
 59
 60            // Schedule updated workflow.
 061            await _workflowDispatcher.DispatchAsync(new DispatchWorkflowInstanceRequest(workflowInstance.Id), cancellati
 062        }
 63
 64        // Write response.
 065        var response = new Response(allResults);
 066        await Send.OkAsync(response, cancellationToken);
 067    }
 68
 69    private IEnumerable<string> GetActivityIds(Request request, WorkflowInstance workflowInstance)
 70    {
 71        // If activity IDs are explicitly specified, use them.
 072        if (request.ActivityIds?.Any() == true)
 073            return request.ActivityIds;
 74
 75        // Otherwise, select IDs of all faulted activities.
 076        return workflowInstance.WorkflowState.Incidents.Select(x => x.ActivityId).Distinct().ToList();
 77    }
 78}