| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Workflows.Runtime.Notifications; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Runtime.Services; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Default implementation of <see cref="IWorkflowRuntimeAdminService"/>. Encapsulates the pause/resume/force-drain |
| | | 9 | | /// state-machine semantics and the audit-on-effective-transition rule (SC-007) in one place so transport-specific |
| | | 10 | | /// adapters (HTTP, MCP, CLI) stay thin. |
| | | 11 | | /// </summary> |
| | 3 | 12 | | public sealed class WorkflowRuntimeAdminService( |
| | 3 | 13 | | IQuiescenceSignal signal, |
| | 3 | 14 | | IIngressSourceRegistry registry, |
| | 3 | 15 | | IDrainOrchestrator orchestrator, |
| | 3 | 16 | | INotificationSender mediator, |
| | 3 | 17 | | ISystemClock clock) : IWorkflowRuntimeAdminService |
| | | 18 | | { |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public async ValueTask<QuiescenceState> PauseAsync(string? reason, string? requestedBy, CancellationToken cancellati |
| | | 21 | | { |
| | 0 | 22 | | var before = signal.CurrentState; |
| | 0 | 23 | | var after = await signal.PauseAsync(reason, requestedBy, cancellationToken); |
| | | 24 | | |
| | | 25 | | // Audit only effective transitions (SC-007). |
| | 0 | 26 | | var wasPaused = (before.Reason & QuiescenceReason.AdministrativePause) != 0; |
| | 0 | 27 | | var isPaused = (after.Reason & QuiescenceReason.AdministrativePause) != 0; |
| | 0 | 28 | | if (!wasPaused && isPaused) |
| | 0 | 29 | | await mediator.SendAsync(new RuntimePauseRequested(requestedBy, reason, after.PausedAt ?? clock.UtcNow), can |
| | | 30 | | |
| | 0 | 31 | | return after; |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public async ValueTask<QuiescenceState> ResumeAsync(string? requestedBy, CancellationToken cancellationToken) |
| | | 36 | | { |
| | 0 | 37 | | var before = signal.CurrentState; |
| | 0 | 38 | | var after = await signal.ResumeAsync(requestedBy, cancellationToken); |
| | | 39 | | |
| | 0 | 40 | | var wasPaused = (before.Reason & QuiescenceReason.AdministrativePause) != 0; |
| | 0 | 41 | | var isPaused = (after.Reason & QuiescenceReason.AdministrativePause) != 0; |
| | 0 | 42 | | if (wasPaused && !isPaused) |
| | 0 | 43 | | await mediator.SendAsync(new RuntimeResumeRequested(requestedBy, clock.UtcNow), cancellationToken); |
| | | 44 | | |
| | 0 | 45 | | return after; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public async ValueTask<DrainOutcome> ForceDrainAsync(string? reason, string? requestedBy, CancellationToken cancella |
| | | 50 | | { |
| | | 51 | | // Caller catches InvalidOperationException to translate to 409 etc. |
| | 0 | 52 | | var outcome = await orchestrator.DrainAsync(DrainTrigger.OperatorForce, cancellationToken); |
| | | 53 | | |
| | | 54 | | // Audit. Skip when the call returned a cached outcome — repeated invocations in the same generation |
| | | 55 | | // would otherwise emit spurious audit events and break SC-007 idempotency. |
| | 0 | 56 | | if (!outcome.WasCached) |
| | 0 | 57 | | await mediator.SendAsync(new RuntimeForceDrainRequested(requestedBy, reason, clock.UtcNow, outcome), cancell |
| | | 58 | | |
| | 0 | 59 | | return outcome; |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | 0 | 63 | | public RuntimeAdminStatus GetStatus() => new(signal.CurrentState, registry.Snapshot(), signal.ActiveExecutionCycleCo |
| | | 64 | | } |