< 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: 63
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 their instances as <see cref="WorkflowSubStatus.Interrupted"/>, and writes a
 13/// <c>WorkflowInterrupted</c> log entry per affected instance. The host process is NOT exited; the runtime is left
 14/// in <see cref="QuiescenceReason.Drain"/> until the next runtime generation.
 15/// </summary>
 16[PublicAPI]
 417internal sealed class ForceDrainEndpoint(IWorkflowRuntimeAdminService admin) : ElsaEndpoint<ForceDrainRequest, ForceDrai
 18{
 19    public override void Configure()
 20    {
 421        Post("/admin/workflow-runtime/force-drain");
 422        RequirePermission(Elsa.Workflows.Api.Permissions.WorkflowPermissions.Runtime, "control");
 423    }
 24
 25    public override async Task HandleAsync(ForceDrainRequest req, CancellationToken ct)
 26    {
 27        DrainOutcome outcome;
 28        try
 29        {
 030            outcome = await admin.ForceDrainAsync(req.Reason, HttpContext.User.Identity?.Name, ct);
 031        }
 32        catch (InvalidOperationException)
 33        {
 34            // Non-force drain already in progress — orchestrator rejects parallel runs. Write the discriminated
 35            // ConflictResponse directly: Send.ResponseAsync is constrained to the endpoint's TResponse and would
 36            // force a null-Outcome ForceDrainResponse otherwise.
 037            HttpContext.Response.StatusCode = StatusCodes.Status409Conflict;
 038            await HttpContext.Response.WriteAsJsonAsync(
 039                new ConflictResponse { Code = "drain-in-progress", State = StatusResponseFactory.Build(admin.GetStatus()
 040                ct);
 041            return;
 42        }
 43
 044        await Send.OkAsync(new ForceDrainResponse { Outcome = MapOutcome(outcome) }, ct);
 045    }
 46
 047    private static DrainOutcomeDto MapOutcome(DrainOutcome o) => new()
 048    {
 049        OverallResult = o.OverallResult.ToString(),
 050        StartedAt = o.StartedAt,
 051        CompletedAt = o.CompletedAt,
 052        PausePhaseDuration = o.PausePhaseDuration,
 053        WaitPhaseDuration = o.WaitPhaseDuration,
 054        Sources = o.Sources.Select(s => new IngressSourceStateDto
 055        {
 056            Name = s.Name,
 057            State = s.State.ToString(),
 058            LastError = s.LastError?.Message,
 059        }).ToList(),
 060        ExecutionCyclesForceCancelledCount = o.ExecutionCyclesForceCancelledCount,
 061        ForceCancelledInstanceIds = o.ForceCancelledInstanceIds.ToList(),
 062    };
 63}