[CHANGE] Reworked ExtendedBackgroundService.cs with State logic and actions

This commit is contained in:
max
2025-10-08 18:22:16 +02:00
parent b8d2573d78
commit 2f19d60be0
7 changed files with 230 additions and 86 deletions

View File

@@ -7,7 +7,7 @@
<PageTitle>Services</PageTitle> <PageTitle>Services</PageTitle>
<MudDataGrid T="ExtendedBackgroundService" Items="@_backgroundServices" Filterable QuickFilter="@QuickFilter"> <MudDataGrid T="ExtendedBackgroundService" Items="@_backgroundServices" Filterable QuickFilter="@QuickFilter" Dense>
<ToolBarContent> <ToolBarContent>
<MudText Typo="Typo.h6">Services</MudText> <MudText Typo="Typo.h6">Services</MudText>
<MudSpacer/> <MudSpacer/>
@@ -20,16 +20,19 @@
<PropertyColumn Property="x => x.Description" Title="Description"/> <PropertyColumn Property="x => x.Description" Title="Description"/>
<PropertyColumn Property="x => x.State" Title="Status"/> <PropertyColumn Property="x => x.State" Title="Status"/>
<PropertyColumn Property="x => x.ExecuteInterval" Title="Execute interval"/> <PropertyColumn Property="x => x.ExecuteInterval" Title="Execute interval"/>
<TemplateColumn> <TemplateColumn Title="Actions">
<CellTemplate> <CellTemplate>
<MudStack Row Spacing="2"> <MudMenu Icon="@Icons.Material.Filled.MoreVert"
<MudButton Disabled="@(context.Item?.State == ServiceState.Paused)" AriaLabel="Actions">
OnClick="@(() => { context.Item?.Pause(); })" Variant="Variant.Outlined">Pause @foreach (var action in context.Item?.Actions ?? [])
</MudButton> {
<MudButton Disabled="@(context.Item?.State == ServiceState.Running)" <MudMenuItem OnClick="@action.Action" Disabled="@(!action.IsEnabled())">
OnClick="@(() => { context.Item?.Resume(); })" Variant="Variant.Outlined">Resume <MudTooltip Text="@action.Description">
</MudButton> <span>@action.Id</span>
</MudStack> </MudTooltip>
</MudMenuItem>
}
</MudMenu>
</CellTemplate> </CellTemplate>
</TemplateColumn> </TemplateColumn>
</Columns> </Columns>

View File

@@ -7,52 +7,91 @@ public abstract class ExtendedBackgroundService(string name, string description,
: BackgroundService : BackgroundService
{ {
private TaskCompletionSource _resumeSignal = new(TaskCreationOptions.RunContinuationsAsynchronously); private TaskCompletionSource _resumeSignal = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly List<ServiceAction> _actions = [];
private TaskCompletionSource? _manualContinue;
public ServiceState State { get; private set; } = ServiceState.Stopped; public ServiceState State { get; private set; } = ServiceState.Stopped;
public CircularBuffer<ServiceEvent> ProgressEvents { get; } = new(500); public CircularBuffer<ServiceEvent> ProgressEvents { get; } = new(500);
public string Name { get; } = name; public string Name { get; } = name;
public string Description { get; set; } = description; public string Description { get; } = description;
public TimeSpan ExecuteInterval { get; set; } = executeInterval ?? TimeSpan.FromMinutes(1); 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) protected sealed override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
State = ServiceState.Running; State = ServiceState.Running;
logger.LogInformation("Initializing background service: {ServiceName}", Name); 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); await InitializeAsync(stoppingToken);
try while (!stoppingToken.IsCancellationRequested)
{ {
logger.LogInformation("Running background service: {ServiceName}", Name); if (State == ServiceState.Running)
while (!stoppingToken.IsCancellationRequested)
{ {
if (State == ServiceState.Paused) try
{ {
_resumeSignal = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); logger.LogInformation("Started running background service: {ServiceName}", Name);
await _resumeSignal.Task.WaitAsync(stoppingToken); while (!stoppingToken.IsCancellationRequested)
} {
if (State == ServiceState.Paused)
{
_resumeSignal = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
await _resumeSignal.Task.WaitAsync(stoppingToken);
}
await ExecuteServiceAsync(stoppingToken); await ExecuteServiceAsync(stoppingToken);
await Task.Delay(ExecuteInterval, 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) _manualContinue = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
{ var delayTask = Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
if (e is not OperationCanceledException) await Task.WhenAny(delayTask, _manualContinue.Task);
{ _manualContinue = null;
State = ServiceState.Faulted;
logger.LogError(e,"Background service {ServiceName} faulted!", Name);
throw;
}
logger.LogInformation(e,"Service {ServiceName} received cancellation", Name);
}
finally
{
State = ServiceState.Stopped;
} }
} }
protected void LogEvent(string message, LogSeverity severity = LogSeverity.Info) => ProgressEvents.Add(new ServiceEvent(string.Intern(Name), message, DateTime.UtcNow, severity)); 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() public void Pause()
{ {
if (State == ServiceState.Running) if (State == ServiceState.Running)
@@ -94,4 +133,6 @@ public enum ServiceState
Paused 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);

View File

@@ -2,6 +2,7 @@ using System.Security.Cryptography;
using System.Text; using System.Text;
using DotBased.Logging; using DotBased.Logging;
using DotBased.Monads; using DotBased.Monads;
using DotBased.Utilities;
using Manager.Data.Contexts; using Manager.Data.Contexts;
using Manager.Data.Entities.Cache; using Manager.Data.Entities.Cache;
using Manager.YouTube; using Manager.YouTube;
@@ -10,23 +11,45 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
namespace Manager.App.Services.System; namespace Manager.App.Services.System;
public class CacheService(ILogger<CacheService> logger, IHostEnvironment environment) : ExtendedBackgroundService(nameof(CacheService), "Manages caching.", logger, TimeSpan.FromHours(5)) public class CacheService(ILogger<CacheService> logger, IHostEnvironment environment)
: ExtendedBackgroundService(nameof(CacheService), "Manages caching.", logger, TimeSpan.FromHours(5))
{ {
private DirectoryInfo? _cacheDirectory; private DirectoryInfo? _cacheDirectory;
private PooledDbContextFactory<CacheDbContext>? _dbContextFactory; private PooledDbContextFactory<CacheDbContext>? _dbContextFactory;
private const string DataSubDir = "data"; private const string DataSubDir = "data";
private const int CacheMaxAgeDays = 1;
private readonly SemaphoreSlim _cacheSemaphoreSlim = new(1, 1);
protected override Task InitializeAsync(CancellationToken stoppingToken) protected override Task InitializeAsync(CancellationToken stoppingToken)
{ {
_cacheDirectory = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "cache")); _cacheDirectory = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "cache"));
_cacheDirectory.Create(); _cacheDirectory.Create();
Directory.CreateDirectory(Path.Combine(_cacheDirectory.FullName, DataSubDir)); Directory.CreateDirectory(Path.Combine(_cacheDirectory.FullName, DataSubDir));
LogEvent($"Cache directory: {_cacheDirectory.FullName}"); LogEvent($"Cache directory: {_cacheDirectory.FullName}");
AddActions([
new ServiceAction("Clear cache", "Manually clear cache", () =>
{
LogEvent("Manual cache clear requested.");
_ = Task.Run(async () =>
{
try
{
await ClearCacheAsync(stoppingToken);
}
catch (Exception e)
{
logger.LogError(e, "Error clearing cache manually!");
LogEvent("Error manually clearing cache.", LogSeverity.Error);
}
}, stoppingToken);
}, () => true)
]);
var dbContextOptionsBuilder = new DbContextOptionsBuilder<CacheDbContext>(); var dbContextOptionsBuilder = new DbContextOptionsBuilder<CacheDbContext>();
dbContextOptionsBuilder.UseSqlite($"Data Source={Path.Combine(_cacheDirectory.FullName, "cache_index.db")}"); dbContextOptionsBuilder.UseSqlite($"Data Source={Path.Combine(_cacheDirectory.FullName, "cache_index.db")}");
_dbContextFactory = new PooledDbContextFactory<CacheDbContext>(dbContextOptionsBuilder.Options); _dbContextFactory = new PooledDbContextFactory<CacheDbContext>(dbContextOptionsBuilder.Options);
return Task.CompletedTask; return Task.CompletedTask;
} }
@@ -72,20 +95,24 @@ public class CacheService(ILogger<CacheService> logger, IHostEnvironment environ
} }
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken); await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var urlKeyBytes = SHA1.HashData(Encoding.UTF8.GetBytes(url)); var urlKeyBytes = SHA1.HashData(Encoding.UTF8.GetBytes(url));
var urlKey = Convert.ToHexString(urlKeyBytes); var urlKey = Convert.ToHexString(urlKeyBytes);
var cacheEntity = await context.Cache.FirstOrDefaultAsync(c => c.Id == urlKey, cancellationToken: cancellationToken); var cacheEntity =
await context.Cache.FirstOrDefaultAsync(c => c.Id == urlKey, cancellationToken: cancellationToken);
if (cacheEntity == null) if (cacheEntity == null)
{ {
var downloadResult = await NetworkService.DownloadBytesAsync(new HttpRequestMessage(HttpMethod.Get, url)); var downloadResult =
await NetworkService.DownloadBytesAsync(new HttpRequestMessage(HttpMethod.Get, url));
if (!downloadResult.IsSuccess) if (!downloadResult.IsSuccess)
{ {
LogEvent($"Failed to download from url: {url}"); LogEvent($"Failed to download from url: {url}");
return ResultError.Fail("Download failed."); return ResultError.Fail("Download failed.");
} }
var download = downloadResult.Value; var download = downloadResult.Value;
await using var downloadFile = File.Create(Path.Combine(_cacheDirectory.FullName, DataSubDir, $"{urlKey}.cache")); await using var downloadFile =
File.Create(Path.Combine(_cacheDirectory.FullName, DataSubDir, $"{urlKey}.cache"));
await downloadFile.WriteAsync(download.Data.AsMemory(0, download.Data.Length), cancellationToken); await downloadFile.WriteAsync(download.Data.AsMemory(0, download.Data.Length), cancellationToken);
cacheEntity = new CacheEntity cacheEntity = new CacheEntity
@@ -96,7 +123,7 @@ public class CacheService(ILogger<CacheService> logger, IHostEnvironment environ
ContentType = download.ContentType, ContentType = download.ContentType,
OriginalFileName = download.FileName, OriginalFileName = download.FileName,
}; };
context.Cache.Add(cacheEntity); context.Cache.Add(cacheEntity);
var saved = await context.SaveChangesAsync(cancellationToken); var saved = await context.SaveChangesAsync(cancellationToken);
if (saved <= 0) if (saved <= 0)
@@ -126,58 +153,71 @@ public class CacheService(ILogger<CacheService> logger, IHostEnvironment environ
private async Task ClearCacheAsync(CancellationToken cancellationToken) private async Task ClearCacheAsync(CancellationToken cancellationToken)
{ {
if (_dbContextFactory == null) if (!await _cacheSemaphoreSlim.WaitAsync(0, cancellationToken))
{ {
throw new InvalidOperationException("No DbContext factory configured."); LogEvent("The cache cleaning task is already running. Skipping this call.", LogSeverity.Warning);
}
if (_cacheDirectory == null)
{
throw new InvalidOperationException("No cache directory configured.");
}
await using var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var toRemove = dbContext.Cache.Where(c => c.CachedAtUtc < DateTime.UtcNow.AddDays(-1));
if (!toRemove.Any())
{
LogEvent("No items found to purge from cache.");
return; return;
} }
var totalToRemove = toRemove.Count(); try
LogEvent($"Found {totalToRemove} cache items that are older than 1 day(s)");
var deleted = new List<CacheEntity>();
foreach (var entity in toRemove)
{ {
var pathToFile = Path.Combine(_cacheDirectory.FullName, DataSubDir, $"{entity.Id}.cache"); if (_dbContextFactory == null)
if (!File.Exists(pathToFile)) throw new InvalidOperationException("No DbContext factory configured.");
if (_cacheDirectory == null)
throw new InvalidOperationException("No cache directory configured.");
await using var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var toRemove = dbContext.Cache.Where(c => c.CachedAtUtc < DateTime.UtcNow.AddDays(-CacheMaxAgeDays));
if (!toRemove.Any())
{ {
LogEvent($"No items older than {CacheMaxAgeDays} day(s) found to clear from cache.");
return;
}
var totalToRemove = toRemove.Count();
LogEvent($"Found {totalToRemove} cache items that are older than 1 day(s)");
var deleted = new List<CacheEntity>();
long totalBytesRemoved = 0;
foreach (var entity in toRemove)
{
var pathToFile = Path.Combine(_cacheDirectory.FullName, DataSubDir, $"{entity.Id}.cache");
if (!File.Exists(pathToFile))
{
deleted.Add(entity);
totalBytesRemoved += entity.ContentLength;
continue;
}
try
{
File.Delete(pathToFile);
}
catch (Exception e)
{
logger.LogError(e, "Failed to delete cache entity with id: {EntityId}. Skipping cache entity...",
entity.Id);
continue;
}
totalBytesRemoved += entity.ContentLength;
deleted.Add(entity); deleted.Add(entity);
continue;
} }
try dbContext.RemoveRange(deleted);
{ var dbDeleted = await dbContext.SaveChangesAsync(cancellationToken);
File.Delete(pathToFile);
} if (dbDeleted < deleted.Count)
catch (Exception e) LogEvent("Could not delete all files from cache.", LogSeverity.Warning);
{
logger.LogError(e, "Failed to delete cache entity with id: {EntityId}. Skipping cache entity...", entity.Id); LogEvent($"Removed {dbDeleted}/{totalToRemove} items from cache. Total of {Suffix.BytesToSizeSuffix(totalBytesRemoved)} removed from disk.");
continue;
}
deleted.Add(entity);
} }
finally
dbContext.RemoveRange(deleted);
var dbDeleted = await dbContext.SaveChangesAsync(cancellationToken);
if (dbDeleted < deleted.Count)
{ {
LogEvent("Could not delete all files from cache.", LogSeverity.Warning); _cacheSemaphoreSlim.Release();
} }
LogEvent($"Removed {dbDeleted}/{totalToRemove} items");
} }
} }

View File

@@ -0,0 +1,16 @@
namespace Manager.App.Services.System;
public class SettingsService(ILogger<SettingsService> logger) : ExtendedBackgroundService(nameof(SettingsService), "Service for handling application settings.", logger, TimeSpan.FromMinutes(10))
{
protected override async Task InitializeAsync(CancellationToken stoppingToken)
{
AddActions([
new ServiceAction("Save settings", "Save the application settings to the database", () => { }, () => true)
]);
}
protected override async Task ExecuteServiceAsync(CancellationToken stoppingToken)
{
}
}

View File

@@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;
namespace Manager.Data.Contexts;
public sealed class ApplicationContext : DbContext
{
public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
{
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
ChangeTracker.LazyLoadingEnabled = false;
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.AddInterceptors(new DateInterceptor());
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}

View File

@@ -0,0 +1,10 @@
namespace Manager.Data.Entities.ApplicationContext;
public class WorkItemEntity : DateTimeBase
{
public required Guid Id { get; set; }
public required string Name { get; set; }
public required string Description { get; set; }
public WorkStatus Status { get; set; } = WorkStatus.Pending;
public required string ClientId { get; set; }
}

View File

@@ -0,0 +1,10 @@
namespace Manager.Data.Entities.ApplicationContext;
public enum WorkStatus
{
Pending = 0,
InProgress = 1,
Paused = 2,
Completed = 3,
Faulted = 4,
}