| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using Kestrun.Hosting.Options; |
| | | 3 | | using Kestrun.Scripting; |
| | | 4 | | |
| | | 5 | | namespace Kestrun.Tasks; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Service to run ad-hoc Kestrun tasks in PowerShell, C#, or VB.NET, with status, result, and cancellation. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <param name="pool">PowerShell runspace pool manager.</param> |
| | | 11 | | /// <param name="log">Logger instance.</param> |
| | 15 | 12 | | public sealed class KestrunTaskService(KestrunRunspacePoolManager pool, Serilog.ILogger log) |
| | | 13 | | { |
| | 15 | 14 | | private readonly ConcurrentDictionary<string, KestrunTask> _tasks = new(StringComparer.OrdinalIgnoreCase); |
| | 15 | 15 | | private readonly KestrunRunspacePoolManager _pool = pool; |
| | 15 | 16 | | private readonly Serilog.ILogger _log = log; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates a task from a code snippet without starting it. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="id">Optional unique task identifier. If null or empty, a new GUID will be generated.</param> |
| | | 22 | | /// <param name="scriptCode">The scripting language and code configuration for this task.</param> |
| | | 23 | | /// <param name="autoStart">Whether to start the task automatically.</param> |
| | | 24 | | /// <param name="name">Optional human-friendly name of the task.</param> |
| | | 25 | | /// <param name="description">Optional description of the task.</param> |
| | | 26 | | /// <returns>The unique identifier of the created task.</returns> |
| | | 27 | | /// <exception cref="ArgumentNullException">Thrown if scriptCode is null.</exception> |
| | | 28 | | /// <exception cref="InvalidOperationException">Thrown if a task with the same id already exists.</exception> |
| | | 29 | | public string Create(string? id, LanguageOptions scriptCode, bool autoStart, string? name, string? description = nul |
| | | 30 | | { |
| | 17 | 31 | | ArgumentNullException.ThrowIfNull(scriptCode); |
| | | 32 | | |
| | 16 | 33 | | if (string.IsNullOrWhiteSpace(id)) |
| | | 34 | | { |
| | 14 | 35 | | id = Guid.NewGuid().ToString("n"); |
| | | 36 | | } |
| | 16 | 37 | | if (_tasks.ContainsKey(id)) |
| | | 38 | | { |
| | 1 | 39 | | throw new InvalidOperationException($"Task id '{id}' already exists."); |
| | | 40 | | } |
| | | 41 | | |
| | 15 | 42 | | var progress = new ProgressiveKestrunTaskState(); |
| | 15 | 43 | | var cfg = new TaskJobFactory.TaskJobConfig(id, scriptCode, _log, _pool, progress); |
| | 15 | 44 | | var work = TaskJobFactory.Create(cfg); |
| | 15 | 45 | | var cts = new CancellationTokenSource(); |
| | 15 | 46 | | var task = new KestrunTask(id, scriptCode, cts) |
| | 15 | 47 | | { |
| | 15 | 48 | | Work = work, |
| | 15 | 49 | | Progress = progress, |
| | 15 | 50 | | Name = string.IsNullOrWhiteSpace(name) ? ("Task " + id) : name, |
| | 15 | 51 | | Description = string.IsNullOrWhiteSpace(description) ? string.Empty : description |
| | 15 | 52 | | }; |
| | | 53 | | |
| | 15 | 54 | | if (!_tasks.TryAdd(id, task)) |
| | | 55 | | { |
| | 0 | 56 | | throw new InvalidOperationException($"Task id '{id}' already exists."); |
| | | 57 | | } |
| | 15 | 58 | | if (autoStart) |
| | | 59 | | { |
| | 2 | 60 | | _ = Start(id); |
| | | 61 | | } |
| | 15 | 62 | | return id; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Sets or updates the name of a task. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="id">The task identifier.</param> |
| | | 69 | | /// <param name="name">The new name for the task.</param> |
| | | 70 | | /// <returns>True if the task was found and updated; false if not found.</returns> |
| | | 71 | | public bool SetTaskName(string id, string name) |
| | | 72 | | { |
| | 3 | 73 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 74 | | { |
| | 1 | 75 | | throw new ArgumentNullException(nameof(name)); |
| | | 76 | | } |
| | | 77 | | |
| | 2 | 78 | | if (!_tasks.TryGetValue(id, out var task)) |
| | | 79 | | { |
| | 1 | 80 | | return false; |
| | | 81 | | } |
| | 1 | 82 | | task.Name = name; |
| | 1 | 83 | | return true; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Sets or updates the description of a task. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="id">The task identifier.</param> |
| | | 90 | | /// <param name="description">The new description for the task.</param> |
| | | 91 | | /// <returns>True if the task was found and updated; false if not found.</returns> |
| | | 92 | | public bool SetTaskDescription(string id, string description) |
| | | 93 | | { |
| | 3 | 94 | | if (string.IsNullOrWhiteSpace(description)) |
| | | 95 | | { |
| | 1 | 96 | | throw new ArgumentNullException(nameof(description)); |
| | | 97 | | } |
| | 2 | 98 | | if (!_tasks.TryGetValue(id, out var task)) |
| | | 99 | | { |
| | 1 | 100 | | return false; |
| | | 101 | | } |
| | 1 | 102 | | task.Description = description; |
| | 1 | 103 | | return true; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Starts a previously created task by id. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="id">The task identifier.</param> |
| | | 110 | | /// <returns>True if the task was found and started; false if not found or already started.</returns> |
| | | 111 | | public bool Start(string id) |
| | | 112 | | { |
| | 13 | 113 | | if (!_tasks.TryGetValue(id, out var task)) |
| | | 114 | | { |
| | 1 | 115 | | return false; |
| | | 116 | | } |
| | 12 | 117 | | if (task.State != TaskState.NotStarted || task.Runner != null) |
| | | 118 | | { |
| | 1 | 119 | | return false; // only start once from Created state |
| | | 120 | | } |
| | 22 | 121 | | task.Runner = Task.Run(async () => await ExecuteAsync(task).ConfigureAwait(false), task.TokenSource.Token); |
| | 11 | 122 | | return true; |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Starts a previously created task by id, and awaits its completion. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="id">The task identifier.</param> |
| | | 129 | | /// <returns>True if the task was found and started; false if not found or already started.</returns> |
| | | 130 | | public async Task<bool> StartAsync(string id) |
| | | 131 | | { |
| | 2 | 132 | | if (!_tasks.TryGetValue(id, out var task)) |
| | | 133 | | { |
| | 0 | 134 | | return false; |
| | | 135 | | } |
| | | 136 | | |
| | 2 | 137 | | if (task.State != TaskState.NotStarted || task.Runner != null) |
| | | 138 | | { |
| | 1 | 139 | | return false; // only start once from Created state |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | // Launch the task asynchronously and store its runner |
| | 2 | 143 | | task.Runner = Task.Run(() => ExecuteAsync(task), task.TokenSource.Token); |
| | | 144 | | |
| | | 145 | | try |
| | | 146 | | { |
| | 1 | 147 | | await task.Runner.ConfigureAwait(false); |
| | 1 | 148 | | } |
| | 0 | 149 | | catch (OperationCanceledException) |
| | | 150 | | { |
| | | 151 | | // Optional: handle cancellation gracefully |
| | 0 | 152 | | } |
| | 0 | 153 | | catch (Exception ex) |
| | | 154 | | { |
| | | 155 | | // Optional: handle or log errors |
| | 0 | 156 | | _log.Error(ex, "Task {Id} failed", id); |
| | 0 | 157 | | } |
| | | 158 | | |
| | 1 | 159 | | return true; |
| | 2 | 160 | | } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Gets a task by id. |
| | | 164 | | /// </summary> |
| | | 165 | | /// <param name="id">The task identifier.</param> |
| | | 166 | | /// <returns>The task result, or null if not found.</returns> |
| | | 167 | | public KrTask? Get(string id) |
| | 8 | 168 | | => _tasks.TryGetValue(id, out var t) ? t.ToKrTask() : null; |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// Gets the current state for a task. |
| | | 172 | | /// </summary> |
| | | 173 | | /// <param name="id">The task identifier.</param> |
| | | 174 | | /// <returns>The task state, or null if not found.</returns> |
| | | 175 | | public TaskState? GetState(string id) |
| | 71 | 176 | | => _tasks.TryGetValue(id, out var t) ? t.State : null; |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Gets the output object for a completed task. |
| | | 180 | | /// </summary> |
| | | 181 | | /// <param name="id">The task identifier.</param> |
| | | 182 | | /// <returns>The task output object, or null if not found or no output.</returns> |
| | | 183 | | public object? GetResult(string id) |
| | 6 | 184 | | => _tasks.TryGetValue(id, out var t) ? t.Output : null; |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Attempts to cancel a task. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <remarks> |
| | | 190 | | /// If the task has not been started (NotStarted) it is transitioned directly to the terminal |
| | | 191 | | /// Stopped state so it can be removed later and does not remain orphaned. |
| | | 192 | | /// </remarks> |
| | | 193 | | public bool Cancel(string id) |
| | | 194 | | { |
| | 6 | 195 | | if (!_tasks.TryGetValue(id, out var t)) |
| | | 196 | | { |
| | 1 | 197 | | return false; |
| | | 198 | | } |
| | 5 | 199 | | if (t.State is TaskState.Completed or TaskState.Failed or TaskState.Stopped) |
| | | 200 | | { |
| | 1 | 201 | | return false; |
| | | 202 | | } |
| | | 203 | | |
| | 4 | 204 | | if (t.State == TaskState.NotStarted) |
| | | 205 | | { |
| | 0 | 206 | | _log.Information("Cancelling task {Id} before start", id); |
| | | 207 | | // Transition to a terminal state so Remove() can succeed |
| | 0 | 208 | | t.State = TaskState.Stopped; |
| | 0 | 209 | | t.Progress.Cancel("Cancelled before start"); |
| | 0 | 210 | | var now = DateTimeOffset.UtcNow; |
| | 0 | 211 | | t.StartedAtUtc ??= now; |
| | 0 | 212 | | t.CompletedAtUtc = now; |
| | 0 | 213 | | t.TokenSource.Cancel(); // ensure any future Start() attempt observes cancellation |
| | 0 | 214 | | return true; |
| | | 215 | | } |
| | | 216 | | |
| | 4 | 217 | | _log.Information("Cancelling running task {Id}", id); |
| | 4 | 218 | | t.TokenSource.Cancel(); |
| | 4 | 219 | | return true; |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// Checks recursively if all children of a task are finished. |
| | | 224 | | /// </summary> |
| | | 225 | | /// <param name="task">The parent task to check.</param> |
| | | 226 | | /// <returns>True if all children are finished; false otherwise.</returns> |
| | | 227 | | private static bool ChildrenAreFinished(KestrunTask task) |
| | | 228 | | { |
| | 17 | 229 | | foreach (var child in task.Children) |
| | | 230 | | { |
| | 2 | 231 | | if (!ChildrenAreFinished(child)) |
| | | 232 | | { |
| | 0 | 233 | | return false; |
| | | 234 | | } |
| | 2 | 235 | | if (child.State is not TaskState.Completed and not TaskState.Failed and not TaskState.Stopped) |
| | | 236 | | { |
| | 1 | 237 | | return false; |
| | | 238 | | } |
| | | 239 | | } |
| | 6 | 240 | | return true; |
| | 1 | 241 | | } |
| | | 242 | | /// <summary> |
| | | 243 | | /// Removes a finished task from the registry. |
| | | 244 | | /// </summary> |
| | | 245 | | /// <param name="id">The task identifier.</param> |
| | | 246 | | /// <returns>True if the task was found and removed; false if not found or not finished.</returns> |
| | | 247 | | /// <remarks> |
| | | 248 | | /// A task can only be removed if it is in a terminal state (Completed, Failed, Stopped) |
| | | 249 | | /// and all its child tasks are also in terminal states. |
| | | 250 | | /// </remarks> |
| | | 251 | | public bool Remove(string id) |
| | | 252 | | { |
| | 7 | 253 | | if (_tasks.TryGetValue(id, out var t)) |
| | | 254 | | { |
| | 6 | 255 | | if (t.State is TaskState.Completed or TaskState.Failed or TaskState.Stopped) |
| | | 256 | | { |
| | 5 | 257 | | if (!ChildrenAreFinished(t)) |
| | | 258 | | { |
| | 1 | 259 | | _log.Warning("Cannot remove task {Id} because it has running child tasks", id); |
| | 1 | 260 | | return false; |
| | | 261 | | } |
| | | 262 | | |
| | 4 | 263 | | _log.Information("Removing task {Id}", id); |
| | 4 | 264 | | if (_tasks.TryRemove(id, out _)) |
| | | 265 | | { |
| | | 266 | | // Detach from parent first so recursive child removals can't mutate the list we iterate |
| | 4 | 267 | | if (t.Parent is not null) |
| | | 268 | | { |
| | 1 | 269 | | _ = t.Parent.Children.Remove(t); |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | // Take a point-in-time snapshot because recursive Remove(child.Id) will |
| | | 273 | | // mutate the parent's Children collection (each child removes itself). |
| | 4 | 274 | | if (t.Children.Count > 0) |
| | | 275 | | { |
| | 1 | 276 | | var snapshot = t.Children.ToArray(); |
| | 4 | 277 | | foreach (var child in snapshot) |
| | | 278 | | { |
| | 1 | 279 | | if (!Remove(child.Id)) |
| | | 280 | | { |
| | 0 | 281 | | _log.Warning("Failed to remove child task {ChildId} of parent task {ParentId}", child.Id |
| | | 282 | | } |
| | | 283 | | } |
| | | 284 | | } |
| | 4 | 285 | | return true; |
| | | 286 | | } |
| | | 287 | | } |
| | 1 | 288 | | return false; |
| | | 289 | | } |
| | 1 | 290 | | return false; |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | /// <summary> |
| | | 294 | | /// Lists all tasks with basic info. |
| | | 295 | | /// Does not include output or error details. |
| | | 296 | | /// </summary> |
| | | 297 | | public IReadOnlyCollection<KrTask> List() |
| | 5 | 298 | | => [.. _tasks.Values.Select(v => v.ToKrTask())]; |
| | | 299 | | |
| | | 300 | | /// <summary> |
| | | 301 | | /// Executes the task's work function and updates its state accordingly. |
| | | 302 | | /// </summary> |
| | | 303 | | /// <param name="task">The task to execute.</param> |
| | | 304 | | /// <returns>A task representing the asynchronous operation.</returns> |
| | | 305 | | private async Task ExecuteAsync(KestrunTask task) |
| | | 306 | | { |
| | 12 | 307 | | task.State = TaskState.Running; |
| | 12 | 308 | | task.Progress.StatusMessage = "Running"; |
| | 12 | 309 | | task.StartedAtUtc = DateTimeOffset.UtcNow; |
| | | 310 | | try |
| | | 311 | | { |
| | 12 | 312 | | var result = await task.Work(task.TokenSource.Token).ConfigureAwait(false); |
| | 9 | 313 | | task.Output = result; |
| | 9 | 314 | | task.State = task.TokenSource.IsCancellationRequested ? TaskState.Stopped : TaskState.Completed; |
| | 9 | 315 | | if (task.State == TaskState.Completed) |
| | | 316 | | { |
| | 8 | 317 | | task.Progress.Complete("Completed"); |
| | | 318 | | } |
| | 1 | 319 | | else if (task.State == TaskState.Stopped) |
| | | 320 | | { |
| | | 321 | | // If cancellation was requested but no exception was thrown (graceful exit), normalize progress |
| | 1 | 322 | | task.Progress.Cancel("Cancelled"); |
| | | 323 | | } |
| | 9 | 324 | | } |
| | 3 | 325 | | catch (OperationCanceledException) when (task.TokenSource.IsCancellationRequested) |
| | | 326 | | { |
| | 3 | 327 | | task.State = TaskState.Stopped; |
| | 3 | 328 | | task.Progress.Cancel("Cancelled"); |
| | 3 | 329 | | } |
| | 0 | 330 | | catch (TaskCanceledException) when (task.TokenSource.IsCancellationRequested) |
| | | 331 | | { |
| | | 332 | | // Some libraries throw TaskCanceledException instead of OperationCanceledException on cancellation |
| | 0 | 333 | | task.State = TaskState.Stopped; |
| | 0 | 334 | | task.Progress.Cancel("Cancelled"); |
| | 0 | 335 | | } |
| | 0 | 336 | | catch (Exception ex) when (task.TokenSource.IsCancellationRequested) |
| | | 337 | | { |
| | | 338 | | // During cancellation, certain engines (e.g., PowerShell) may surface non-cancellation exceptions |
| | | 339 | | // such as PipelineStoppedException. If cancellation was requested, normalize to Stopped. |
| | 0 | 340 | | task.State = TaskState.Stopped; |
| | 0 | 341 | | task.Progress.Cancel("Cancelled"); |
| | 0 | 342 | | _log.Information(ex, "Task {Id} cancelled with exception after cancellation was requested", task.Id); |
| | 0 | 343 | | } |
| | 0 | 344 | | catch (Exception ex) |
| | | 345 | | { |
| | 0 | 346 | | task.Fault = ex; |
| | 0 | 347 | | task.State = TaskState.Failed; |
| | 0 | 348 | | _log.Error(ex, "Task {Id} failed", task.Id); |
| | 0 | 349 | | task.Progress.Fail("Failed"); |
| | 0 | 350 | | } |
| | | 351 | | finally |
| | | 352 | | { |
| | 12 | 353 | | task.CompletedAtUtc = DateTimeOffset.UtcNow; |
| | | 354 | | } |
| | 12 | 355 | | } |
| | | 356 | | } |