[CHANGE] Service extended and events

This commit is contained in:
max
2025-09-10 01:46:07 +02:00
parent 9ff4fcded2
commit 03631cd0c8
9 changed files with 202 additions and 26 deletions

View File

@@ -9,16 +9,18 @@ public abstract class ExtendedBackgroundService : BackgroundService
private TaskCompletionSource _resumeSignal = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly ILogger _logger;
public ServiceState State { get; private set; } = ServiceState.Stopped;
public CircularBuffer<ServiceProgress> ProgressLog { get; } = new(500);
public CircularBuffer<ServiceEvent> ProgressEvents { get; } = new(500);
public string Name { get; }
public string Description { get; set; }
public TimeSpan ExecuteInterval { get; set; }
public ExtendedBackgroundService(string name, ILogger logger, BackgroundServiceManager manager, TimeSpan? executeInterval = null)
public ExtendedBackgroundService(string name, string description, ILogger logger, BackgroundServiceManager manager, TimeSpan? executeInterval = null)
{
Name = name;
Description = description;
_logger = logger;
manager.RegisterService(this);
ExecuteInterval = executeInterval ?? TimeSpan.Zero;
ExecuteInterval = executeInterval ?? TimeSpan.FromMinutes(1);
}
protected sealed override async Task ExecuteAsync(CancellationToken stoppingToken)
@@ -58,7 +60,7 @@ public abstract class ExtendedBackgroundService : BackgroundService
}
}
protected void LogProgress(string message, LogSeverity severity = LogSeverity.Info) => ProgressLog.Add(new ServiceProgress(message, DateTime.UtcNow, severity));
protected void LogEvent(string message, LogSeverity severity = LogSeverity.Info) => ProgressEvents.Add(new ServiceEvent(Name, message, DateTime.UtcNow, severity));
public void Pause()
{
@@ -101,4 +103,4 @@ public enum ServiceState
Paused
}
public record ServiceProgress(string Message, DateTime StartTime, LogSeverity Severity);
public record ServiceEvent(string Source, string Message, DateTime Date, LogSeverity Severity);