Merge pull request #114 from rlauuzo/master

Add cancellation of automatic tasks from scheduled tasks
This commit is contained in:
TwistedUmbrellaX 2024-04-13 10:54:55 -04:00 committed by GitHub
commit bbf96ce5a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 108 additions and 44 deletions

View File

@ -25,6 +25,8 @@ public class Entrypoint : IHostedService, IDisposable
private readonly ILoggerFactory _loggerFactory;
private Timer _queueTimer;
private bool _analyzeAgain;
private static CancellationTokenSource? _cancellationTokenSource;
private static ManualResetEventSlim _autoTaskCompletEvent = new ManualResetEventSlim(false);
/// <summary>
/// Initializes a new instance of the <see cref="Entrypoint"/> class.
@ -57,6 +59,24 @@ public class Entrypoint : IHostedService, IDisposable
Timeout.InfiniteTimeSpan);
}
/// <summary>
/// Gets State of the automatic task.
/// </summary>
public static TaskState AutomaticTaskState
{
get
{
if (_cancellationTokenSource is not null)
{
return _cancellationTokenSource.IsCancellationRequested
? TaskState.Cancelling
: TaskState.Running;
}
return TaskState.Idle;
}
}
/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken)
{
@ -91,6 +111,12 @@ public class Entrypoint : IHostedService, IDisposable
// Stop the timer
_queueTimer.Change(Timeout.Infinite, 0);
if (_cancellationTokenSource != null) // Null Check
{
_cancellationTokenSource.Dispose();
_cancellationTokenSource = null;
}
return Task.CompletedTask;
}
@ -219,8 +245,10 @@ public class Entrypoint : IHostedService, IDisposable
_logger.LogInformation("Timer elapsed - start analyzing");
Plugin.Instance!.AnalyzerTaskIsRunning = true;
using (_cancellationTokenSource = new CancellationTokenSource())
{
var progress = new Progress<double>();
var cancellationToken = new CancellationToken(false);
var cancellationToken = _cancellationTokenSource.Token;
if (Plugin.Instance!.Configuration.AutoDetectIntros && Plugin.Instance!.Configuration.AutoDetectCredits)
{
@ -261,8 +289,10 @@ public class Entrypoint : IHostedService, IDisposable
baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken);
}
}
Plugin.Instance!.AnalyzerTaskIsRunning = false;
_autoTaskCompletEvent.Set();
// New item detected, start timer again
if (_analyzeAgain)
@ -273,6 +303,23 @@ public class Entrypoint : IHostedService, IDisposable
}
}
/// <summary>
/// Method to cancel the automatic task.
/// </summary>
public static void CancelAutomaticTask()
{
if (_cancellationTokenSource != null)
{
_cancellationTokenSource.Cancel();
_autoTaskCompletEvent.Wait(); // Wait for the signal
_autoTaskCompletEvent.Reset(); // Reset for the next task
_cancellationTokenSource.Dispose(); // Now safe to dispose
_cancellationTokenSource = null;
}
}
/// <summary>
/// Dispose.
/// </summary>

View File

@ -70,15 +70,19 @@ public class DetectCreditsTask : IScheduledTask
}
// abort if analyzer is already running
if (Plugin.Instance!.AnalyzerTaskIsRunning)
if (Plugin.Instance!.AnalyzerTaskIsRunning && Entrypoint.AutomaticTaskState == TaskState.Idle)
{
return Task.CompletedTask;
}
else
else if (Plugin.Instance!.AnalyzerTaskIsRunning && Entrypoint.AutomaticTaskState == TaskState.Running)
{
Plugin.Instance!.AnalyzerTaskIsRunning = true;
_logger.LogInformation("Automatic Task is {0} and will be canceled.", Entrypoint.AutomaticTaskState);
Entrypoint.CancelAutomaticTask();
}
_logger.LogInformation("Scheduled Task is starting");
Plugin.Instance!.AnalyzerTaskIsRunning = true;
var baseCreditAnalyzer = new BaseItemAnalyzerTask(
AnalysisMode.Credits,
_loggerFactory.CreateLogger<DetectCreditsTask>(),

View File

@ -13,6 +13,8 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper;
/// </summary>
public class DetectIntrosCreditsTask : IScheduledTask
{
private readonly ILogger<DetectIntrosCreditsTask> _logger;
private readonly ILoggerFactory _loggerFactory;
private readonly ILibraryManager _libraryManager;
@ -22,10 +24,13 @@ public class DetectIntrosCreditsTask : IScheduledTask
/// </summary>
/// <param name="loggerFactory">Logger factory.</param>
/// <param name="libraryManager">Library manager.</param>
/// <param name="logger">Logger.</param>
public DetectIntrosCreditsTask(
ILogger<DetectIntrosCreditsTask> logger,
ILoggerFactory loggerFactory,
ILibraryManager libraryManager)
{
_logger = logger;
_loggerFactory = loggerFactory;
_libraryManager = libraryManager;
}
@ -64,15 +69,19 @@ public class DetectIntrosCreditsTask : IScheduledTask
}
// abort if analyzer is already running
if (Plugin.Instance!.AnalyzerTaskIsRunning)
if (Plugin.Instance!.AnalyzerTaskIsRunning && Entrypoint.AutomaticTaskState == TaskState.Idle)
{
return Task.CompletedTask;
}
else
else if (Plugin.Instance!.AnalyzerTaskIsRunning && Entrypoint.AutomaticTaskState == TaskState.Running)
{
Plugin.Instance!.AnalyzerTaskIsRunning = true;
_logger.LogInformation("Automatic Task is {0} and will be canceled.", Entrypoint.AutomaticTaskState);
Entrypoint.CancelAutomaticTask();
}
_logger.LogInformation("Scheduled Task is starting");
Plugin.Instance!.AnalyzerTaskIsRunning = true;
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
AnalysisMode.Introduction,
_loggerFactory.CreateLogger<DetectIntrosCreditsTask>(),

View File

@ -69,15 +69,19 @@ public class DetectIntrosTask : IScheduledTask
}
// abort if analyzer is already running
if (Plugin.Instance!.AnalyzerTaskIsRunning)
if (Plugin.Instance!.AnalyzerTaskIsRunning && Entrypoint.AutomaticTaskState == TaskState.Idle)
{
return Task.CompletedTask;
}
else
else if (Plugin.Instance!.AnalyzerTaskIsRunning && Entrypoint.AutomaticTaskState == TaskState.Running)
{
Plugin.Instance!.AnalyzerTaskIsRunning = true;
_logger.LogInformation("Automatic Task is {0} and will be canceled.", Entrypoint.AutomaticTaskState);
Entrypoint.CancelAutomaticTask();
}
_logger.LogInformation("Scheduled Task is starting");
Plugin.Instance!.AnalyzerTaskIsRunning = true;
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
AnalysisMode.Introduction,
_loggerFactory.CreateLogger<DetectIntrosTask>(),