| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using Kestrun.Hosting; |
| | | 3 | | using Kestrun.Hosting.Options; |
| | | 4 | | using Kestrun.Scripting; |
| | | 5 | | |
| | | 6 | | namespace Kestrun.Tasks; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Service to run ad-hoc Kestrun tasks in PowerShell, C#, or VB.NET, with status, result, and cancellation. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <param name="pool">PowerShell runspace pool manager.</param> |
| | | 12 | | /// <param name="log">Logger instance.</param> |
| | 17 | 13 | | public sealed class KestrunTaskService(KestrunRunspacePoolManager pool, Serilog.ILogger log) : IDisposable |
| | | 14 | | { |
| | 1 | 15 | | private static readonly TimeSpan DisposeWaitTimeout = TimeSpan.FromSeconds(5); |
| | 17 | 16 | | private readonly ConcurrentDictionary<string, KestrunTask> _tasks = new(StringComparer.OrdinalIgnoreCase); |
| | 52 | 17 | | internal KestrunRunspacePoolManager TaskRunspacePool { get; } = pool; |
| | 17 | 18 | | private readonly Serilog.ILogger _log = log; |
| | | 19 | | private int _disposed; |
| | | 20 | | |
| | 16 | 21 | | private KestrunHost Host => TaskRunspacePool.Host; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates a task from a code snippet without starting it. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="id">Optional unique task identifier. If null or empty, a new GUID will be generated.</param> |
| | | 27 | | /// <param name="scriptCode">The scripting language and code configuration for this task.</param> |
| | | 28 | | /// <param name="autoStart">Whether to start the task automatically.</param> |
| | | 29 | | /// <param name="name">Optional human-friendly name of the task.</param> |
| | | 30 | | /// <param name="description">Optional description of the task.</param> |
| | | 31 | | /// <returns>The unique identifier of the created task.</returns> |
| | | 32 | | /// <exception cref="ArgumentNullException">Thrown if scriptCode is null.</exception> |
| | | 33 | | /// <exception cref="InvalidOperationException">Thrown if a task with the same id already exists.</exception> |
| | | 34 | | public string Create(string? id, LanguageOptions scriptCode, bool autoStart, string? name, string? description = nul |
| | | 35 | | { |
| | 18 | 36 | | ThrowIfDisposed(); |
| | 18 | 37 | | ArgumentNullException.ThrowIfNull(scriptCode); |
| | | 38 | | |
| | 17 | 39 | | if (string.IsNullOrWhiteSpace(id)) |
| | | 40 | | { |
| | 15 | 41 | | id = Guid.NewGuid().ToString("n"); |
| | | 42 | | } |
| | 17 | 43 | | if (_tasks.ContainsKey(id)) |
| | | 44 | | { |
| | 1 | 45 | | throw new InvalidOperationException($"Task id '{id}' already exists."); |
| | | 46 | | } |
| | | 47 | | |
| | 16 | 48 | | var progress = new ProgressiveKestrunTaskState(); |
| | 16 | 49 | | var cfg = new TaskJobFactory.TaskJobConfig(Host, id, scriptCode, TaskRunspacePool, progress); |
| | 16 | 50 | | var work = TaskJobFactory.Create(cfg); |
| | 16 | 51 | | var cts = new CancellationTokenSource(); |
| | 16 | 52 | | var task = new KestrunTask(id, scriptCode, cts) |
| | 16 | 53 | | { |
| | 16 | 54 | | Work = work, |
| | 16 | 55 | | Progress = progress, |
| | 16 | 56 | | Name = string.IsNullOrWhiteSpace(name) ? ("Task " + id) : name, |
| | 16 | 57 | | Description = string.IsNullOrWhiteSpace(description) ? string.Empty : description |
| | 16 | 58 | | }; |
| | | 59 | | |
| | 16 | 60 | | if (!_tasks.TryAdd(id, task)) |
| | | 61 | | { |
| | 0 | 62 | | throw new InvalidOperationException($"Task id '{id}' already exists."); |
| | | 63 | | } |
| | 16 | 64 | | if (autoStart) |
| | | 65 | | { |
| | 2 | 66 | | _ = Start(id); |
| | | 67 | | } |
| | 16 | 68 | | return id; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Sets or updates the name of a task. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="id">The task identifier.</param> |
| | | 75 | | /// <param name="name">The new name for the task.</param> |
| | | 76 | | /// <returns>True if the task was found and updated; false if not found.</returns> |
| | | 77 | | public bool SetTaskName(string id, string name) |
| | | 78 | | { |
| | 3 | 79 | | ThrowIfDisposed(); |
| | 3 | 80 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 81 | | { |
| | 1 | 82 | | throw new ArgumentNullException(nameof(name)); |
| | | 83 | | } |
| | | 84 | | |
| | 2 | 85 | | if (!_tasks.TryGetValue(id, out var task)) |
| | | 86 | | { |
| | 1 | 87 | | return false; |
| | | 88 | | } |
| | 1 | 89 | | task.Name = name; |
| | 1 | 90 | | return true; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Sets or updates the description of a task. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="id">The task identifier.</param> |
| | | 97 | | /// <param name="description">The new description for the task.</param> |
| | | 98 | | /// <returns>True if the task was found and updated; false if not found.</returns> |
| | | 99 | | public bool SetTaskDescription(string id, string description) |
| | | 100 | | { |
| | 3 | 101 | | ThrowIfDisposed(); |
| | 3 | 102 | | if (string.IsNullOrWhiteSpace(description)) |
| | | 103 | | { |
| | 1 | 104 | | throw new ArgumentNullException(nameof(description)); |
| | | 105 | | } |
| | 2 | 106 | | if (!_tasks.TryGetValue(id, out var task)) |
| | | 107 | | { |
| | 1 | 108 | | return false; |
| | | 109 | | } |
| | 1 | 110 | | task.Description = description; |
| | 1 | 111 | | return true; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Starts a previously created task by id. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="id">The task identifier.</param> |
| | | 118 | | /// <returns>True if the task was found and started; false if not found or already started.</returns> |
| | | 119 | | public bool Start(string id) |
| | | 120 | | { |
| | 13 | 121 | | ThrowIfDisposed(); |
| | 13 | 122 | | if (!_tasks.TryGetValue(id, out var task)) |
| | | 123 | | { |
| | 1 | 124 | | return false; |
| | | 125 | | } |
| | 12 | 126 | | if (task.State != TaskState.NotStarted || task.Runner != null) |
| | | 127 | | { |
| | 1 | 128 | | return false; // only start once from Created state |
| | | 129 | | } |
| | 22 | 130 | | task.Runner = Task.Run(async () => await ExecuteAsync(task).ConfigureAwait(false), task.Token); |
| | 11 | 131 | | return true; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Starts a previously created task by id, and awaits its completion. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <param name="id">The task identifier.</param> |
| | | 138 | | /// <returns>True if the task was found and started; false if not found or already started.</returns> |
| | | 139 | | public async Task<bool> StartAsync(string id) |
| | | 140 | | { |
| | 2 | 141 | | ThrowIfDisposed(); |
| | 2 | 142 | | if (!_tasks.TryGetValue(id, out var task)) |
| | | 143 | | { |
| | 0 | 144 | | return false; |
| | | 145 | | } |
| | | 146 | | |
| | 2 | 147 | | if (task.State != TaskState.NotStarted || task.Runner != null) |
| | | 148 | | { |
| | 1 | 149 | | return false; // only start once from Created state |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | // Launch the task asynchronously and store its runner |
| | 2 | 153 | | task.Runner = Task.Run(() => ExecuteAsync(task), task.Token); |
| | | 154 | | |
| | | 155 | | try |
| | | 156 | | { |
| | 1 | 157 | | await task.Runner.ConfigureAwait(false); |
| | 1 | 158 | | } |
| | 0 | 159 | | catch (OperationCanceledException) |
| | | 160 | | { |
| | | 161 | | // Optional: handle cancellation gracefully |
| | 0 | 162 | | } |
| | 0 | 163 | | catch (Exception ex) |
| | | 164 | | { |
| | | 165 | | // Optional: handle or log errors |
| | 0 | 166 | | _log.Error(ex, "Task {Id} failed", id); |
| | 0 | 167 | | } |
| | | 168 | | |
| | 1 | 169 | | return true; |
| | 2 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Gets a task by id. |
| | | 174 | | /// </summary> |
| | | 175 | | /// <param name="id">The task identifier.</param> |
| | | 176 | | /// <returns>The task result, or null if not found.</returns> |
| | | 177 | | public KrTask? Get(string id) |
| | | 178 | | { |
| | 8 | 179 | | ThrowIfDisposed(); |
| | 8 | 180 | | return _tasks.TryGetValue(id, out var t) ? t.ToKrTask() : null; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Gets the current state for a task. |
| | | 185 | | /// </summary> |
| | | 186 | | /// <param name="id">The task identifier.</param> |
| | | 187 | | /// <returns>The task state, or null if not found.</returns> |
| | | 188 | | public TaskState? GetState(string id) |
| | | 189 | | { |
| | 199 | 190 | | ThrowIfDisposed(); |
| | 199 | 191 | | return _tasks.TryGetValue(id, out var t) ? t.State : null; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | /// <summary> |
| | | 195 | | /// Gets the output object for a completed task. |
| | | 196 | | /// </summary> |
| | | 197 | | /// <param name="id">The task identifier.</param> |
| | | 198 | | /// <returns>The task output object, or null if not found or no output.</returns> |
| | | 199 | | public object? GetResult(string id) |
| | | 200 | | { |
| | 6 | 201 | | ThrowIfDisposed(); |
| | 6 | 202 | | return _tasks.TryGetValue(id, out var t) ? t.Output : null; |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | /// <summary> |
| | | 206 | | /// Attempts to cancel a task. |
| | | 207 | | /// </summary> |
| | | 208 | | /// <remarks> |
| | | 209 | | /// If the task has not been started (NotStarted) it is transitioned directly to the terminal |
| | | 210 | | /// Stopped state so it can be removed later and does not remain orphaned. |
| | | 211 | | /// </remarks> |
| | | 212 | | public bool Cancel(string id) |
| | | 213 | | { |
| | 5 | 214 | | ThrowIfDisposed(); |
| | 5 | 215 | | if (!_tasks.TryGetValue(id, out var t)) |
| | | 216 | | { |
| | 1 | 217 | | return false; |
| | | 218 | | } |
| | 4 | 219 | | if (t.State is TaskState.Completed or TaskState.Failed or TaskState.Stopped) |
| | | 220 | | { |
| | 1 | 221 | | return false; |
| | | 222 | | } |
| | | 223 | | |
| | 3 | 224 | | if (t.State == TaskState.NotStarted) |
| | | 225 | | { |
| | 0 | 226 | | _log.Information("Cancelling task {Id} before start", id); |
| | | 227 | | // Transition to a terminal state so Remove() can succeed |
| | 0 | 228 | | t.State = TaskState.Stopped; |
| | 0 | 229 | | t.Progress.Cancel("Cancelled before start"); |
| | 0 | 230 | | var now = DateTimeOffset.UtcNow; |
| | 0 | 231 | | t.StartedAtUtc ??= now; |
| | 0 | 232 | | t.CompletedAtUtc = now; |
| | 0 | 233 | | t.TokenSource.Cancel(); // ensure any future Start() attempt observes cancellation |
| | 0 | 234 | | return true; |
| | | 235 | | } |
| | | 236 | | |
| | 3 | 237 | | _log.Information("Cancelling running task {Id}", id); |
| | 3 | 238 | | t.TokenSource.Cancel(); |
| | 3 | 239 | | return true; |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | /// <summary> |
| | | 243 | | /// Checks recursively if all children of a task are finished. |
| | | 244 | | /// </summary> |
| | | 245 | | /// <param name="task">The parent task to check.</param> |
| | | 246 | | /// <returns>True if all children are finished; false otherwise.</returns> |
| | | 247 | | private static bool ChildrenAreFinished(KestrunTask task) |
| | | 248 | | { |
| | 17 | 249 | | foreach (var child in task.Children) |
| | | 250 | | { |
| | 2 | 251 | | if (!ChildrenAreFinished(child)) |
| | | 252 | | { |
| | 0 | 253 | | return false; |
| | | 254 | | } |
| | 2 | 255 | | if (child.State is not TaskState.Completed and not TaskState.Failed and not TaskState.Stopped) |
| | | 256 | | { |
| | 1 | 257 | | return false; |
| | | 258 | | } |
| | | 259 | | } |
| | 6 | 260 | | return true; |
| | 1 | 261 | | } |
| | | 262 | | /// <summary> |
| | | 263 | | /// Removes a finished task from the registry. |
| | | 264 | | /// </summary> |
| | | 265 | | /// <param name="id">The task identifier.</param> |
| | | 266 | | /// <returns>True if the task was found and removed; false if not found or not finished.</returns> |
| | | 267 | | /// <remarks> |
| | | 268 | | /// A task can only be removed if it is in a terminal state (Completed, Failed, Stopped) |
| | | 269 | | /// and all its child tasks are also in terminal states. |
| | | 270 | | /// </remarks> |
| | | 271 | | public bool Remove(string id) |
| | | 272 | | { |
| | 7 | 273 | | ThrowIfDisposed(); |
| | 7 | 274 | | if (_tasks.TryGetValue(id, out var t)) |
| | | 275 | | { |
| | 6 | 276 | | if (t.State is TaskState.Completed or TaskState.Failed or TaskState.Stopped) |
| | | 277 | | { |
| | 5 | 278 | | if (!ChildrenAreFinished(t)) |
| | | 279 | | { |
| | 1 | 280 | | _log.Warning("Cannot remove task {Id} because it has running child tasks", id); |
| | 1 | 281 | | return false; |
| | | 282 | | } |
| | | 283 | | |
| | 4 | 284 | | _log.Information("Removing task {Id}", id); |
| | 4 | 285 | | if (_tasks.TryRemove(id, out _)) |
| | | 286 | | { |
| | | 287 | | // Detach from parent first so recursive child removals can't mutate the list we iterate |
| | 4 | 288 | | if (t.Parent is not null) |
| | | 289 | | { |
| | 1 | 290 | | _ = t.Parent.Children.Remove(t); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | // Take a point-in-time snapshot because recursive Remove(child.Id) will |
| | | 294 | | // mutate the parent's Children collection (each child removes itself). |
| | 4 | 295 | | if (t.Children.Count > 0) |
| | | 296 | | { |
| | 1 | 297 | | var snapshot = t.Children.ToArray(); |
| | 4 | 298 | | foreach (var child in snapshot) |
| | | 299 | | { |
| | 1 | 300 | | if (!Remove(child.Id)) |
| | | 301 | | { |
| | 0 | 302 | | _log.Warning("Failed to remove child task {ChildId} of parent task {ParentId}", child.Id |
| | | 303 | | } |
| | | 304 | | } |
| | | 305 | | } |
| | 4 | 306 | | return true; |
| | | 307 | | } |
| | | 308 | | } |
| | 1 | 309 | | return false; |
| | | 310 | | } |
| | 1 | 311 | | return false; |
| | | 312 | | } |
| | | 313 | | |
| | | 314 | | /// <summary> |
| | | 315 | | /// Lists all tasks with basic info. |
| | | 316 | | /// Does not include output or error details. |
| | | 317 | | /// </summary> |
| | | 318 | | public IReadOnlyCollection<KrTask> List() |
| | | 319 | | { |
| | 2 | 320 | | ThrowIfDisposed(); |
| | 5 | 321 | | return [.. _tasks.Values.Select(v => v.ToKrTask())]; |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | /// <summary> |
| | | 325 | | /// Throws when the service has already been disposed. |
| | | 326 | | /// </summary> |
| | | 327 | | private void ThrowIfDisposed() |
| | 266 | 328 | | => ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) != 0, nameof(KestrunTaskService)); |
| | | 329 | | |
| | | 330 | | /// <summary> |
| | | 331 | | /// Tries to get the live task model for internal consumers such as tests. |
| | | 332 | | /// </summary> |
| | | 333 | | /// <param name="id">The task identifier.</param> |
| | | 334 | | /// <param name="task">The matching task when found; otherwise null.</param> |
| | | 335 | | /// <returns>True when the task exists; otherwise false.</returns> |
| | | 336 | | internal bool TryGetTask(string id, out KestrunTask? task) |
| | 1 | 337 | | => _tasks.TryGetValue(id, out task); |
| | | 338 | | |
| | | 339 | | /// <summary> |
| | | 340 | | /// Executes the task's work function and updates its state accordingly. |
| | | 341 | | /// </summary> |
| | | 342 | | /// <param name="task">The task to execute.</param> |
| | | 343 | | /// <returns>A task representing the asynchronous operation.</returns> |
| | | 344 | | private async Task ExecuteAsync(KestrunTask task) |
| | | 345 | | { |
| | 12 | 346 | | var cancellationToken = task.Token; |
| | 12 | 347 | | task.State = TaskState.Running; |
| | 12 | 348 | | task.Progress.StatusMessage = "Running"; |
| | 12 | 349 | | task.StartedAtUtc = DateTimeOffset.UtcNow; |
| | | 350 | | try |
| | | 351 | | { |
| | 12 | 352 | | var result = await task.Work(cancellationToken).ConfigureAwait(false); |
| | 10 | 353 | | task.Output = result; |
| | 10 | 354 | | task.State = cancellationToken.IsCancellationRequested ? TaskState.Stopped : TaskState.Completed; |
| | 10 | 355 | | if (task.State == TaskState.Completed) |
| | | 356 | | { |
| | 9 | 357 | | task.Progress.Complete("Completed"); |
| | | 358 | | } |
| | 1 | 359 | | else if (task.State == TaskState.Stopped) |
| | | 360 | | { |
| | | 361 | | // If cancellation was requested but no exception was thrown (graceful exit), normalize progress |
| | 1 | 362 | | task.Progress.Cancel("Cancelled"); |
| | | 363 | | } |
| | 10 | 364 | | } |
| | 2 | 365 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 366 | | { |
| | 2 | 367 | | task.State = TaskState.Stopped; |
| | 2 | 368 | | task.Progress.Cancel("Cancelled"); |
| | 2 | 369 | | } |
| | 0 | 370 | | catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 371 | | { |
| | | 372 | | // Some libraries throw TaskCanceledException instead of OperationCanceledException on cancellation |
| | 0 | 373 | | task.State = TaskState.Stopped; |
| | 0 | 374 | | task.Progress.Cancel("Cancelled"); |
| | 0 | 375 | | } |
| | 0 | 376 | | catch (Exception ex) when (cancellationToken.IsCancellationRequested) |
| | | 377 | | { |
| | | 378 | | // During cancellation, certain engines (e.g., PowerShell) may surface non-cancellation exceptions |
| | | 379 | | // such as PipelineStoppedException. If cancellation was requested, normalize to Stopped. |
| | 0 | 380 | | task.State = TaskState.Stopped; |
| | 0 | 381 | | task.Progress.Cancel("Cancelled"); |
| | 0 | 382 | | _log.Information(ex, "Task {Id} cancelled with exception after cancellation was requested", task.Id); |
| | 0 | 383 | | } |
| | 0 | 384 | | catch (Exception ex) |
| | | 385 | | { |
| | 0 | 386 | | task.Fault = ex; |
| | 0 | 387 | | task.State = TaskState.Failed; |
| | 0 | 388 | | _log.Error(ex, "Task {Id} failed", task.Id); |
| | 0 | 389 | | task.Progress.Fail("Failed"); |
| | 0 | 390 | | } |
| | | 391 | | finally |
| | | 392 | | { |
| | 12 | 393 | | task.CompletedAtUtc = DateTimeOffset.UtcNow; |
| | | 394 | | } |
| | 12 | 395 | | } |
| | | 396 | | |
| | | 397 | | /// <summary> |
| | | 398 | | /// Cancels active tasks, waits briefly for runners to quiesce, disposes quiesced cancellation sources, |
| | | 399 | | /// clears the task registry, and releases the task runspace pool. |
| | | 400 | | /// </summary> |
| | | 401 | | public void Dispose() |
| | | 402 | | { |
| | 2 | 403 | | if (Interlocked.Exchange(ref _disposed, 1) != 0) |
| | | 404 | | { |
| | 0 | 405 | | return; |
| | | 406 | | } |
| | | 407 | | |
| | 2 | 408 | | var tasks = _tasks.Values.ToArray(); |
| | 2 | 409 | | CancelTasks(tasks); |
| | 2 | 410 | | WaitForRunnersToQuiesce(tasks); |
| | 2 | 411 | | DisposeQuiescedCancellationSources(tasks); |
| | | 412 | | |
| | 2 | 413 | | _tasks.Clear(); |
| | 2 | 414 | | TaskRunspacePool.Dispose(); |
| | 2 | 415 | | _log.Information("KestrunTaskService disposed"); |
| | 2 | 416 | | } |
| | | 417 | | |
| | | 418 | | /// <summary> |
| | | 419 | | /// Requests cancellation for each tracked task during service shutdown. |
| | | 420 | | /// </summary> |
| | | 421 | | /// <param name="tasks">The tasks being shut down.</param> |
| | | 422 | | private void CancelTasks(IReadOnlyCollection<KestrunTask> tasks) |
| | | 423 | | { |
| | 6 | 424 | | foreach (var task in tasks) |
| | | 425 | | { |
| | | 426 | | try |
| | | 427 | | { |
| | 1 | 428 | | task.TokenSource.Cancel(); |
| | 1 | 429 | | } |
| | 0 | 430 | | catch (Exception ex) |
| | | 431 | | { |
| | 0 | 432 | | _log.Debug(ex, "Failed to cancel task {Id} during KestrunTaskService disposal", task.Id); |
| | 0 | 433 | | } |
| | | 434 | | } |
| | 2 | 435 | | } |
| | | 436 | | |
| | | 437 | | /// <summary> |
| | | 438 | | /// Waits for active task runners to reach a terminal state within the shutdown timeout. |
| | | 439 | | /// </summary> |
| | | 440 | | /// <param name="tasks">The tasks being shut down.</param> |
| | | 441 | | private void WaitForRunnersToQuiesce(IReadOnlyCollection<KestrunTask> tasks) |
| | | 442 | | { |
| | 2 | 443 | | var activeRunners = tasks |
| | 1 | 444 | | .Where(task => task.Runner is { IsCompleted: false }) |
| | 0 | 445 | | .Select(task => task.Runner!) |
| | 2 | 446 | | .ToArray(); |
| | | 447 | | |
| | 2 | 448 | | if (activeRunners.Length == 0) |
| | | 449 | | { |
| | 2 | 450 | | return; |
| | | 451 | | } |
| | | 452 | | |
| | 0 | 453 | | var allRunnersCompleted = false; |
| | | 454 | | try |
| | | 455 | | { |
| | 0 | 456 | | allRunnersCompleted = Task.WhenAll(activeRunners).Wait(DisposeWaitTimeout); |
| | 0 | 457 | | } |
| | 0 | 458 | | catch (AggregateException ex) |
| | | 459 | | { |
| | 0 | 460 | | _log.Debug(ex, "One or more task runners faulted during KestrunTaskService disposal"); |
| | 0 | 461 | | allRunnersCompleted = true; |
| | 0 | 462 | | } |
| | | 463 | | |
| | 0 | 464 | | if (!allRunnersCompleted) |
| | | 465 | | { |
| | 0 | 466 | | _log.Debug( |
| | 0 | 467 | | "Timed out waiting for {ActiveCount} task runner(s) to stop during KestrunTaskService disposal after {Ti |
| | 0 | 468 | | activeRunners.Length, |
| | 0 | 469 | | DisposeWaitTimeout.TotalMilliseconds); |
| | | 470 | | } |
| | 0 | 471 | | } |
| | | 472 | | |
| | | 473 | | /// <summary> |
| | | 474 | | /// Disposes cancellation token sources for tasks that are no longer executing. |
| | | 475 | | /// </summary> |
| | | 476 | | /// <param name="tasks">The tasks being shut down.</param> |
| | | 477 | | private void DisposeQuiescedCancellationSources(IReadOnlyCollection<KestrunTask> tasks) |
| | | 478 | | { |
| | 6 | 479 | | foreach (var task in tasks) |
| | | 480 | | { |
| | 1 | 481 | | if (task.Runner is { IsCompleted: false }) |
| | | 482 | | { |
| | 0 | 483 | | _log.Debug("Skipping CancellationTokenSource disposal for still-running task {Id}", task.Id); |
| | 0 | 484 | | continue; |
| | | 485 | | } |
| | | 486 | | |
| | | 487 | | try |
| | | 488 | | { |
| | 1 | 489 | | task.TokenSource.Dispose(); |
| | 1 | 490 | | } |
| | 0 | 491 | | catch (Exception ex) |
| | | 492 | | { |
| | 0 | 493 | | _log.Debug(ex, "Failed to dispose CancellationTokenSource for task {Id} during KestrunTaskService dispos |
| | 0 | 494 | | } |
| | | 495 | | } |
| | 2 | 496 | | } |
| | | 497 | | } |