| | | 1 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Sequential; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Wraps an executor with a maximum execution duration. If the inner executor |
| | | 5 | | /// does not complete within the specified timeout, the linked cancellation token |
| | | 6 | | /// is triggered. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <param name="inner">The executor to wrap.</param> |
| | | 9 | | /// <param name="timeout">The maximum duration before cancellation.</param> |
| | | 10 | | /// <example> |
| | | 11 | | /// <code> |
| | | 12 | | /// var executor = new TimeoutExecutor(innerExecutor, TimeSpan.FromSeconds(30)); |
| | | 13 | | /// |
| | | 14 | | /// // Throws OperationCanceledException if inner takes longer than 30 seconds. |
| | | 15 | | /// var result = await executor.ExecuteAsync(context, cancellationToken); |
| | | 16 | | /// </code> |
| | | 17 | | /// </example> |
| | | 18 | | [DoNotAutoRegister] |
| | 6 | 19 | | public sealed class TimeoutExecutor( |
| | 6 | 20 | | IStageExecutor inner, |
| | 6 | 21 | | TimeSpan timeout) : IStageExecutor |
| | | 22 | | { |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public async Task<StageExecutionResult> ExecuteAsync( |
| | | 25 | | StageExecutionContext context, |
| | | 26 | | CancellationToken cancellationToken) |
| | | 27 | | { |
| | 3 | 28 | | using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); |
| | 3 | 29 | | cts.CancelAfter(timeout); |
| | 3 | 30 | | return await inner.ExecuteAsync(context, cts.Token); |
| | 2 | 31 | | } |
| | | 32 | | } |