< 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: 62
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.Abstractions;
 2using Elsa.Workflows.Runtime;
 3using FastEndpoints;
 4using JetBrains.Annotations;
 5using Microsoft.AspNetCore.Http;
 6
 7namespace Elsa.Workflows.Api.Endpoints.RuntimeAdmin.ForceDrain;
 8
 9/// <summary>
 10/// <c>POST /admin/workflow-runtime/force-drain</c> — operator-escalation drain with zero deadline. Cancels every
 11/// active execution cycle, persists their instances as <see cref="WorkflowSubStatus.Interrupted"/>, and writes a
 12/// <c>WorkflowInterrupted</c> log entry per affected instance. The host process is NOT exited; the runtime is left
 13/// in <see cref="QuiescenceReason.Drain"/> until the next runtime generation.
 14/// </summary>
 15[PublicAPI]
 316internal sealed class ForceDrainEndpoint(IWorkflowRuntimeAdminService admin) : ElsaEndpoint<ForceDrainRequest, ForceDrai
 17{
 18    public override void Configure()
 19    {
 320        Post("/admin/workflow-runtime/force-drain");
 321        ConfigurePermissions(PermissionNames.ManageWorkflowRuntime);
 322    }
 23
 24    public override async Task HandleAsync(ForceDrainRequest req, CancellationToken ct)
 25    {
 26        DrainOutcome outcome;
 27        try
 28        {
 029            outcome = await admin.ForceDrainAsync(req.Reason, HttpContext.User.Identity?.Name, ct);
 030        }
 31        catch (InvalidOperationException)
 32        {
 33            // Non-force drain already in progress — orchestrator rejects parallel runs. Write the discriminated
 34            // ConflictResponse directly: Send.ResponseAsync is constrained to the endpoint's TResponse and would
 35            // force a null-Outcome ForceDrainResponse otherwise.
 036            HttpContext.Response.StatusCode = StatusCodes.Status409Conflict;
 037            await HttpContext.Response.WriteAsJsonAsync(
 038                new ConflictResponse { Code = "drain-in-progress", State = StatusResponseFactory.Build(admin.GetStatus()
 039                ct);
 040            return;
 41        }
 42
 043        await Send.OkAsync(new ForceDrainResponse { Outcome = MapOutcome(outcome) }, ct);
 044    }
 45
 046    private static DrainOutcomeDto MapOutcome(DrainOutcome o) => new()
 047    {
 048        OverallResult = o.OverallResult.ToString(),
 049        StartedAt = o.StartedAt,
 050        CompletedAt = o.CompletedAt,
 051        PausePhaseDuration = o.PausePhaseDuration,
 052        WaitPhaseDuration = o.WaitPhaseDuration,
 053        Sources = o.Sources.Select(s => new IngressSourceStateDto
 054        {
 055            Name = s.Name,
 056            State = s.State.ToString(),
 057            LastError = s.LastError?.Message,
 058        }).ToList(),
 059        ExecutionCyclesForceCancelledCount = o.ExecutionCyclesForceCancelledCount,
 060        ForceCancelledInstanceIds = o.ForceCancelledInstanceIds.ToList(),
 061    };
 62}