Merge Scheduled Tasks

This commit is contained in:
rlauu 2024-03-30 15:36:15 +01:00 committed by TwistedUmbrellaX
parent 0b870c7db7
commit 3446c180aa
2 changed files with 28 additions and 171 deletions

View File

@ -1,127 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace ConfusedPolarBear.Plugin.IntroSkipper;
/// <summary>
/// Analyze all television episodes for credits.
/// TODO: analyze all media files.
/// </summary>
public class DetectCreditsTask : IScheduledTask
{
private readonly ILogger<DetectCreditsTask> _logger;
private readonly ILoggerFactory _loggerFactory;
private readonly ILibraryManager _libraryManager;
/// <summary>
/// Initializes a new instance of the <see cref="DetectCreditsTask"/> class.
/// </summary>
/// <param name="loggerFactory">Logger factory.</param>
/// <param name="libraryManager">Library manager.</param>
/// <param name="logger">Logger.</param>
public DetectCreditsTask(
ILogger<DetectCreditsTask> logger,
ILoggerFactory loggerFactory,
ILibraryManager libraryManager)
{
_logger = logger;
_loggerFactory = loggerFactory;
_libraryManager = libraryManager;
}
/// <summary>
/// Gets the task name.
/// </summary>
public string Name => "Detect Credits";
/// <summary>
/// Gets the task category.
/// </summary>
public string Category => "Intro Skipper";
/// <summary>
/// Gets the task description.
/// </summary>
public string Description => "Analyzes the audio and video of all television episodes to find credits.";
/// <summary>
/// Gets the task key.
/// </summary>
public string Key => "CPBIntroSkipperDetectCredits";
/// <summary>
/// Analyze all episodes in the queue. Only one instance of this task should be run at a time.
/// </summary>
/// <param name="progress">Task progress.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Task.</returns>
public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
if (_libraryManager is null)
{
throw new InvalidOperationException("Library manager was null");
}
// Wait for running analyzer
if (Plugin.Instance!.AnalyzerTaskIsRunning)
{
_logger.LogInformation("Other running Analyzer Task detected. Wait...");
using (var timer = new Timer(_ => { }, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan))
{
try
{
void DisposeTimerOnCancellation() => timer.Dispose();
cancellationToken.Register(DisposeTimerOnCancellation);
while (Plugin.Instance!.AnalyzerTaskIsRunning && !cancellationToken.IsCancellationRequested)
{
timer.Change(TimeSpan.FromMilliseconds(20000), Timeout.InfiniteTimeSpan); // Adjust delay
}
}
catch (OperationCanceledException)
{
return Task.CompletedTask;
}
}
if (!cancellationToken.IsCancellationRequested) // Check cancellation again before logging
{
_logger.LogInformation("No other Task active. Run Analyzer Task");
}
else
{
_logger.LogInformation("Task was canceled");
return Task.CompletedTask;
}
}
Plugin.Instance!.AnalyzerTaskIsRunning = true;
var baseAnalyzer = new BaseItemAnalyzerTask(
AnalysisMode.Credits,
_loggerFactory.CreateLogger<DetectCreditsTask>(),
_loggerFactory,
_libraryManager);
baseAnalyzer.AnalyzeItems(progress, cancellationToken);
Plugin.Instance!.AnalyzerTaskIsRunning = false;
return Task.CompletedTask;
}
/// <summary>
/// Get task triggers.
/// </summary>
/// <returns>Task triggers.</returns>
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
return Array.Empty<TaskTriggerInfo>();
}
}

View File

@ -11,26 +11,21 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper;
/// <summary> /// <summary>
/// Analyze all television episodes for introduction sequences. /// Analyze all television episodes for introduction sequences.
/// </summary> /// </summary>
public class DetectIntroductionsTask : IScheduledTask public class DetectIntrosAndCreditsTask : IScheduledTask
{ {
private readonly ILogger<DetectIntroductionsTask> _logger;
private readonly ILoggerFactory _loggerFactory; private readonly ILoggerFactory _loggerFactory;
private readonly ILibraryManager _libraryManager; private readonly ILibraryManager _libraryManager;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DetectIntroductionsTask"/> class. /// Initializes a new instance of the <see cref="DetectIntrosAndCreditsTask"/> class.
/// </summary> /// </summary>
/// <param name="loggerFactory">Logger factory.</param> /// <param name="loggerFactory">Logger factory.</param>
/// <param name="libraryManager">Library manager.</param> /// <param name="libraryManager">Library manager.</param>
/// <param name="logger">Logger.</param> public DetectIntrosAndCreditsTask(
public DetectIntroductionsTask(
ILogger<DetectIntroductionsTask> logger,
ILoggerFactory loggerFactory, ILoggerFactory loggerFactory,
ILibraryManager libraryManager) ILibraryManager libraryManager)
{ {
_logger = logger;
_loggerFactory = loggerFactory; _loggerFactory = loggerFactory;
_libraryManager = libraryManager; _libraryManager = libraryManager;
} }
@ -38,7 +33,7 @@ public class DetectIntroductionsTask : IScheduledTask
/// <summary> /// <summary>
/// Gets the task name. /// Gets the task name.
/// </summary> /// </summary>
public string Name => "Detect Introductions"; public string Name => "Detect Introductions and Credits";
/// <summary> /// <summary>
/// Gets the task category. /// Gets the task category.
@ -48,12 +43,12 @@ public class DetectIntroductionsTask : IScheduledTask
/// <summary> /// <summary>
/// Gets the task description. /// Gets the task description.
/// </summary> /// </summary>
public string Description => "Analyzes the audio of all television episodes to find introduction sequences."; public string Description => "Analyzes the audio of all television episodes to find introduction and credit sequences.";
/// <summary> /// <summary>
/// Gets the task key. /// Gets the task key.
/// </summary> /// </summary>
public string Key => "CPBIntroSkipperDetectIntroductions"; public string Key => "CPBIntroSkipperDetectIntrosAndCredits";
/// <summary> /// <summary>
/// Analyze all episodes in the queue. Only one instance of this task should be run at a time. /// Analyze all episodes in the queue. Only one instance of this task should be run at a time.
@ -68,47 +63,36 @@ public class DetectIntroductionsTask : IScheduledTask
throw new InvalidOperationException("Library manager was null"); throw new InvalidOperationException("Library manager was null");
} }
// Wait for running analyzer // abort if analyzer is already running
if (Plugin.Instance!.AnalyzerTaskIsRunning) if (Plugin.Instance!.AnalyzerTaskIsRunning)
{ {
_logger.LogInformation("Other running Analyzer Task detected. Wait..."); return Task.CompletedTask;
using (var timer = new Timer(_ => { }, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan)) }
{ else
try {
{ Plugin.Instance!.AnalyzerTaskIsRunning = true;
void DisposeTimerOnCancellation() => timer.Dispose();
cancellationToken.Register(DisposeTimerOnCancellation);
while (Plugin.Instance!.AnalyzerTaskIsRunning && !cancellationToken.IsCancellationRequested)
{
timer.Change(TimeSpan.FromMilliseconds(20000), Timeout.InfiniteTimeSpan); // Adjust delay
}
}
catch (OperationCanceledException)
{
return Task.CompletedTask;
}
}
if (!cancellationToken.IsCancellationRequested) // Check cancellation again before logging
{
_logger.LogInformation("No other Task active. Run Analyzer Task");
}
else
{
_logger.LogInformation("Task was canceled");
return Task.CompletedTask;
}
} }
Plugin.Instance!.AnalyzerTaskIsRunning = true; // intro
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
var baseAnalyzer = new BaseItemAnalyzerTask(
AnalysisMode.Introduction, AnalysisMode.Introduction,
_loggerFactory.CreateLogger<DetectIntroductionsTask>(), _loggerFactory.CreateLogger<DetectIntrosAndCreditsTask>(),
_loggerFactory, _loggerFactory,
_libraryManager); _libraryManager);
baseAnalyzer.AnalyzeItems(progress, cancellationToken); baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
// reset progress
progress.Report(0);
// outro
var baseCreditAnalyzer = new BaseItemAnalyzerTask(
AnalysisMode.Credits,
_loggerFactory.CreateLogger<DetectIntrosAndCreditsTask>(),
_loggerFactory,
_libraryManager);
baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken);
Plugin.Instance!.AnalyzerTaskIsRunning = false; Plugin.Instance!.AnalyzerTaskIsRunning = false;