[CHANGE] Reworked ExtendedBackgroundService.cs with State logic and actions
This commit is contained in:
@@ -7,52 +7,91 @@ public abstract class ExtendedBackgroundService(string name, string description,
|
||||
: BackgroundService
|
||||
{
|
||||
private TaskCompletionSource _resumeSignal = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
private readonly List<ServiceAction> _actions = [];
|
||||
private TaskCompletionSource? _manualContinue;
|
||||
|
||||
public ServiceState State { get; private set; } = ServiceState.Stopped;
|
||||
public CircularBuffer<ServiceEvent> ProgressEvents { get; } = new(500);
|
||||
public string Name { get; } = name;
|
||||
public string Description { get; set; } = description;
|
||||
public TimeSpan ExecuteInterval { get; set; } = executeInterval ?? TimeSpan.FromMinutes(1);
|
||||
public string Description { get; } = description;
|
||||
public TimeSpan ExecuteInterval { get; } = executeInterval ?? TimeSpan.FromSeconds(5);
|
||||
|
||||
public IReadOnlyList<ServiceAction> Actions => _actions;
|
||||
|
||||
protected void AddActions(IEnumerable<ServiceAction> actions)
|
||||
{
|
||||
_actions.AddRange(actions);
|
||||
}
|
||||
|
||||
protected sealed override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
State = ServiceState.Running;
|
||||
logger.LogInformation("Initializing background service: {ServiceName}", Name);
|
||||
|
||||
_actions.AddRange(
|
||||
[
|
||||
new ServiceAction("Start", "Start the service (after the service is stopped of faulted.)", Start, () => State is ServiceState.Stopped or ServiceState.Faulted),
|
||||
new ServiceAction("Pause", "Pause the service", Pause, () => State != ServiceState.Paused),
|
||||
new ServiceAction("Resume", "Resume the service", Resume, () => State != ServiceState.Running)
|
||||
]);
|
||||
|
||||
await InitializeAsync(stoppingToken);
|
||||
|
||||
try
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
logger.LogInformation("Running background service: {ServiceName}", Name);
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
if (State == ServiceState.Running)
|
||||
{
|
||||
if (State == ServiceState.Paused)
|
||||
try
|
||||
{
|
||||
_resumeSignal = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
await _resumeSignal.Task.WaitAsync(stoppingToken);
|
||||
}
|
||||
logger.LogInformation("Started running background service: {ServiceName}", Name);
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
if (State == ServiceState.Paused)
|
||||
{
|
||||
_resumeSignal = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
await _resumeSignal.Task.WaitAsync(stoppingToken);
|
||||
}
|
||||
|
||||
await ExecuteServiceAsync(stoppingToken);
|
||||
|
||||
await Task.Delay(ExecuteInterval, stoppingToken);
|
||||
await ExecuteServiceAsync(stoppingToken);
|
||||
|
||||
await Task.Delay(ExecuteInterval, stoppingToken);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException e)
|
||||
{
|
||||
logger.LogInformation(e, "Service {ServiceName} received cancellation", Name);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
State = ServiceState.Faulted;
|
||||
logger.LogError(e, "Background service {ServiceName} faulted!", Name);
|
||||
LogEvent("Error executing background service.", LogSeverity.Error);
|
||||
}
|
||||
finally
|
||||
{
|
||||
State = ServiceState.Stopped;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (e is not OperationCanceledException)
|
||||
{
|
||||
State = ServiceState.Faulted;
|
||||
logger.LogError(e,"Background service {ServiceName} faulted!", Name);
|
||||
throw;
|
||||
}
|
||||
logger.LogInformation(e,"Service {ServiceName} received cancellation", Name);
|
||||
}
|
||||
finally
|
||||
{
|
||||
State = ServiceState.Stopped;
|
||||
|
||||
_manualContinue = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var delayTask = Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
|
||||
await Task.WhenAny(delayTask, _manualContinue.Task);
|
||||
_manualContinue = null;
|
||||
}
|
||||
}
|
||||
|
||||
protected void LogEvent(string message, LogSeverity severity = LogSeverity.Info) => ProgressEvents.Add(new ServiceEvent(string.Intern(Name), message, DateTime.UtcNow, severity));
|
||||
|
||||
public void Start()
|
||||
{
|
||||
if (State is ServiceState.Stopped or ServiceState.Faulted)
|
||||
{
|
||||
State = ServiceState.Running;
|
||||
_manualContinue?.TrySetResult();
|
||||
LogEvent("Started service.");
|
||||
}
|
||||
}
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
if (State == ServiceState.Running)
|
||||
@@ -94,4 +133,6 @@ public enum ServiceState
|
||||
Paused
|
||||
}
|
||||
|
||||
public record struct ServiceEvent(string Source, string Message, DateTime DateUtc, LogSeverity Severity);
|
||||
public record struct ServiceEvent(string Source, string Message, DateTime DateUtc, LogSeverity Severity);
|
||||
|
||||
public record ServiceAction(string Id, string Description, Action Action, Func<bool> IsEnabled);
|
||||
Reference in New Issue
Block a user