clean up semaphore

This commit is contained in:
rlauu 2024-09-23 19:45:16 +02:00
parent ad9d36ebd6
commit 9ff742013b
5 changed files with 7 additions and 18 deletions

View File

@ -78,7 +78,7 @@ public class DetectCreditsTask : IScheduledTask
Entrypoint.CancelAutomaticTask(cancellationToken); Entrypoint.CancelAutomaticTask(cancellationToken);
} }
using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken)) using (ScheduledTaskSemaphore.Acquire(cancellationToken))
{ {
_logger.LogInformation("Scheduled Task is starting"); _logger.LogInformation("Scheduled Task is starting");

View File

@ -77,7 +77,7 @@ public class DetectIntrosCreditsTask : IScheduledTask
Entrypoint.CancelAutomaticTask(cancellationToken); Entrypoint.CancelAutomaticTask(cancellationToken);
} }
using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken)) using (ScheduledTaskSemaphore.Acquire(cancellationToken))
{ {
_logger.LogInformation("Scheduled Task is starting"); _logger.LogInformation("Scheduled Task is starting");

View File

@ -77,7 +77,7 @@ public class DetectIntrosTask : IScheduledTask
Entrypoint.CancelAutomaticTask(cancellationToken); Entrypoint.CancelAutomaticTask(cancellationToken);
} }
using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken)) using (ScheduledTaskSemaphore.Acquire(cancellationToken))
{ {
_logger.LogInformation("Scheduled Task is starting"); _logger.LogInformation("Scheduled Task is starting");

View File

@ -7,29 +7,18 @@ internal sealed class ScheduledTaskSemaphore : IDisposable
{ {
private static readonly SemaphoreSlim _semaphore = new(1, 1); private static readonly SemaphoreSlim _semaphore = new(1, 1);
private static bool _isHeld;
private ScheduledTaskSemaphore() private ScheduledTaskSemaphore()
{ {
} }
public static int CurrentCount => _semaphore.CurrentCount; public static IDisposable Acquire(CancellationToken cancellationToken)
public static IDisposable Acquire(int timeout, CancellationToken cancellationToken)
{ {
_isHeld = _semaphore.Wait(timeout, cancellationToken); _semaphore.Wait(cancellationToken);
return new ScheduledTaskSemaphore(); return new ScheduledTaskSemaphore();
} }
/// <summary>
/// Dispose.
/// </summary>
public void Dispose() public void Dispose()
{ {
if (_isHeld) // Release only if acquired _semaphore.Release();
{
_semaphore.Release();
_isHeld = false;
}
} }
} }

View File

@ -247,7 +247,7 @@ public sealed class Entrypoint : IHostedService, IDisposable
_autoTaskCompletEvent.Reset(); _autoTaskCompletEvent.Reset();
using (_cancellationTokenSource = new CancellationTokenSource()) using (_cancellationTokenSource = new CancellationTokenSource())
using (ScheduledTaskSemaphore.Acquire(-1, _cancellationTokenSource.Token)) using (ScheduledTaskSemaphore.Acquire(_cancellationTokenSource.Token))
{ {
var seasonIds = new HashSet<Guid>(_seasonsToAnalyze); var seasonIds = new HashSet<Guid>(_seasonsToAnalyze);
_seasonsToAnalyze.Clear(); _seasonsToAnalyze.Clear();