[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

@@ -2,6 +2,7 @@ using System.Security.Cryptography;
using System.Text;
using DotBased.Logging;
using DotBased.Monads;
using DotBased.Utilities;
using Manager.Data.Contexts;
using Manager.Data.Entities.Cache;
using Manager.YouTube;
@@ -10,23 +11,45 @@ using Microsoft.EntityFrameworkCore.Infrastructure;
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 PooledDbContextFactory<CacheDbContext>? _dbContextFactory;
private const string DataSubDir = "data";
private const int CacheMaxAgeDays = 1;
private readonly SemaphoreSlim _cacheSemaphoreSlim = new(1, 1);
protected override Task InitializeAsync(CancellationToken stoppingToken)
{
_cacheDirectory = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "cache"));
_cacheDirectory.Create();
Directory.CreateDirectory(Path.Combine(_cacheDirectory.FullName, DataSubDir));
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>();
dbContextOptionsBuilder.UseSqlite($"Data Source={Path.Combine(_cacheDirectory.FullName, "cache_index.db")}");
_dbContextFactory = new PooledDbContextFactory<CacheDbContext>(dbContextOptionsBuilder.Options);
return Task.CompletedTask;
}
@@ -72,20 +95,24 @@ public class CacheService(ILogger<CacheService> logger, IHostEnvironment environ
}
await using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
var urlKeyBytes = SHA1.HashData(Encoding.UTF8.GetBytes(url));
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)
{
var downloadResult = await NetworkService.DownloadBytesAsync(new HttpRequestMessage(HttpMethod.Get, url));
var downloadResult =
await NetworkService.DownloadBytesAsync(new HttpRequestMessage(HttpMethod.Get, url));
if (!downloadResult.IsSuccess)
{
LogEvent($"Failed to download from url: {url}");
return ResultError.Fail("Download failed.");
}
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);
cacheEntity = new CacheEntity
@@ -96,7 +123,7 @@ public class CacheService(ILogger<CacheService> logger, IHostEnvironment environ
ContentType = download.ContentType,
OriginalFileName = download.FileName,
};
context.Cache.Add(cacheEntity);
var saved = await context.SaveChangesAsync(cancellationToken);
if (saved <= 0)
@@ -126,58 +153,71 @@ public class CacheService(ILogger<CacheService> logger, IHostEnvironment environ
private async Task ClearCacheAsync(CancellationToken cancellationToken)
{
if (_dbContextFactory == null)
if (!await _cacheSemaphoreSlim.WaitAsync(0, cancellationToken))
{
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(-1));
if (!toRemove.Any())
{
LogEvent("No items found to purge from cache.");
LogEvent("The cache cleaning task is already running. Skipping this call.", LogSeverity.Warning);
return;
}
var totalToRemove = toRemove.Count();
LogEvent($"Found {totalToRemove} cache items that are older than 1 day(s)");
var deleted = new List<CacheEntity>();
foreach (var entity in toRemove)
try
{
var pathToFile = Path.Combine(_cacheDirectory.FullName, DataSubDir, $"{entity.Id}.cache");
if (!File.Exists(pathToFile))
if (_dbContextFactory == null)
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);
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;
}
deleted.Add(entity);
dbContext.RemoveRange(deleted);
var dbDeleted = await dbContext.SaveChangesAsync(cancellationToken);
if (dbDeleted < deleted.Count)
LogEvent("Could not delete all files from cache.", LogSeverity.Warning);
LogEvent($"Removed {dbDeleted}/{totalToRemove} items from cache. Total of {Suffix.BytesToSizeSuffix(totalBytesRemoved)} removed from disk.");
}
dbContext.RemoveRange(deleted);
var dbDeleted = await dbContext.SaveChangesAsync(cancellationToken);
if (dbDeleted < deleted.Count)
finally
{
LogEvent("Could not delete all files from cache.", LogSeverity.Warning);
_cacheSemaphoreSlim.Release();
}
LogEvent($"Removed {dbDeleted}/{totalToRemove} items");
}
}