Skip to content

Commit

Permalink
fix: Check for nulls
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Aug 29, 2024
1 parent 883a6e3 commit 3c2aa80
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ All notable changes to **NCronJob** will be documented in this file. The project

## [Unreleased]

### Fixed
- Dispose could still lead to issues when an exception has thrown an exception.

## [2.8.5] - 2024-08-18

### Fixed
Expand Down
7 changes: 7 additions & 0 deletions src/NCronJob/NotNullLinqExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NCronJob;

internal static class NotNullLinqExtensions
{
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source)
where T : class => source.Where(x => x is not null)!;
}
10 changes: 8 additions & 2 deletions src/NCronJob/Scheduler/QueueWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal sealed partial class QueueWorker : BackgroundService
private readonly StartupJobManager startupJobManager;
private readonly ILogger<QueueWorker> logger;
private CancellationTokenSource? shutdown;
private readonly ConcurrentDictionary<string, Task> workerTasks = new();
private readonly ConcurrentDictionary<string, Task?> workerTasks = new();
private readonly ConcurrentDictionary<string, bool> addingWorkerTasks = new();
private volatile bool isDisposed;

Expand Down Expand Up @@ -49,6 +49,11 @@ public override async Task StopAsync(CancellationToken cancellationToken)

foreach (var (jobType, task) in currentTasks)
{
if (task is null)
{
continue;
}

var taskEnded = task.IsCanceled || task.IsFaulted || task.IsCompleted;
if (taskEnded && workerTasks.TryRemove(jobType, out _))
{
Expand Down Expand Up @@ -101,7 +106,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
CreateWorkerQueues(stopToken);
jobQueueManager.QueueAdded += OnQueueAdded; // this needs to come after we create the initial Worker Queues

await Task.WhenAll(workerTasks.Values).ConfigureAwait(false);
var tasks = workerTasks.Values.WhereNotNull();
await Task.WhenAll(tasks).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
Expand Down

0 comments on commit 3c2aa80

Please sign in to comment.