| | | 1 | | using Kestrun.Hosting.Options; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Tasks; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a single-shot task execution with its current state and telemetry. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <param name="Id">Unique task identifier.</param> |
| | | 9 | | /// <param name="ScriptCode">The scripting language and code configuration for this task.</param> |
| | | 10 | | /// <param name="tokenSource">Cancellation token source for this task.</param> |
| | 15 | 11 | | public sealed class KestrunTask(string Id, LanguageOptions ScriptCode, CancellationTokenSource tokenSource) |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Unique task identifier. |
| | | 15 | | /// </summary> |
| | 23 | 16 | | public string Id { get; init; } = Id; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Optional human-friendly name of the task. |
| | | 20 | | /// </summary> |
| | 38 | 21 | | public string Name { get; set; } = "Task " + Id; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Optional description of the task. |
| | | 25 | | /// </summary> |
| | 38 | 26 | | public string Description { get; set; } = string.Empty; |
| | | 27 | | /// <summary> |
| | | 28 | | /// The scripting language and code configuration for this task. |
| | | 29 | | /// </summary> |
| | 15 | 30 | | public LanguageOptions ScriptCode { get; init; } = ScriptCode; |
| | | 31 | | /// <summary> |
| | | 32 | | /// Compiled work delegate producing a result. |
| | | 33 | | /// </summary> |
| | 27 | 34 | | public required Func<CancellationToken, Task<object?>> Work { get; init; } |
| | | 35 | | /// <summary> |
| | | 36 | | /// Cancellation token source for this task. |
| | | 37 | | /// </summary> |
| | 55 | 38 | | public CancellationTokenSource TokenSource { get; } = tokenSource; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Current state of the task. |
| | | 42 | | /// </summary> |
| | 142 | 43 | | public TaskState State { get; internal set; } = TaskState.NotStarted; |
| | | 44 | | /// <summary> |
| | | 45 | | /// Fault exception if the task failed. |
| | | 46 | | /// </summary> |
| | 0 | 47 | | public Exception? Fault { get; internal set; } |
| | | 48 | | /// <summary> |
| | | 49 | | /// Output produced by the task (last expression for C#/VB, pipeline for PowerShell). |
| | | 50 | | /// </summary> |
| | 14 | 51 | | public object? Output { get; internal set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// UTC timestamp when execution started. |
| | | 55 | | /// </summary> |
| | 19 | 56 | | public DateTimeOffset? StartedAtUtc { get; internal set; } |
| | | 57 | | /// <summary> |
| | | 58 | | /// UTC timestamp when execution ended. |
| | | 59 | | /// </summary> |
| | 19 | 60 | | public DateTimeOffset? CompletedAtUtc { get; internal set; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// The background task executing this work. |
| | | 64 | | /// </summary> |
| | 26 | 65 | | public Task? Runner { get; internal set; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Creates a basic task info record for this task. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <returns> A basic task info record. </returns> |
| | 7 | 71 | | public KrTask ToKrTask() => new(Id, Name, Description, State, StartedAtUtc, CompletedAtUtc, Progress, |
| | 7 | 72 | | Parent?.Id ?? string.Empty, |
| | 7 | 73 | | [.. Children.Select(c => c.Id)]); |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Indicates whether the task has reached a terminal state. |
| | | 77 | | /// </summary> |
| | 0 | 78 | | public bool Finished => State is TaskState.Completed or TaskState.Failed or TaskState.Stopped; |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// For hierarchical tasks; null for root tasks. |
| | | 82 | | /// </summary> |
| | 13 | 83 | | public KestrunTask Parent { get; set; } = null!; // For hierarchical tasks; null for root tasks |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Child tasks spawned by this task. |
| | | 87 | | /// </summary> |
| | 36 | 88 | | public List<KestrunTask> Children { get; } = []; |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Progress state of the task, if supported by the script. |
| | | 92 | | /// </summary> |
| | 61 | 93 | | public ProgressiveKestrunTaskState Progress { get; init; } = new(); |
| | | 94 | | } |