| | | 1 | | using System.Text.Json.Serialization; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse; |
| | | 4 | | |
| | | 5 | | /// <summary>Lifecycle of a durable flow run.</summary> |
| | | 6 | | public enum FlowRunStatus |
| | | 7 | | { |
| | | 8 | | /// <summary>The run is executing or waiting to be (re-)executed.</summary> |
| | | 9 | | Running = 0, |
| | | 10 | | |
| | | 11 | | /// <summary>The flow body completed; every step checkpointed as done.</summary> |
| | | 12 | | Succeeded = 1, |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// The run was terminally failed — by a <see cref="DurableFlowFailedException"/>, or by the |
| | | 16 | | /// lost-subscriber failure route of an awaited step. |
| | | 17 | | /// </summary> |
| | | 18 | | Failed = 2, |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The run is parked by an operator. Wake-ups, recoveries, resumes, and failure signals are |
| | | 22 | | /// all ignored while suspended, so a dead-lettered run cannot be resurrected or terminally |
| | | 23 | | /// failed behind the operator's back by a late response. Not terminal: set the status back to |
| | | 24 | | /// <see cref="Running"/> and call <c>IDurableFlowExecutor.ResumeAsync</c> to replay the run |
| | | 25 | | /// from its checkpoints. A parent awaiting a suspended child keeps waiting. |
| | | 26 | | /// </summary> |
| | | 27 | | Suspended = 3 |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// The persisted state of one durable flow run: which steps completed (with memoized results), |
| | | 32 | | /// which awaited step is in flight, and the run's status. This is the flow's entire durable |
| | | 33 | | /// memory — the "ledger" of the checkpointed-flow pattern, owned by the library. |
| | | 34 | | /// <para> |
| | | 35 | | /// <b>Contract warning:</b> instances are serialized into the flow state store and must remain |
| | | 36 | | /// readable across deployments. Treat property names as a wire contract — additive changes only; |
| | | 37 | | /// <see cref="SchemaVersion"/> lets readers reject entries written with an unrecognized schema |
| | | 38 | | /// (see <see cref="FlowStateSchema"/>). |
| | | 39 | | /// </para> |
| | | 40 | | /// </summary> |
| | | 41 | | public sealed class FlowState |
| | | 42 | | { |
| | | 43 | | /// <summary>The required wire schema version this state was written with.</summary> |
| | | 44 | | [JsonRequired] |
| | 3 | 45 | | public int SchemaVersion { get; set; } = FlowStateSchema.Current; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Optimistic-concurrency revision maintained by durable flow stores. It starts at zero and is |
| | | 49 | | /// incremented on every conditional checkpoint so a stale executor cannot overwrite newer state. |
| | | 50 | | /// </summary> |
| | | 51 | | public long Revision { get; set; } |
| | | 52 | | |
| | | 53 | | /// <summary>The flow run id.</summary> |
| | | 54 | | public string? FlowId { get; set; } |
| | | 55 | | |
| | | 56 | | /// <summary>Full name of the flow class, resolved through DI on every (re-)execution.</summary> |
| | | 57 | | public string? FlowTypeName { get; set; } |
| | | 58 | | |
| | | 59 | | /// <summary>Full name of the input type.</summary> |
| | | 60 | | public string? InputTypeName { get; set; } |
| | | 61 | | |
| | | 62 | | /// <summary>The flow input, serialized as JSON.</summary> |
| | | 63 | | public string? InputJson { get; set; } |
| | | 64 | | |
| | | 65 | | /// <summary>The run's lifecycle status.</summary> |
| | | 66 | | public FlowRunStatus Status { get; set; } |
| | | 67 | | |
| | | 68 | | /// <summary>The most recent progress or outcome message (operator-facing).</summary> |
| | | 69 | | public string? LastMessage { get; set; } |
| | | 70 | | |
| | | 71 | | /// <summary>UTC timestamp the run was created.</summary> |
| | | 72 | | public DateTime? CreatedAtUtc { get; set; } |
| | | 73 | | |
| | | 74 | | /// <summary>UTC timestamp of the last persisted change.</summary> |
| | | 75 | | public DateTime? UpdatedAtUtc { get; set; } |
| | | 76 | | |
| | | 77 | | /// <summary>How many times the flow body has been entered (start, resumes, redeliveries).</summary> |
| | | 78 | | public int Attempts { get; set; } |
| | | 79 | | |
| | | 80 | | /// <summary>Per-step checkpoints, keyed by the step's stable name.</summary> |
| | | 81 | | public Dictionary<string, FlowStepState>? Steps { get; set; } |
| | | 82 | | |
| | | 83 | | /// <summary>The flow's user key/value bag (values serialized as JSON).</summary> |
| | | 84 | | public Dictionary<string, string>? Values { get; set; } |
| | | 85 | | |
| | | 86 | | /// <summary>Parent flow run id when this run was started by <see cref="IDurableFlowContext.AwaitChildFlowAsync{TFlo |
| | | 87 | | public string? ParentFlowId { get; set; } |
| | | 88 | | |
| | | 89 | | /// <summary>Parent step name that is waiting for this child flow, when any.</summary> |
| | | 90 | | public string? ParentStepName { get; set; } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Serialized ambient context captured when the run was started (see |
| | | 94 | | /// <see cref="IAsyncResponseContextPropagator"/>), restored before every (re-)execution — |
| | | 95 | | /// which may happen in a different deployment. |
| | | 96 | | /// </summary> |
| | | 97 | | public Dictionary<string, string>? Context { get; set; } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary>One step's checkpoint inside <see cref="FlowState"/>.</summary> |
| | | 101 | | public sealed class FlowStepState |
| | | 102 | | { |
| | | 103 | | /// <summary>Whether the step completed; completed steps are skipped on re-runs.</summary> |
| | | 104 | | public bool Completed { get; set; } |
| | | 105 | | |
| | | 106 | | /// <summary>The step's memoized result (or terminal response payload), serialized as JSON.</summary> |
| | | 107 | | public string? ResultJson { get; set; } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// The correlation id of the in-flight awaited operation — the breadcrumb a re-run uses to |
| | | 111 | | /// re-attach instead of re-triggering. Cleared when the step completes. |
| | | 112 | | /// </summary> |
| | | 113 | | public string? PendingCorrelationId { get; set; } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Whether the last attempt of this step faulted (timeout or exception); a faulted awaited |
| | | 117 | | /// step is restarted fresh instead of re-attached. |
| | | 118 | | /// </summary> |
| | | 119 | | public bool Faulted { get; set; } |
| | | 120 | | |
| | | 121 | | /// <summary>The step's most recent message (progress or failure).</summary> |
| | | 122 | | public string? Message { get; set; } |
| | | 123 | | |
| | | 124 | | /// <summary>Child flow run id when this checkpoint is waiting for a child flow.</summary> |
| | | 125 | | public string? ChildFlowId { get; set; } |
| | | 126 | | |
| | | 127 | | /// <summary>UTC timestamp the step completed.</summary> |
| | | 128 | | public DateTime? CompletedAtUtc { get; set; } |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Wire-schema version stamp for <see cref="FlowState"/>. Readers reject versions not explicitly |
| | | 133 | | /// supported by the build instead of guessing compatibility. |
| | | 134 | | /// </summary> |
| | | 135 | | public static class FlowStateSchema |
| | | 136 | | { |
| | | 137 | | /// <summary>The current wire schema version written by this build.</summary> |
| | | 138 | | public const int Current = 1; |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Returns <c>true</c> only for a schema version explicitly supported by this build. Add older |
| | | 142 | | /// versions here deliberately if a future release provides a tested migration path. |
| | | 143 | | /// </summary> |
| | | 144 | | public static bool IsReadable(int entryVersion) => entryVersion == Current; |
| | | 145 | | } |