[CHANGE] BackgroundServices

This commit is contained in:
max
2025-09-09 19:11:02 +02:00
parent d183803390
commit 2593d02a73
7 changed files with 188 additions and 17 deletions

View File

@@ -0,0 +1,23 @@
using Manager.App.Models.System;
namespace Manager.App.Services.System;
public class BackgroundServiceManager
{
private readonly HashSet<ExtendedBackgroundService> _backgroundServices = [];
public void RegisterService(ExtendedBackgroundService service)
{
_backgroundServices.Add(service);
}
public ListResult<ExtendedBackgroundService> GetServices(string serviceName, int total = 20, int skip = 0)
{
var filtered = string.IsNullOrWhiteSpace(serviceName) ? _backgroundServices.ToArray() : _backgroundServices.Where(x => x.Name.Equals(serviceName, StringComparison.OrdinalIgnoreCase)).ToArray();
var results = filtered.OrderDescending()
.Skip(skip)
.Take(total);
return new ListResultReturn<ExtendedBackgroundService>(results.ToList(), filtered.Length);
}
}

View File

@@ -6,19 +6,19 @@ using Manager.YouTube;
namespace Manager.App.Services.System;
public class ClientManager(IServiceScopeFactory scopeFactory, HostedServiceConnector serviceConnector) : BackgroundService
public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientService> logger, BackgroundServiceManager serviceManager) : ExtendedBackgroundService("ClientService", logger, serviceManager)
{
private readonly List<YouTubeClient> _clients = [];
private CancellationToken _cancellationToken;
private ILibraryService? _libraryService;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
protected override async Task ExecuteServiceAsync(CancellationToken stoppingToken)
{
serviceConnector.RegisterService(this);
_cancellationToken = stoppingToken;
stoppingToken.Register(CancellationRequested);
using var scope = scopeFactory.CreateScope();
_libraryService = scope.ServiceProvider.GetRequiredService<ILibraryService>();
LogProgress("Initializing service...");
}
private void CancellationRequested()

View File

@@ -1,11 +0,0 @@
namespace Manager.App.Services.System;
public class HostedServiceConnector
{
private readonly List<IHostedService> _hostedServices = [];
public void RegisterService(IHostedService service)
{
_hostedServices.Add(service);
}
}