Skip to content

Commit

Permalink
When a scan is already running, append it
Browse files Browse the repository at this point in the history
  • Loading branch information
AbandonedCart committed Apr 14, 2024
1 parent b1e94bd commit 3096f3f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
36 changes: 26 additions & 10 deletions ConfusedPolarBear.Plugin.IntroSkipper/Entrypoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Entrypoint : IServerEntryPoint
private bool _analyzeAgain;
private static CancellationTokenSource? _cancellationTokenSource;
private static ManualResetEventSlim _autoTaskCompletEvent = new ManualResetEventSlim(false);
private QueueManager _queueManager;

/// <summary>
/// Initializes a new instance of the <see cref="Entrypoint"/> class.
Expand Down Expand Up @@ -56,6 +57,10 @@ public class Entrypoint : IServerEntryPoint
null,
Timeout.InfiniteTimeSpan,
Timeout.InfiniteTimeSpan);

_queueManager = new QueueManager(
_loggerFactory.CreateLogger<QueueManager>(),
_libraryManager);
}

/// <summary>
Expand Down Expand Up @@ -92,8 +97,7 @@ public Task RunAsync()
{
// Enqueue all episodes at startup to ensure any FFmpeg errors appear as early as possible
_logger.LogInformation("Running startup enqueue");
var queueManager = new QueueManager(_loggerFactory.CreateLogger<QueueManager>(), _libraryManager);
queueManager.GetMediaItems();
_queueManager.GetMediaItems();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -121,7 +125,7 @@ private void OnItemAdded(object? sender, ItemChangeEventArgs itemChangeEventArgs
}

// Don't do anything if it's not a supported media type
if (itemChangeEventArgs.Item is not Episode)
if (itemChangeEventArgs.Item is not Episode episode)
{
return;
}
Expand All @@ -131,9 +135,15 @@ private void OnItemAdded(object? sender, ItemChangeEventArgs itemChangeEventArgs
return;
}

Plugin.Instance!.Configuration.PathRestrictions.Add(itemChangeEventArgs.Item.ContainingFolderPath);

StartTimer();
if (Plugin.Instance!.AnalyzerTaskIsRunning)
{
_queueManager.QueueEpisode(episode);
}
else
{
Plugin.Instance!.Configuration.PathRestrictions.Add(itemChangeEventArgs.Item.ContainingFolderPath);
StartTimer();
}
}

/// <summary>
Expand All @@ -150,7 +160,7 @@ private void OnItemModified(object? sender, ItemChangeEventArgs itemChangeEventA
}

// Don't do anything if it's not a supported media type
if (itemChangeEventArgs.Item is not Episode)
if (itemChangeEventArgs.Item is not Episode episode)
{
return;
}
Expand All @@ -160,9 +170,15 @@ private void OnItemModified(object? sender, ItemChangeEventArgs itemChangeEventA
return;
}

Plugin.Instance!.Configuration.PathRestrictions.Add(itemChangeEventArgs.Item.ContainingFolderPath);

StartTimer();
if (Plugin.Instance!.AnalyzerTaskIsRunning)
{
_queueManager.QueueEpisode(episode);
}
else
{
Plugin.Instance!.Configuration.PathRestrictions.Add(itemChangeEventArgs.Item.ContainingFolderPath);
StartTimer();
}
}

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion ConfusedPolarBear.Plugin.IntroSkipper/QueueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ private void QueueLibraryContents(Guid id)
_logger.LogDebug("Queued {Count} episodes", items.Count);
}

private void QueueEpisode(Episode episode)
/// <summary>
/// Adds a single episode to the current queue for analyzing.
/// </summary>
/// <param name="episode">The episode to analyze.</param>
public void QueueEpisode(Episode episode)
{
if (Plugin.Instance is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellat
{
return Task.CompletedTask;
}
else if (Plugin.Instance!.AnalyzerTaskIsRunning && Entrypoint.AutomaticTaskState == TaskState.Running)
{
_logger.LogInformation("Automatic Task is {0} and will be canceled.", Entrypoint.AutomaticTaskState);
Entrypoint.CancelAutomaticTask();
}

_logger.LogInformation("Scheduled Task is starting");
Plugin.Instance!.AnalyzerTaskIsRunning = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellat
{
return Task.CompletedTask;
}
else if (Plugin.Instance!.AnalyzerTaskIsRunning && Entrypoint.AutomaticTaskState == TaskState.Running)
{
_logger.LogInformation("Automatic Task is {0} and will be canceled.", Entrypoint.AutomaticTaskState);
Entrypoint.CancelAutomaticTask();
}

_logger.LogInformation("Scheduled Task is starting");
Plugin.Instance!.AnalyzerTaskIsRunning = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellat
{
return Task.CompletedTask;
}
else if (Plugin.Instance!.AnalyzerTaskIsRunning && Entrypoint.AutomaticTaskState == TaskState.Running)
{
_logger.LogInformation("Automatic Task is {0} and will be canceled.", Entrypoint.AutomaticTaskState);
Entrypoint.CancelAutomaticTask();
}

_logger.LogInformation("Scheduled Task is starting");
Plugin.Instance!.AnalyzerTaskIsRunning = true;
Expand Down

0 comments on commit 3096f3f

Please sign in to comment.