| | | 1 | | namespace AsyncResponse; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// A durable multi-step flow: plain sequential C# whose steps are checkpointed, so the flow can be |
| | | 5 | | /// killed at any point (crash, redeploy, redelivery) and re-run safely — completed steps are |
| | | 6 | | /// skipped, the in-flight awaited step is re-attached, and everything after continues. |
| | | 7 | | /// <para> |
| | | 8 | | /// Implement the flow as ordinary code: conditionals, loops, and data flow are all allowed. The |
| | | 9 | | /// only rules are that each step has a stable unique name (names are persisted in the flow state) |
| | | 10 | | /// and that step bodies are safe to re-execute when the library cannot prove they completed |
| | | 11 | | /// (at-least-once, like every other delivery guarantee in AsyncResponse). |
| | | 12 | | /// </para> |
| | | 13 | | /// <para> |
| | | 14 | | /// Register the implementation in DI (e.g. <c>services.AddScoped<MyFlow>()</c>) and start it with |
| | | 15 | | /// <see cref="IDurableFlows.StartAsync{TFlow,TInput}"/>. The flow class name is persisted in the |
| | | 16 | | /// flow state and resolved when the flow is executed or resumed — treat the class name as a wire |
| | | 17 | | /// contract (rename with a forwarding type, like recovery-callback names). |
| | | 18 | | /// </para> |
| | | 19 | | /// </summary> |
| | | 20 | | /// <typeparam name="TInput"> |
| | | 21 | | /// The flow's input, persisted as JSON with the flow state and handed to every (re-)execution. |
| | | 22 | | /// Use one serializable record; capture nothing through closures. |
| | | 23 | | /// </typeparam> |
| | | 24 | | public interface IDurableFlow<in TInput> |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// The flow body. Invoked on start and on every resume/redelivery — always from the top, with |
| | | 28 | | /// completed steps skipping via their checkpoints. Must therefore be safe to call repeatedly. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="flow">The step context: checkpointed steps, awaited steps, progress, values.</param> |
| | | 31 | | /// <param name="input">The input the flow was started with, rehydrated from the flow state.</param> |
| | | 32 | | Task ExecuteAsync(IDurableFlowContext flow, TInput input); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Terminates a durable flow run as <see cref="FlowRunStatus.Failed"/> without transport |
| | | 37 | | /// redelivery. Any other exception thrown from a flow is treated as retriable: it propagates to |
| | | 38 | | /// the worker transport, which redelivers the flow run with bounded attempts and dead-letters it |
| | | 39 | | /// when they are exhausted. |
| | | 40 | | /// </summary> |
| | | 41 | | public sealed class DurableFlowFailedException : Exception |
| | | 42 | | { |
| | | 43 | | /// <summary>Creates a terminal flow failure with an operator-facing message.</summary> |
| | 3 | 44 | | public DurableFlowFailedException(string message) : base(message) |
| | | 45 | | { |
| | 3 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary>Creates a terminal flow failure wrapping the causing exception.</summary> |
| | 2 | 49 | | public DurableFlowFailedException(string message, Exception innerException) : base(message, innerException) |
| | | 50 | | { |
| | 3 | 51 | | } |
| | | 52 | | } |