handle disposed token (#163)

Co-authored-by: rlauu <46294892+rlauu@users.noreply.github.com>
This commit is contained in:
rlauuzo 2024-05-16 19:22:22 +02:00 committed by GitHub
parent 48bfec8d32
commit fa22495be7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,8 +23,10 @@ public class Entrypoint : IHostedService, IDisposable
private readonly ILibraryManager _libraryManager;
private readonly ILogger<Entrypoint> _logger;
private readonly ILoggerFactory _loggerFactory;
private readonly object _pathRestrictionsLock = new();
private Timer _queueTimer;
private bool _analyzeAgain;
private List<string> _pathRestrictions = new List<string>();
private static CancellationTokenSource? _cancellationTokenSource;
private static ManualResetEventSlim _autoTaskCompletEvent = new ManualResetEventSlim(false);
@ -148,7 +150,10 @@ public class Entrypoint : IHostedService, IDisposable
return;
}
Plugin.Instance.Configuration.PathRestrictions.Add(itemChangeEventArgs.Item.ContainingFolderPath);
lock (_pathRestrictionsLock)
{
_pathRestrictions.Add(itemChangeEventArgs.Item.ContainingFolderPath);
}
StartTimer();
}
@ -177,7 +182,10 @@ public class Entrypoint : IHostedService, IDisposable
return;
}
Plugin.Instance.Configuration.PathRestrictions.Add(itemChangeEventArgs.Item.ContainingFolderPath);
lock (_pathRestrictionsLock)
{
_pathRestrictions.Add(itemChangeEventArgs.Item.ContainingFolderPath);
}
StartTimer();
}
@ -223,11 +231,11 @@ public class Entrypoint : IHostedService, IDisposable
{
if (AutomaticTaskState == TaskState.Running)
{
_analyzeAgain = true; // Items added during a scan will be included later.
_analyzeAgain = true;
}
else if (ScheduledTaskSemaphore.CurrentCount > 0)
else if (AutomaticTaskState == TaskState.Idle)
{
_logger.LogInformation("Media Library changed, analyzis will start soon!");
_logger.LogDebug("Media Library changed, analyzis will start soon!");
_queueTimer.Change(TimeSpan.FromMilliseconds(20000), Timeout.InfiniteTimeSpan);
}
}
@ -245,6 +253,11 @@ public class Entrypoint : IHostedService, IDisposable
{
_logger.LogError(ex, "Error in PerformAnalysis");
}
// Clean up
Plugin.Instance!.Configuration.PathRestrictions.Clear();
_cancellationTokenSource = null;
_autoTaskCompletEvent.Set();
}
/// <summary>
@ -252,14 +265,24 @@ public class Entrypoint : IHostedService, IDisposable
/// </summary>
private void PerformAnalysis()
{
_logger.LogInformation("Timer elapsed - start analyzing");
_logger.LogInformation("Initiate automatic analysis task.");
_autoTaskCompletEvent.Reset();
using (_cancellationTokenSource = new CancellationTokenSource())
using (ScheduledTaskSemaphore.Acquire(-1, _cancellationTokenSource.Token))
{
var progress = new Progress<double>();
var cancellationToken = _cancellationTokenSource.Token;
lock (_pathRestrictionsLock)
{
foreach (var path in _pathRestrictions)
{
Plugin.Instance!.Configuration.PathRestrictions.Add(path);
}
_pathRestrictions.Clear();
}
_analyzeAgain = false;
var progress = new Progress<double>();
var modes = new List<AnalysisMode>();
var tasklogger = _loggerFactory.CreateLogger("DefaultLogger");
@ -286,21 +309,16 @@ public class Entrypoint : IHostedService, IDisposable
_loggerFactory,
_libraryManager);
baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken);
}
Plugin.Instance.Configuration.PathRestrictions.Clear();
_autoTaskCompletEvent.Set();
_cancellationTokenSource = null;
baseCreditAnalyzer.AnalyzeItems(progress, _cancellationTokenSource.Token);
// New item detected, start timer again
if (_analyzeAgain)
if (_analyzeAgain && !_cancellationTokenSource.IsCancellationRequested)
{
_logger.LogInformation("Analyzing ended, but we need to analyze again!");
_analyzeAgain = false;
StartTimer();
}
}
}
/// <summary>
/// Method to cancel the automatic task.
@ -308,16 +326,20 @@ public class Entrypoint : IHostedService, IDisposable
/// <param name="cancellationToken">Cancellation token.</param>
public static void CancelAutomaticTask(CancellationToken cancellationToken)
{
if (_cancellationTokenSource != null)
if (_cancellationTokenSource != null && !_cancellationTokenSource.IsCancellationRequested)
{
if (!_cancellationTokenSource.IsCancellationRequested)
try
{
_cancellationTokenSource.Cancel();
}
catch (ObjectDisposedException)
{
_cancellationTokenSource = null;
}
}
_autoTaskCompletEvent.Wait(TimeSpan.FromSeconds(60), cancellationToken); // Wait for the signal
}
}
/// <summary>
/// Dispose.