Refactor ScheduledTaskSemaphore (#148)
This commit is contained in:
parent
482f573ecb
commit
605c71f4aa
@ -77,29 +77,23 @@ public class DetectCreditsTask : IScheduledTask
|
|||||||
Entrypoint.CancelAutomaticTask(cancellationToken);
|
Entrypoint.CancelAutomaticTask(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
ScheduledTaskSemaphore.Wait(-1, cancellationToken);
|
using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken))
|
||||||
|
|
||||||
if (cancellationToken.IsCancellationRequested)
|
|
||||||
{
|
{
|
||||||
ScheduledTaskSemaphore.Release();
|
_logger.LogInformation("Scheduled Task is starting");
|
||||||
|
|
||||||
|
Plugin.Instance!.Configuration.PathRestrictions.Clear();
|
||||||
|
var modes = new List<AnalysisMode> { AnalysisMode.Credits };
|
||||||
|
|
||||||
|
var baseCreditAnalyzer = new BaseItemAnalyzerTask(
|
||||||
|
modes.AsReadOnly(),
|
||||||
|
_loggerFactory.CreateLogger<DetectCreditsTask>(),
|
||||||
|
_loggerFactory,
|
||||||
|
_libraryManager);
|
||||||
|
|
||||||
|
baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation("Scheduled Task is starting");
|
|
||||||
|
|
||||||
Plugin.Instance!.Configuration.PathRestrictions.Clear();
|
|
||||||
var modes = new List<AnalysisMode> { AnalysisMode.Credits };
|
|
||||||
|
|
||||||
var baseCreditAnalyzer = new BaseItemAnalyzerTask(
|
|
||||||
modes.AsReadOnly(),
|
|
||||||
_loggerFactory.CreateLogger<DetectCreditsTask>(),
|
|
||||||
_loggerFactory,
|
|
||||||
_libraryManager);
|
|
||||||
|
|
||||||
baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken);
|
|
||||||
|
|
||||||
ScheduledTaskSemaphore.Release();
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -76,29 +76,23 @@ public class DetectIntrosCreditsTask : IScheduledTask
|
|||||||
Entrypoint.CancelAutomaticTask(cancellationToken);
|
Entrypoint.CancelAutomaticTask(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
ScheduledTaskSemaphore.Wait(-1, cancellationToken);
|
using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken))
|
||||||
|
|
||||||
if (cancellationToken.IsCancellationRequested)
|
|
||||||
{
|
{
|
||||||
ScheduledTaskSemaphore.Release();
|
_logger.LogInformation("Scheduled Task is starting");
|
||||||
|
|
||||||
|
Plugin.Instance!.Configuration.PathRestrictions.Clear();
|
||||||
|
var modes = new List<AnalysisMode> { AnalysisMode.Introduction, AnalysisMode.Credits };
|
||||||
|
|
||||||
|
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
|
||||||
|
modes.AsReadOnly(),
|
||||||
|
_loggerFactory.CreateLogger<DetectIntrosCreditsTask>(),
|
||||||
|
_loggerFactory,
|
||||||
|
_libraryManager);
|
||||||
|
|
||||||
|
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation("Scheduled Task is starting");
|
|
||||||
|
|
||||||
Plugin.Instance!.Configuration.PathRestrictions.Clear();
|
|
||||||
var modes = new List<AnalysisMode> { AnalysisMode.Introduction, AnalysisMode.Credits };
|
|
||||||
|
|
||||||
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
|
|
||||||
modes.AsReadOnly(),
|
|
||||||
_loggerFactory.CreateLogger<DetectIntrosCreditsTask>(),
|
|
||||||
_loggerFactory,
|
|
||||||
_libraryManager);
|
|
||||||
|
|
||||||
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
|
|
||||||
|
|
||||||
ScheduledTaskSemaphore.Release();
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -76,29 +76,23 @@ public class DetectIntrosTask : IScheduledTask
|
|||||||
Entrypoint.CancelAutomaticTask(cancellationToken);
|
Entrypoint.CancelAutomaticTask(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
ScheduledTaskSemaphore.Wait(-1, cancellationToken);
|
using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken))
|
||||||
|
|
||||||
if (cancellationToken.IsCancellationRequested)
|
|
||||||
{
|
{
|
||||||
ScheduledTaskSemaphore.Release();
|
_logger.LogInformation("Scheduled Task is starting");
|
||||||
|
|
||||||
|
Plugin.Instance!.Configuration.PathRestrictions.Clear();
|
||||||
|
var modes = new List<AnalysisMode> { AnalysisMode.Introduction };
|
||||||
|
|
||||||
|
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
|
||||||
|
modes.AsReadOnly(),
|
||||||
|
_loggerFactory.CreateLogger<DetectIntrosTask>(),
|
||||||
|
_loggerFactory,
|
||||||
|
_libraryManager);
|
||||||
|
|
||||||
|
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation("Scheduled Task is starting");
|
|
||||||
|
|
||||||
Plugin.Instance!.Configuration.PathRestrictions.Clear();
|
|
||||||
var modes = new List<AnalysisMode> { AnalysisMode.Introduction };
|
|
||||||
|
|
||||||
var baseIntroAnalyzer = new BaseItemAnalyzerTask(
|
|
||||||
modes.AsReadOnly(),
|
|
||||||
_loggerFactory.CreateLogger<DetectIntrosTask>(),
|
|
||||||
_loggerFactory,
|
|
||||||
_libraryManager);
|
|
||||||
|
|
||||||
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
|
|
||||||
|
|
||||||
ScheduledTaskSemaphore.Release();
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -7,20 +7,18 @@ internal sealed class ScheduledTaskSemaphore : IDisposable
|
|||||||
{
|
{
|
||||||
private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
|
private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
|
||||||
|
|
||||||
|
private static bool _isHeld = false;
|
||||||
|
|
||||||
private ScheduledTaskSemaphore()
|
private ScheduledTaskSemaphore()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int CurrentCount => _semaphore.CurrentCount;
|
public static int CurrentCount => _semaphore.CurrentCount;
|
||||||
|
|
||||||
public static bool Wait(int timeout, CancellationToken cancellationToken)
|
public static IDisposable Acquire(int timeout, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return _semaphore.Wait(timeout, cancellationToken);
|
_isHeld = _semaphore.Wait(timeout, cancellationToken);
|
||||||
}
|
return new ScheduledTaskSemaphore();
|
||||||
|
|
||||||
public static int Release()
|
|
||||||
{
|
|
||||||
return _semaphore.Release();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -28,21 +26,10 @@ internal sealed class ScheduledTaskSemaphore : IDisposable
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
Dispose(true);
|
if (_isHeld) // Release only if acquired
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Protected dispose.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="disposing">Dispose.</param>
|
|
||||||
private void Dispose(bool disposing)
|
|
||||||
{
|
|
||||||
if (!disposing)
|
|
||||||
{
|
{
|
||||||
return;
|
_semaphore.Release();
|
||||||
|
_isHeld = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_semaphore.Dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user