| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Workflows.Runtime; |
| | | 3 | | using FastEndpoints; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using Microsoft.AspNetCore.Http; |
| | | 6 | | |
| | | 7 | | namespace 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] |
| | 3 | 16 | | internal sealed class ForceDrainEndpoint(IWorkflowRuntimeAdminService admin) : ElsaEndpoint<ForceDrainRequest, ForceDrai |
| | | 17 | | { |
| | | 18 | | public override void Configure() |
| | | 19 | | { |
| | 3 | 20 | | Post("/admin/workflow-runtime/force-drain"); |
| | 3 | 21 | | ConfigurePermissions(PermissionNames.ManageWorkflowRuntime); |
| | 3 | 22 | | } |
| | | 23 | | |
| | | 24 | | public override async Task HandleAsync(ForceDrainRequest req, CancellationToken ct) |
| | | 25 | | { |
| | | 26 | | DrainOutcome outcome; |
| | | 27 | | try |
| | | 28 | | { |
| | 0 | 29 | | outcome = await admin.ForceDrainAsync(req.Reason, HttpContext.User.Identity?.Name, ct); |
| | 0 | 30 | | } |
| | | 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. |
| | 0 | 36 | | HttpContext.Response.StatusCode = StatusCodes.Status409Conflict; |
| | 0 | 37 | | await HttpContext.Response.WriteAsJsonAsync( |
| | 0 | 38 | | new ConflictResponse { Code = "drain-in-progress", State = StatusResponseFactory.Build(admin.GetStatus() |
| | 0 | 39 | | ct); |
| | 0 | 40 | | return; |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | await Send.OkAsync(new ForceDrainResponse { Outcome = MapOutcome(outcome) }, ct); |
| | 0 | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | private static DrainOutcomeDto MapOutcome(DrainOutcome o) => new() |
| | 0 | 47 | | { |
| | 0 | 48 | | OverallResult = o.OverallResult.ToString(), |
| | 0 | 49 | | StartedAt = o.StartedAt, |
| | 0 | 50 | | CompletedAt = o.CompletedAt, |
| | 0 | 51 | | PausePhaseDuration = o.PausePhaseDuration, |
| | 0 | 52 | | WaitPhaseDuration = o.WaitPhaseDuration, |
| | 0 | 53 | | Sources = o.Sources.Select(s => new IngressSourceStateDto |
| | 0 | 54 | | { |
| | 0 | 55 | | Name = s.Name, |
| | 0 | 56 | | State = s.State.ToString(), |
| | 0 | 57 | | LastError = s.LastError?.Message, |
| | 0 | 58 | | }).ToList(), |
| | 0 | 59 | | ExecutionCyclesForceCancelledCount = o.ExecutionCyclesForceCancelledCount, |
| | 0 | 60 | | ForceCancelledInstanceIds = o.ForceCancelledInstanceIds.ToList(), |
| | 0 | 61 | | }; |
| | | 62 | | } |