[CHANGE] Reworked event console

This commit is contained in:
max
2025-09-10 18:19:36 +02:00
parent ef6ca0ee07
commit 9be6f5be89
16 changed files with 326 additions and 131 deletions

View File

@@ -1,37 +1,27 @@
using DotBased.Logging;
using Manager.App.Services.System;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace Manager.App.Services;
public abstract class ExtendedBackgroundService : BackgroundService
public abstract class ExtendedBackgroundService(string name, string description, ILogger logger, TimeSpan? executeInterval = null)
: BackgroundService
{
private TaskCompletionSource _resumeSignal = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly ILogger _logger;
public ServiceState State { get; private set; } = ServiceState.Stopped;
public CircularBuffer<ServiceEvent> ProgressEvents { get; } = new(500);
public string Name { get; }
public string Description { get; set; }
public TimeSpan ExecuteInterval { get; set; }
public string Name { get; } = name;
public string Description { get; set; } = description;
public TimeSpan ExecuteInterval { get; set; } = executeInterval ?? TimeSpan.FromMinutes(1);
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.FromMinutes(1);
}
protected sealed override async Task ExecuteAsync(CancellationToken stoppingToken)
{
State = ServiceState.Running;
_logger.LogInformation("Initializing background service: {ServiceName}", Name);
logger.LogInformation("Initializing background service: {ServiceName}", Name);
await InitializeAsync(stoppingToken);
try
{
_logger.LogInformation("Running background service: {ServiceName}", Name);
logger.LogInformation("Running background service: {ServiceName}", Name);
while (!stoppingToken.IsCancellationRequested)
{
if (State == ServiceState.Paused)
@@ -49,10 +39,10 @@ public abstract class ExtendedBackgroundService : BackgroundService
if (e is not OperationCanceledException)
{
State = ServiceState.Faulted;
_logger.LogError(e,"Background service {ServiceName} faulted!", Name);
logger.LogError(e,"Background service {ServiceName} faulted!", Name);
throw;
}
_logger.LogInformation(e,"Service {ServiceName} received cancellation", Name);
logger.LogInformation(e,"Service {ServiceName} received cancellation", Name);
}
finally
{
@@ -67,7 +57,7 @@ public abstract class ExtendedBackgroundService : BackgroundService
if (State == ServiceState.Running)
{
State = ServiceState.Paused;
_logger.LogInformation("Pauses service: {ServiceName}", Name);
logger.LogInformation("Pauses service: {ServiceName}", Name);
}
}
@@ -77,7 +67,7 @@ public abstract class ExtendedBackgroundService : BackgroundService
{
State = ServiceState.Running;
_resumeSignal.TrySetResult();
_logger.LogInformation("Resumed service: {ServiceName}", Name);
logger.LogInformation("Resumed service: {ServiceName}", Name);
}
}
@@ -103,4 +93,4 @@ public enum ServiceState
Paused
}
public record struct ServiceEvent(string Source, string Message, DateTime Date, LogSeverity Severity);
public record struct ServiceEvent(string Source, string Message, DateTime DateUtc, LogSeverity Severity);