Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce cognitive complexity outlined by SonarCloud #157

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.x
- name: Restore dependencies
Expand Down
177 changes: 96 additions & 81 deletions Axuno.BackgroundTask/ConcurrentBackgroundQueueService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public ConcurrentBackgroundQueueService(IBackgroundQueue taskQueue,
/// <returns>A <see cref="Task"/>.</returns>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
lock (_locker)
{
_resetEvent.WaitOne();
}
await WaitForStartSignal();

var taskListReference = new Queue<IBackgroundTask>();

Expand All @@ -59,103 +56,121 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
stoppingToken.ThrowIfCancellationRequested();
await Task.Delay(Config.PollQueueDelay, stoppingToken);

if (_concurrentTaskCount < Config.MaxConcurrentCount && TaskQueue?.Count > 0)
{
Interlocked.Increment(ref _concurrentTaskCount);
_logger.LogDebug("Num of tasks: {concurrentTaskCount}", _concurrentTaskCount);
taskListReference.Enqueue(TaskQueue.DequeueTask());
}
else
{
var taskChunk = new List<Task>();
while (taskListReference.Count > 0)
{
try
{
stoppingToken.ThrowIfCancellationRequested();

// The service shall only be cancelled when the app shuts down
using (var taskCancellation = new CancellationTokenSource())
using (var combinedCancellation =
CancellationTokenSource.CreateLinkedTokenSource(stoppingToken,
taskCancellation.Token))
{
var t = TaskQueue?.RunTaskAsync(taskListReference.Dequeue(),
combinedCancellation.Token);
if (t is null)
throw new NullReferenceException($"{nameof(TaskQueue)} cannot be null here.");
if (t.Exception != null) throw t.Exception;
taskChunk.Add(t);
}

stoppingToken.ThrowIfCancellationRequested();
}
catch (Exception e)
{
_logger.LogError(e, "Error occurred executing TaskItem.");
}
finally
{
Interlocked.Decrement(ref _concurrentTaskCount);
}
}

if (taskChunk.Count == 0) continue;

// Task.WhenAll will not throw all exceptions when it encounters them.
// Instead, it adds them to an AggregateException, that must be
// checked at the end of waiting for the tasks to complete
Task? allTasks = null;
try
{
allTasks = Task.WhenAll(taskChunk);
// re-throws an AggregateException if one exists
// after waiting for the tasks to complete
await allTasks.WaitAsync(stoppingToken);
}
catch (Exception e)
{
_logger.LogError(e, "Task chunk exception");
if (allTasks?.Exception != null)
{
_logger.LogError(allTasks.Exception, "Task chunk aggregate exception");
}
}
finally
{
taskListReference.Clear();
}
}
EnqueuePendingTasks(taskListReference);
await ExecuteTaskChunk(taskListReference, stoppingToken);
}
}
catch (Exception e) when (e is TaskCanceledException)
catch (TaskCanceledException ex)
{
_logger.LogError(e, $"{nameof(ConcurrentBackgroundQueueService)} was canceled.");
_logger.LogError(ex, "{Service} was canceled.", nameof(ConcurrentBackgroundQueueService));
}
catch(Exception e)
catch (Exception ex)
{
_logger.LogError(e, $"{nameof(ConcurrentBackgroundQueueService)} failed.");
TaskQueue = null; // we can't process the queue any more
_logger.LogError(ex, "{Service} failed.", nameof(ConcurrentBackgroundQueueService));
TaskQueue = null; // we can't process the queue anymore
}
finally
{
lock (_locker)
SignalServiceStopped();
}
}

private Task WaitForStartSignal()
{
lock (_locker)
{
_resetEvent.WaitOne();
}

return Task.CompletedTask;
}

private void EnqueuePendingTasks(Queue<IBackgroundTask> taskListReference)
{
if (_concurrentTaskCount >= Config.MaxConcurrentCount || TaskQueue == null || TaskQueue.Count == 0)
return;

Interlocked.Increment(ref _concurrentTaskCount);
_logger.LogDebug("Num of tasks: {ConcurrentTaskCount}", _concurrentTaskCount);

taskListReference.Enqueue(TaskQueue.DequeueTask());
}

private async Task ExecuteTaskChunk(Queue<IBackgroundTask> taskListReference, CancellationToken stoppingToken)
{
if (taskListReference.Count == 0)
return;

var taskChunk = new List<Task>();

try
{
while (taskListReference.Count > 0)
{
_resetEvent.Reset();
_resetEvent.Set();
stoppingToken.ThrowIfCancellationRequested();

using var taskCancellation = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
var task = (TaskQueue?.RunTaskAsync(taskListReference.Dequeue(), taskCancellation.Token)) ??
throw new NullReferenceException($"{nameof(TaskQueue)} cannot be null here.");

if (task.Exception != null)
throw task.Exception;

taskChunk.Add(task);
}

await ExecuteTaskChunkAndWait(taskChunk);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occurred executing TaskItem.");
}
finally
{
Interlocked.Decrement(ref _concurrentTaskCount);
taskListReference.Clear();
}
}

private async Task ExecuteTaskChunkAndWait(List<Task> taskChunk)
{
if (taskChunk.Count == 0)
return;

try
{
await Task.WhenAll(taskChunk);
}
catch (Exception ex)
{
_logger.LogError(ex, "Task chunk exception");
var taskChunkExceptions = taskChunk.Where(task => task.Exception != null).Select(task => task.Exception!).ToList();
if (taskChunkExceptions.Count > 0)
{
_logger.LogError(new AggregateException(taskChunkExceptions), "Task chunk aggregate exception");
}
}
}

private void SignalServiceStopped()
{
lock (_locker)
{
_resetEvent.Reset();
_resetEvent.Set();
}
}


/// <summary>
/// Stops the service.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogDebug($"{nameof(ConcurrentBackgroundQueueService)} is stopping.");
_logger.LogDebug("{Service} is stopping.", nameof(ConcurrentBackgroundQueueService));
await base.StopAsync(cancellationToken);
_logger.LogDebug($"{nameof(ConcurrentBackgroundQueueService)} stopped.");
_logger.LogDebug("{Service} stopped.", nameof(ConcurrentBackgroundQueueService));
}
}
17 changes: 8 additions & 9 deletions Axuno.Tools/FileSystem/DelayedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ public DelayedEvent(FileSystemEventArgs args)
/// </summary>
public FileSystemEventArgs Args { get; }

public virtual bool IsDuplicate(object obj)
public virtual bool IsDuplicate(object? obj)
{
if (!(obj is DelayedEvent delayedEvent))
if (obj is not DelayedEvent delayedEvent)
return false;

var allEventArgs = Args;
var renamedEventArgs = Args as RenamedEventArgs;

var allDelayedEventArgs = delayedEvent.Args;
Expand All @@ -37,16 +36,16 @@ public virtual bool IsDuplicate(object obj)
// We also eliminate Changed events that follow recent Created events
// because many apps create new files by creating an empty file and then
// update the file with the file content.
return (allEventArgs.ChangeType == allDelayedEventArgs.ChangeType
&& allEventArgs.FullPath == allDelayedEventArgs.FullPath &&
allEventArgs.Name == allDelayedEventArgs.Name) &&
return (Args.ChangeType == allDelayedEventArgs.ChangeType
&& Args.FullPath == allDelayedEventArgs.FullPath &&
Args.Name == allDelayedEventArgs.Name) &&
((renamedEventArgs == null && delayedRenamedEventArgs == null) || (renamedEventArgs != null &&
delayedRenamedEventArgs != null &&
renamedEventArgs.OldFullPath == delayedRenamedEventArgs.OldFullPath &&
renamedEventArgs.OldName == delayedRenamedEventArgs.OldName)) ||
(allEventArgs.ChangeType == WatcherChangeTypes.Created
(Args.ChangeType == WatcherChangeTypes.Created
&& allDelayedEventArgs.ChangeType == WatcherChangeTypes.Changed
&& allEventArgs.FullPath == allDelayedEventArgs.FullPath &&
allEventArgs.Name == allDelayedEventArgs.Name);
&& Args.FullPath == allDelayedEventArgs.FullPath &&
Args.Name == allDelayedEventArgs.Name);
}
}
Loading