|
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Nodes; |
| 3 | +using System.Text.Json.Serialization; |
| 4 | + |
| 5 | +namespace PythonScriptRunner; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Internal command <c>psr.execute-step.v1</c>: execute one attempt of a step. It carries identifiers only; inputs and files |
| 9 | +/// stay in the database. |
| 10 | +/// </summary> |
| 11 | +/// <param name="RunId">Run identifier.</param> |
| 12 | +/// <param name="RunCreatedAt">Creation time of the run (its partition).</param> |
| 13 | +/// <param name="StepRunId">Step identifier.</param> |
| 14 | +/// <param name="Attempt">Attempt number to start.</param> |
| 15 | +public sealed record ExecuteStepCommand(RunId RunId, DateTimeOffset RunCreatedAt, StepRunId StepRunId, int Attempt) |
| 16 | +{ |
| 17 | + /// <summary>Why the attempt starts.</summary> |
| 18 | + public AttemptCause Cause { get; init; } = AttemptCause.Initial; |
| 19 | + |
| 20 | + /// <summary>Who started the attempt (the reprocessing user, or the run's caller).</summary> |
| 21 | + public string? TriggeredBy { get; init; } |
| 22 | +} |
| 23 | + |
| 24 | +/// <summary> |
| 25 | +/// Public command <c>psr.start-pipeline-run.v1</c> (exchange <c>psr.public</c>): lets systems that are not .NET start a |
| 26 | +/// pipeline. Inputs cannot contain files. |
| 27 | +/// </summary> |
| 28 | +public sealed record StartPipelineRunCommand |
| 29 | +{ |
| 30 | + /// <summary>Pipeline key.</summary> |
| 31 | + public required string PipelineKey { get; init; } |
| 32 | + |
| 33 | + /// <summary>Version to run; <c>null</c> runs the latest published version.</summary> |
| 34 | + public int? Version { get; init; } |
| 35 | + |
| 36 | + /// <summary>Pipeline inputs (no files).</summary> |
| 37 | + public JsonObject? Inputs { get; init; } |
| 38 | + |
| 39 | + /// <summary>Deduplicates repeated commands for 24 hours (default: the AMQP <c>message_id</c>).</summary> |
| 40 | + public string? IdempotencyKey { get; init; } |
| 41 | + |
| 42 | + /// <summary>Returned in every event of the run.</summary> |
| 43 | + public string? CorrelationId { get; init; } |
| 44 | + |
| 45 | + /// <summary>Name of the calling system (the run is triggered by <c>queue:<name></c>).</summary> |
| 46 | + public string? RequestedBy { get; init; } |
| 47 | +} |
| 48 | + |
| 49 | +/// <summary>Control message <c>psr.cancel-run.v1</c> (fanout to every instance): stop the processes of a run.</summary> |
| 50 | +/// <param name="RunId">Run identifier.</param> |
| 51 | +/// <param name="StepRunId">Running step, when known.</param> |
| 52 | +/// <param name="RequestedBy">Who cancelled.</param> |
| 53 | +public sealed record CancelRunCommand(RunId RunId, StepRunId? StepRunId, string RequestedBy); |
| 54 | + |
| 55 | +/// <summary>Versioned message types (AMQP <c>type</c> property).</summary> |
| 56 | +public static class PsrMessageTypes |
| 57 | +{ |
| 58 | + /// <summary><see cref="ExecuteStepCommand"/>.</summary> |
| 59 | + public const string ExecuteStep = "psr.execute-step.v1"; |
| 60 | + |
| 61 | + /// <summary><see cref="StartPipelineRunCommand"/>.</summary> |
| 62 | + public const string StartPipelineRun = "psr.start-pipeline-run.v1"; |
| 63 | + |
| 64 | + /// <summary><see cref="CancelRunCommand"/>.</summary> |
| 65 | + public const string CancelRun = "psr.cancel-run.v1"; |
| 66 | + |
| 67 | + /// <summary><see cref="RunEvent"/>.</summary> |
| 68 | + public const string RunEvent = "psr.run-event.v1"; |
| 69 | +} |
| 70 | + |
| 71 | +/// <summary>JSON settings of the messages: camelCase properties and enum names as strings.</summary> |
| 72 | +public static class PsrMessageJson |
| 73 | +{ |
| 74 | + /// <summary>Shared options (do not modify).</summary> |
| 75 | + public static JsonSerializerOptions Options { get; } = CreateOptions(); |
| 76 | + |
| 77 | + private static JsonSerializerOptions CreateOptions() |
| 78 | + { |
| 79 | + var options = new JsonSerializerOptions(JsonSerializerDefaults.Web) |
| 80 | + { |
| 81 | + Converters = { new JsonStringEnumConverter() }, |
| 82 | + }; |
| 83 | + options.MakeReadOnly(populateMissingResolver: true); |
| 84 | + return options; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// <summary>Writes <see cref="RunId"/> as its GUID text.</summary> |
| 89 | +internal sealed class RunIdJsonConverter : JsonConverter<RunId> |
| 90 | +{ |
| 91 | + public override RunId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => new(reader.GetGuid()); |
| 92 | + |
| 93 | + public override void Write(Utf8JsonWriter writer, RunId value, JsonSerializerOptions options) => writer.WriteStringValue(value.Value); |
| 94 | +} |
| 95 | + |
| 96 | +/// <summary>Writes <see cref="StepRunId"/> as its GUID text.</summary> |
| 97 | +internal sealed class StepRunIdJsonConverter : JsonConverter<StepRunId> |
| 98 | +{ |
| 99 | + public override StepRunId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => new(reader.GetGuid()); |
| 100 | + |
| 101 | + public override void Write(Utf8JsonWriter writer, StepRunId value, JsonSerializerOptions options) => writer.WriteStringValue(value.Value); |
| 102 | +} |
0 commit comments