Refactor ScheduledTaskSemaphore (#148)

This commit is contained in:
rlauuzo 2024-05-10 06:35:52 +02:00 committed by GitHub
parent 482f573ecb
commit 605c71f4aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 50 additions and 81 deletions

View File

@ -77,14 +77,8 @@ public class DetectCreditsTask : IScheduledTask
Entrypoint.CancelAutomaticTask(cancellationToken); Entrypoint.CancelAutomaticTask(cancellationToken);
} }
ScheduledTaskSemaphore.Wait(-1, cancellationToken); using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken))
if (cancellationToken.IsCancellationRequested)
{ {
ScheduledTaskSemaphore.Release();
return Task.CompletedTask;
}
_logger.LogInformation("Scheduled Task is starting"); _logger.LogInformation("Scheduled Task is starting");
Plugin.Instance!.Configuration.PathRestrictions.Clear(); Plugin.Instance!.Configuration.PathRestrictions.Clear();
@ -98,9 +92,9 @@ public class DetectCreditsTask : IScheduledTask
baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken); baseCreditAnalyzer.AnalyzeItems(progress, cancellationToken);
ScheduledTaskSemaphore.Release();
return Task.CompletedTask; return Task.CompletedTask;
} }
}
/// <summary> /// <summary>
/// Get task triggers. /// Get task triggers.

View File

@ -76,14 +76,8 @@ public class DetectIntrosCreditsTask : IScheduledTask
Entrypoint.CancelAutomaticTask(cancellationToken); Entrypoint.CancelAutomaticTask(cancellationToken);
} }
ScheduledTaskSemaphore.Wait(-1, cancellationToken); using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken))
if (cancellationToken.IsCancellationRequested)
{ {
ScheduledTaskSemaphore.Release();
return Task.CompletedTask;
}
_logger.LogInformation("Scheduled Task is starting"); _logger.LogInformation("Scheduled Task is starting");
Plugin.Instance!.Configuration.PathRestrictions.Clear(); Plugin.Instance!.Configuration.PathRestrictions.Clear();
@ -97,9 +91,9 @@ public class DetectIntrosCreditsTask : IScheduledTask
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken); baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
ScheduledTaskSemaphore.Release();
return Task.CompletedTask; return Task.CompletedTask;
} }
}
/// <summary> /// <summary>
/// Get task triggers. /// Get task triggers.

View File

@ -76,14 +76,8 @@ public class DetectIntrosTask : IScheduledTask
Entrypoint.CancelAutomaticTask(cancellationToken); Entrypoint.CancelAutomaticTask(cancellationToken);
} }
ScheduledTaskSemaphore.Wait(-1, cancellationToken); using (ScheduledTaskSemaphore.Acquire(-1, cancellationToken))
if (cancellationToken.IsCancellationRequested)
{ {
ScheduledTaskSemaphore.Release();
return Task.CompletedTask;
}
_logger.LogInformation("Scheduled Task is starting"); _logger.LogInformation("Scheduled Task is starting");
Plugin.Instance!.Configuration.PathRestrictions.Clear(); Plugin.Instance!.Configuration.PathRestrictions.Clear();
@ -97,9 +91,9 @@ public class DetectIntrosTask : IScheduledTask
baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken); baseIntroAnalyzer.AnalyzeItems(progress, cancellationToken);
ScheduledTaskSemaphore.Release();
return Task.CompletedTask; return Task.CompletedTask;
} }
}
/// <summary> /// <summary>
/// Get task triggers. /// Get task triggers.

View File

@ -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) _semaphore.Release();
{ _isHeld = false;
return;
} }
_semaphore.Dispose();
} }
} }