|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Text.Json; |
| 3 | +using System.Text.Json.Nodes; |
| 4 | +using Microsoft.Extensions.Hosting; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using PythonScriptRunner.EntityFrameworkCore; |
| 7 | + |
| 8 | +namespace PythonScriptRunner.RabbitMQ; |
| 9 | + |
| 10 | +/// <summary>Runs one <see cref="QueueConsumer"/> for the lifetime of the host.</summary> |
| 11 | +internal abstract class ConsumerService(PsrBroker broker, ILogger logger) : IHostedService, IDisposable |
| 12 | +{ |
| 13 | + private readonly CancellationTokenSource _stopping = new(); |
| 14 | + private QueueConsumer? _consumer; |
| 15 | + private Task? _running; |
| 16 | + |
| 17 | + protected PsrBroker Broker { get; } = broker; |
| 18 | + |
| 19 | + protected ILogger Logger { get; } = logger; |
| 20 | + |
| 21 | + /// <summary>How long deliveries in progress may finish when the host stops.</summary> |
| 22 | + protected virtual TimeSpan GracePeriod => TimeSpan.FromSeconds(30); |
| 23 | + |
| 24 | + public Task StartAsync(CancellationToken cancellationToken) |
| 25 | + { |
| 26 | + _consumer = new QueueConsumer(Broker, Settings(), HandleAsync, Logger); |
| 27 | + _running = Task.Run(() => _consumer.RunAsync(_stopping.Token), CancellationToken.None); |
| 28 | + return Task.CompletedTask; |
| 29 | + } |
| 30 | + |
| 31 | + public async Task StopAsync(CancellationToken cancellationToken) |
| 32 | + { |
| 33 | + // A host can be stopped more than once (e.g. stop, then dispose). |
| 34 | + if (Interlocked.Exchange(ref _consumer, null) is not { } consumer || _running is not { } running) |
| 35 | + { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + await consumer.StopAsync(GracePeriod, cancellationToken).ConfigureAwait(false); |
| 40 | + await _stopping.CancelAsync().ConfigureAwait(false); |
| 41 | + try |
| 42 | + { |
| 43 | + await running.WaitAsync(cancellationToken).ConfigureAwait(false); |
| 44 | + } |
| 45 | + catch (OperationCanceledException) |
| 46 | + { |
| 47 | + // Host shutdown timeout. |
| 48 | + } |
| 49 | + |
| 50 | + await consumer.DisposeAsync().ConfigureAwait(false); |
| 51 | + } |
| 52 | + |
| 53 | + public void Dispose() => _stopping.Dispose(); |
| 54 | + |
| 55 | + protected abstract QueueConsumerSettings Settings(); |
| 56 | + |
| 57 | + protected abstract Task<DeliveryOutcome> HandleAsync(PsrDelivery delivery, CancellationToken cancellationToken); |
| 58 | + |
| 59 | + /// <summary>Reads the JSON body; <c>null</c> (logged) when it is not valid.</summary> |
| 60 | + protected T? Read<T>(PsrDelivery delivery, string expectedType) |
| 61 | + where T : class |
| 62 | + { |
| 63 | + if (delivery.Type is { Length: > 0 } type && type != expectedType) |
| 64 | + { |
| 65 | + Log.InvalidMessage(Logger, delivery.MessageId, delivery.Queue, $"unknown message type '{type}'"); |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + try |
| 70 | + { |
| 71 | + return JsonSerializer.Deserialize<T>(delivery.Body.Span, PsrMessageJson.Options) |
| 72 | + ?? throw new JsonException("The body is null."); |
| 73 | + } |
| 74 | + catch (JsonException ex) |
| 75 | + { |
| 76 | + Log.InvalidMessage(Logger, delivery.MessageId, delivery.Queue, ex.Message); |
| 77 | + return null; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// <summary> |
| 83 | +/// Consumes the public <c>StartPipelineRun</c> command (D-72): valid commands create a run (<c>run.queued</c>); unknown |
| 84 | +/// pipelines, invalid inputs and file inputs produce <c>run.rejected</c> with the reasons. Both carry the caller's |
| 85 | +/// <c>correlationId</c>. |
| 86 | +/// </summary> |
| 87 | +internal sealed class StartPipelineRunService( |
| 88 | + PsrBroker broker, ICatalog catalog, IRunEngine engine, AsyncRunLifecycle lifecycle, ILogger<StartPipelineRunService> logger) |
| 89 | + : ConsumerService(broker, logger) |
| 90 | +{ |
| 91 | + private const int MaxTextLength = 200; |
| 92 | + |
| 93 | + protected override QueueConsumerSettings Settings() => new() { Queue = PsrTopology.StartPipelineQueue, Prefetch = 8 }; |
| 94 | + |
| 95 | + protected override async Task<DeliveryOutcome> HandleAsync(PsrDelivery delivery, CancellationToken cancellationToken) |
| 96 | + { |
| 97 | + var command = Read<StartPipelineRunCommand>(delivery, PsrMessageTypes.StartPipelineRun); |
| 98 | + if (command is null || string.IsNullOrWhiteSpace(command.PipelineKey)) |
| 99 | + { |
| 100 | + return DeliveryOutcome.DeadLetter; |
| 101 | + } |
| 102 | + |
| 103 | + using var activity = RabbitMqDiagnostics.StartFrom("psr.message.consume", ActivityKind.Consumer, delivery.TraceParent); |
| 104 | + activity?.SetTag("messaging.destination.name", delivery.Queue); |
| 105 | + activity?.SetTag("psr.pipeline.key", command.PipelineKey); |
| 106 | + try |
| 107 | + { |
| 108 | + if (new[] { command.RequestedBy, command.CorrelationId, command.IdempotencyKey }.Any(text => text?.Length > MaxTextLength - 10)) |
| 109 | + { |
| 110 | + throw new PsrDomainException("TEXT_TOO_LONG", $"requestedBy, correlationId and idempotencyKey must have up to {MaxTextLength - 10} chars."); |
| 111 | + } |
| 112 | + |
| 113 | + var pipeline = await catalog.FindPipelineAsync(command.PipelineKey, command.Version, cancellationToken).ConfigureAwait(false) |
| 114 | + ?? throw (command.Version is { } version ? new VersionNotFoundException(command.PipelineKey, version) : new PipelineNotFoundException(command.PipelineKey)); |
| 115 | + var inputs = command.Inputs ?? []; |
| 116 | + var files = pipeline.Inputs.Where(f => f.Type.IsFile && (f.Required || inputs.ContainsKey(f.Name))).Select(f => f.Name).ToList(); |
| 117 | + if (files.Count > 0) |
| 118 | + { |
| 119 | + throw new PsrDomainException(PsrErrorCodes.FileInputNotAllowed, |
| 120 | + $"Pipeline '{pipeline.Key}' has file inputs ({string.Join(", ", files)}); the queue command cannot send files."); |
| 121 | + } |
| 122 | + |
| 123 | + await lifecycle.CreateAsync(new NewAsyncRun |
| 124 | + { |
| 125 | + Kind = RunKind.Pipeline, |
| 126 | + Pipeline = pipeline, |
| 127 | + Inputs = engine.BindInputs(pipeline.Inputs, (JsonObject)inputs.DeepClone()), |
| 128 | + Trigger = RunTrigger.QueueCommand, |
| 129 | + TriggeredBy = command.RequestedBy is { Length: > 0 } requestedBy ? $"queue:{requestedBy}" : "system:queue", |
| 130 | + IdempotencyKey = command.IdempotencyKey, |
| 131 | + ImplicitIdempotencyKey = delivery.MessageId is { Length: > 0 } messageId ? $"message:{messageId}" : null, |
| 132 | + CorrelationId = command.CorrelationId, |
| 133 | + TraceParent = Activity.Current?.Id ?? delivery.TraceParent, |
| 134 | + AnnounceDuplicate = true, |
| 135 | + }, cancellationToken).ConfigureAwait(false); |
| 136 | + } |
| 137 | + catch (Exception ex) when (ex is PsrException and not PsrConfigurationException) |
| 138 | + { |
| 139 | + Log.CommandRejected(Logger, command.PipelineKey, ex.Message); |
| 140 | + await lifecycle.PublishRejectedAsync(new RunEvent |
| 141 | + { |
| 142 | + EventId = Guid.CreateVersion7(), |
| 143 | + Type = RunEventTypes.RunRejected, |
| 144 | + OccurredAt = DateTimeOffset.UtcNow, |
| 145 | + Kind = RunKind.Pipeline, |
| 146 | + PipelineKey = command.PipelineKey, |
| 147 | + CorrelationId = command.CorrelationId, |
| 148 | + Trigger = RunTrigger.QueueCommand, |
| 149 | + Error = AsyncRunLifecycle.Rejection(ex), |
| 150 | + }, cancellationToken).ConfigureAwait(false); |
| 151 | + } |
| 152 | + |
| 153 | + return DeliveryOutcome.Ack; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/// <summary>Consumes the control queue of this instance: <c>CancelRun</c> stops the processes of the run if it executes here.</summary> |
| 158 | +internal sealed class ControlService(PsrBroker broker, IRunEngine engine, ILogger<ControlService> logger) : ConsumerService(broker, logger) |
| 159 | +{ |
| 160 | + protected override TimeSpan GracePeriod => TimeSpan.FromSeconds(5); |
| 161 | + |
| 162 | + protected override QueueConsumerSettings Settings() |
| 163 | + { |
| 164 | + var queue = PsrTopology.ControlQueue(Broker.InstanceId); |
| 165 | + return new() |
| 166 | + { |
| 167 | + Queue = queue, |
| 168 | + Prefetch = 4, |
| 169 | + Declare = async (channel, cancellationToken) => |
| 170 | + { |
| 171 | + await channel.QueueDeclareAsync(queue, durable: false, exclusive: true, autoDelete: true, cancellationToken: cancellationToken) |
| 172 | + .ConfigureAwait(false); |
| 173 | + await channel.QueueBindAsync(queue, PsrTopology.ControlExchange, string.Empty, cancellationToken: cancellationToken).ConfigureAwait(false); |
| 174 | + }, |
| 175 | + }; |
| 176 | + } |
| 177 | + |
| 178 | + protected override Task<DeliveryOutcome> HandleAsync(PsrDelivery delivery, CancellationToken cancellationToken) |
| 179 | + { |
| 180 | + if (Read<CancelRunCommand>(delivery, PsrMessageTypes.CancelRun) is not { } command) |
| 181 | + { |
| 182 | + return Task.FromResult(DeliveryOutcome.DeadLetter); |
| 183 | + } |
| 184 | + |
| 185 | + engine.RequestCancellation(command.RunId); |
| 186 | + return Task.FromResult(DeliveryOutcome.Ack); |
| 187 | + } |
| 188 | +} |
0 commit comments