[CHANGE] Reworked event console
This commit is contained in:
@@ -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);
|
@@ -1,16 +0,0 @@
|
||||
namespace Manager.App.Services.System;
|
||||
|
||||
public class BackgroundServiceManager
|
||||
{
|
||||
private readonly HashSet<ExtendedBackgroundService> _backgroundServices = [];
|
||||
|
||||
public void RegisterService(ExtendedBackgroundService service)
|
||||
{
|
||||
_backgroundServices.Add(service);
|
||||
}
|
||||
|
||||
public List<ExtendedBackgroundService> GetServices()
|
||||
{
|
||||
return _backgroundServices.ToList();
|
||||
}
|
||||
}
|
9
Manager.App/Services/System/BackgroundServiceRegistry.cs
Normal file
9
Manager.App/Services/System/BackgroundServiceRegistry.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Manager.App.Services.System;
|
||||
|
||||
public class BackgroundServiceRegistry(IEnumerable<ExtendedBackgroundService> backgroundServices)
|
||||
{
|
||||
public List<ExtendedBackgroundService> GetServices()
|
||||
{
|
||||
return backgroundServices.ToList();
|
||||
}
|
||||
}
|
@@ -7,8 +7,8 @@ using Manager.YouTube;
|
||||
|
||||
namespace Manager.App.Services.System;
|
||||
|
||||
public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientService> logger, BackgroundServiceManager serviceManager)
|
||||
: ExtendedBackgroundService("ClientService", "Managing YouTube clients", logger, serviceManager, TimeSpan.FromMilliseconds(100))
|
||||
public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientService> logger)
|
||||
: ExtendedBackgroundService("ClientService", "Managing YouTube clients", logger, TimeSpan.FromMilliseconds(100))
|
||||
{
|
||||
private readonly List<YouTubeClient> _clients = [];
|
||||
private CancellationToken _cancellationToken;
|
||||
|
21
Manager.App/Services/System/TestService.cs
Normal file
21
Manager.App/Services/System/TestService.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using DotBased.Logging;
|
||||
|
||||
namespace Manager.App.Services.System;
|
||||
|
||||
public class TestService(ILogger<TestService> logger) : ExtendedBackgroundService("TestService", "Development service", logger, TimeSpan.FromMilliseconds(100))
|
||||
{
|
||||
protected override Task InitializeAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
protected override Task ExecuteServiceAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
LogEvent("TestService");
|
||||
LogEvent($"Error {Guid.NewGuid()}", LogSeverity.Error);
|
||||
LogEvent("Something went wrong!", LogSeverity.Warning);
|
||||
LogEvent("Tracing.", LogSeverity.Trace);
|
||||
LogEvent("Fatal error!", LogSeverity.Fatal);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user