< Summary

Information
Class: Elsa.Workflows.Api.Endpoints.RuntimeAdmin.ForceDrain.ForceDrainEndpoint
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/RuntimeAdmin/ForceDrain/Endpoint.cs
Line coverage
13%
Covered lines: 4
Uncovered lines: 25
Coverable lines: 29
Total lines: 64
Line coverage: 13.7%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%
MapOutcome(...)0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/RuntimeAdmin/ForceDrain/Endpoint.cs

#LineLine coverage
 1using Elsa.Authorization;
 2using Elsa.Abstractions;
 3using Elsa.Workflows.Runtime;
 4using FastEndpoints;
 5using JetBrains.Annotations;
 6using Microsoft.AspNetCore.Http;
 7
 8namespace Elsa.Workflows.Api.Endpoints.RuntimeAdmin.ForceDrain;
 9
 10/// <summary>
 11/// <c>POST /admin/workflow-runtime/force-drain</c> — operator-escalation drain with zero deadline. Cancels every
 12/// active execution cycle, persists still-Running instances as <see cref="WorkflowSubStatus.Interrupted"/>
 13/// (Finished/Cancelled rows stay as they are), and writes a
 14/// <c>WorkflowInterrupted</c> log entry per affected instance. The host process is NOT exited; the runtime is left
 15/// in <see cref="QuiescenceReason.Drain"/> until the next runtime generation.
 16/// </summary>
 17[PublicAPI]
 418internal sealed class ForceDrainEndpoint(IWorkflowRuntimeAdminService admin) : ElsaEndpoint<ForceDrainRequest, ForceDrai
 19{
 20    public override void Configure()
 21    {
 422        Post("/admin/workflow-runtime/force-drain");
 423        RequirePermission(Elsa.Workflows.Api.Permissions.WorkflowPermissions.Runtime, "control");
 424    }
 25
 26    public override async Task HandleAsync(ForceDrainRequest req, CancellationToken ct)
 27    {
 28        DrainOutcome outcome;
 29        try
 30        {
 031            outcome = await admin.ForceDrainAsync(req.Reason, HttpContext.User.Identity?.Name, ct);
 032        }
 33        catch (InvalidOperationException)
 34        {
 35            // Non-force drain already in progress — orchestrator rejects parallel runs. Write the discriminated
 36            // ConflictResponse directly: Send.ResponseAsync is constrained to the endpoint's TResponse and would
 37            // force a null-Outcome ForceDrainResponse otherwise.
 038            HttpContext.Response.StatusCode = StatusCodes.Status409Conflict;
 039            await HttpContext.Response.WriteAsJsonAsync(
 040                new ConflictResponse { Code = "drain-in-progress", State = StatusResponseFactory.Build(admin.GetStatus()
 041                ct);
 042            return;
 43        }
 44
 045        await Send.OkAsync(new ForceDrainResponse { Outcome = MapOutcome(outcome) }, ct);
 046    }
 47
 048    private static DrainOutcomeDto MapOutcome(DrainOutcome o) => new()
 049    {
 050        OverallResult = o.OverallResult.ToString(),
 051        StartedAt = o.StartedAt,
 052        CompletedAt = o.CompletedAt,
 053        PausePhaseDuration = o.PausePhaseDuration,
 054        WaitPhaseDuration = o.WaitPhaseDuration,
 055        Sources = o.Sources.Select(s => new IngressSourceStateDto
 056        {
 057            Name = s.Name,
 058            State = s.State.ToString(),
 059            LastError = s.LastError?.Message,
 060        }).ToList(),
 061        ExecutionCyclesForceCancelledCount = o.ExecutionCyclesForceCancelledCount,
 062        ForceCancelledInstanceIds = o.ForceCancelledInstanceIds.ToList(),
 063    };
 64}