Merge pull request #114 from rlauuzo/master
Add cancellation of automatic tasks from scheduled tasks
This commit is contained in:
commit
bbf96ce5a7
@ -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,50 +245,54 @@ public class Entrypoint : IHostedService, IDisposable
|
||||
_logger.LogInformation("Timer elapsed - start analyzing");
|
||||
Plugin.Instance!.AnalyzerTaskIsRunning = true;
|
||||
|
||||
var progress = new Progress<double>();
|
||||
var cancellationToken = new CancellationToken(false);
|
||||
|
||||
if (Plugin.Instance!.Configuration.AutoDetectIntros && Plugin.Instance!.Configuration.AutoDetectCredits)
|
||||
using (_cancellationTokenSource = new CancellationTokenSource())
|
||||
{
|
||||
// This is where we can optimize a single scan
|
||||
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
|
||||
AnalysisMode.Introduction,
|
||||
_loggerFactory.CreateLogger<DetectIntrosCreditsTask>(),
|
||||
_loggerFactory,
|
||||
_libraryManager);
|
||||
var progress = new Progress<double>();
|
||||
var cancellationToken = _cancellationTokenSource.Token;
|
||||
|
||||
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||
if (Plugin.Instance!.Configuration.AutoDetectIntros && Plugin.Instance!.Configuration.AutoDetectCredits)
|
||||
{
|
||||
// This is where we can optimize a single scan
|
||||
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
|
||||
AnalysisMode.Introduction,
|
||||
_loggerFactory.CreateLogger<DetectIntrosCreditsTask>(),
|
||||
_loggerFactory,
|
||||
_libraryManager);
|
||||
|
||||
var baseCreditAnalyzer = new BaseItemAnalyzerTask(
|
||||
AnalysisMode.Credits,
|
||||
_loggerFactory.CreateLogger<DetectIntrosCreditsTask>(),
|
||||
_loggerFactory,
|
||||
_libraryManager);
|
||||
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||
|
||||
baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||
}
|
||||
else if (Plugin.Instance!.Configuration.AutoDetectIntros)
|
||||
{
|
||||
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
|
||||
AnalysisMode.Introduction,
|
||||
_loggerFactory.CreateLogger<DetectIntrosTask>(),
|
||||
_loggerFactory,
|
||||
_libraryManager);
|
||||
var baseCreditAnalyzer = new BaseItemAnalyzerTask(
|
||||
AnalysisMode.Credits,
|
||||
_loggerFactory.CreateLogger<DetectIntrosCreditsTask>(),
|
||||
_loggerFactory,
|
||||
_libraryManager);
|
||||
|
||||
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||
}
|
||||
else if (Plugin.Instance!.Configuration.AutoDetectCredits)
|
||||
{
|
||||
var baseCreditAnalyzer = new BaseItemAnalyzerTask(
|
||||
AnalysisMode.Credits,
|
||||
_loggerFactory.CreateLogger<DetectCreditsTask>(),
|
||||
_loggerFactory,
|
||||
_libraryManager);
|
||||
baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||
}
|
||||
else if (Plugin.Instance!.Configuration.AutoDetectIntros)
|
||||
{
|
||||
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
|
||||
AnalysisMode.Introduction,
|
||||
_loggerFactory.CreateLogger<DetectIntrosTask>(),
|
||||
_loggerFactory,
|
||||
_libraryManager);
|
||||
|
||||
baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||
}
|
||||
else if (Plugin.Instance!.Configuration.AutoDetectCredits)
|
||||
{
|
||||
var baseCreditAnalyzer = new BaseItemAnalyzerTask(
|
||||
AnalysisMode.Credits,
|
||||
_loggerFactory.CreateLogger<DetectCreditsTask>(),
|
||||
_loggerFactory,
|
||||
_libraryManager);
|
||||
|
||||
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>
|
||||
|
@ -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>(),
|
||||
|
@ -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>(),
|
||||
|
@ -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>(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user