Implement #445, Startup tasks

This commit is contained in:
2025-09-21 18:09:37 +02:00
parent 3a21517a18
commit 5c29496fa5
4 changed files with 17 additions and 29 deletions

View File

@@ -151,6 +151,7 @@ using (IServiceScope scope = app.Services.CreateScope())
Tranga.ServiceProvider = app.Services;
Tranga.StartLogger(new FileInfo("Log4Net.config.xml"));
Tranga.StartupTasks();
Tranga.AddDefaultWorkers();
app.UseCors("AllowAll");

View File

@@ -32,7 +32,6 @@ public static class Tranga
internal static readonly StartNewChapterDownloadsWorker StartNewChapterDownloadsWorker = new();
internal static readonly RemoveOldNotificationsWorker RemoveOldNotificationsWorker = new();
internal static readonly UpdateCoversWorker UpdateCoversWorker = new();
internal static readonly UpdateLibraryConnectorsWorker UpdateLibraryConnectorsWorker = new();
internal static readonly CleanupMangaconnectorIdsWithoutConnector CleanupMangaconnectorIdsWithoutConnector = new();
// ReSharper restore MemberCanBePrivate.Global
@@ -43,18 +42,25 @@ public static class Tranga
Log.Info(Constants.TRANGA);
}
internal static void StartupTasks()
{
AddWorker(SendNotificationsWorker);
AddWorker(CleanupMangaconnectorIdsWithoutConnector);
AddWorker(UpdateChaptersDownloadedWorker);
AddWorker(CleanupMangaCoversWorker);
Log.Info("Waiting for startup to complete...");
while (new List<BaseWorker>() { CleanupMangaconnectorIdsWithoutConnector, UpdateChaptersDownloadedWorker, CleanupMangaCoversWorker}.Any(w => w.State < WorkerExecutionState.Completed))
Thread.Sleep(100);
Log.Info("Start complete!");
}
internal static void AddDefaultWorkers()
{
AddWorker(UpdateMetadataWorker);
AddWorker(SendNotificationsWorker);
AddWorker(UpdateChaptersDownloadedWorker);
AddWorker(CheckForNewChaptersWorker);
AddWorker(CleanupMangaCoversWorker);
AddWorker(StartNewChapterDownloadsWorker);
AddWorker(RemoveOldNotificationsWorker);
AddWorker(UpdateCoversWorker);
AddWorker(UpdateLibraryConnectorsWorker);
AddWorker(CleanupMangaconnectorIdsWithoutConnector);
}
internal static bool TryGetMangaConnector(string name, [NotNullWhen(true)]out MangaConnector? mangaConnector)

View File

@@ -1,4 +1,5 @@
using API.Schema.MangaContext;
using Microsoft.EntityFrameworkCore;
namespace API.Workers.PeriodicWorkers.MaintenanceWorkers;
@@ -8,15 +9,15 @@ public class CleanupMangaCoversWorker(TimeSpan? interval = null, IEnumerable<Bas
public DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
public TimeSpan Interval { get; set; } = interval ?? TimeSpan.FromHours(24);
protected override Task<BaseWorker[]> DoWorkInternal()
protected override async Task<BaseWorker[]> DoWorkInternal()
{
Log.Info("Removing stale files...");
string[] usedFiles = DbContext.Mangas.Select(m => m.CoverFileNameInCache).Where(s => s != null).ToArray()!;
string[] usedFiles = await DbContext.Mangas.Where(m => m.CoverFileNameInCache != null).Select(m => m.CoverFileNameInCache!).ToArrayAsync(CancellationToken);
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheOriginal);
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheLarge);
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheMedium);
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheSmall);
return new Task<BaseWorker[]>(() => []);
return [];
}
private void CleanupImageCache(string[] retainFilenames, string imageCachePath)

View File

@@ -1,20 +0,0 @@
using API.Schema.LibraryContext;
using API.Schema.LibraryContext.LibraryConnectors;
using Microsoft.EntityFrameworkCore;
namespace API.Workers.PeriodicWorkers;
public class UpdateLibraryConnectorsWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
: BaseWorkerWithContext<LibraryContext>(dependsOn), IPeriodic
{
public DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
public TimeSpan Interval { get; set; } = interval ?? TimeSpan.FromMinutes(10);
protected override async Task<BaseWorker[]> DoWorkInternal()
{
List<LibraryConnector> connectors = await DbContext.LibraryConnectors.ToListAsync(CancellationToken);
foreach (LibraryConnector libraryConnector in connectors)
await libraryConnector.UpdateLibrary(CancellationToken);
return [];
}
}