mirror of
https://github.com/C9Glax/tranga.git
synced 2025-10-11 05:09:49 +02:00
Implement #445, Startup tasks
This commit is contained in:
@@ -151,6 +151,7 @@ using (IServiceScope scope = app.Services.CreateScope())
|
|||||||
|
|
||||||
Tranga.ServiceProvider = app.Services;
|
Tranga.ServiceProvider = app.Services;
|
||||||
Tranga.StartLogger(new FileInfo("Log4Net.config.xml"));
|
Tranga.StartLogger(new FileInfo("Log4Net.config.xml"));
|
||||||
|
Tranga.StartupTasks();
|
||||||
Tranga.AddDefaultWorkers();
|
Tranga.AddDefaultWorkers();
|
||||||
|
|
||||||
app.UseCors("AllowAll");
|
app.UseCors("AllowAll");
|
||||||
|
@@ -32,7 +32,6 @@ public static class Tranga
|
|||||||
internal static readonly StartNewChapterDownloadsWorker StartNewChapterDownloadsWorker = new();
|
internal static readonly StartNewChapterDownloadsWorker StartNewChapterDownloadsWorker = new();
|
||||||
internal static readonly RemoveOldNotificationsWorker RemoveOldNotificationsWorker = new();
|
internal static readonly RemoveOldNotificationsWorker RemoveOldNotificationsWorker = new();
|
||||||
internal static readonly UpdateCoversWorker UpdateCoversWorker = new();
|
internal static readonly UpdateCoversWorker UpdateCoversWorker = new();
|
||||||
internal static readonly UpdateLibraryConnectorsWorker UpdateLibraryConnectorsWorker = new();
|
|
||||||
internal static readonly CleanupMangaconnectorIdsWithoutConnector CleanupMangaconnectorIdsWithoutConnector = new();
|
internal static readonly CleanupMangaconnectorIdsWithoutConnector CleanupMangaconnectorIdsWithoutConnector = new();
|
||||||
// ReSharper restore MemberCanBePrivate.Global
|
// ReSharper restore MemberCanBePrivate.Global
|
||||||
|
|
||||||
@@ -43,18 +42,25 @@ public static class Tranga
|
|||||||
Log.Info(Constants.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()
|
internal static void AddDefaultWorkers()
|
||||||
{
|
{
|
||||||
AddWorker(UpdateMetadataWorker);
|
AddWorker(UpdateMetadataWorker);
|
||||||
AddWorker(SendNotificationsWorker);
|
|
||||||
AddWorker(UpdateChaptersDownloadedWorker);
|
|
||||||
AddWorker(CheckForNewChaptersWorker);
|
AddWorker(CheckForNewChaptersWorker);
|
||||||
AddWorker(CleanupMangaCoversWorker);
|
|
||||||
AddWorker(StartNewChapterDownloadsWorker);
|
AddWorker(StartNewChapterDownloadsWorker);
|
||||||
AddWorker(RemoveOldNotificationsWorker);
|
AddWorker(RemoveOldNotificationsWorker);
|
||||||
AddWorker(UpdateCoversWorker);
|
AddWorker(UpdateCoversWorker);
|
||||||
AddWorker(UpdateLibraryConnectorsWorker);
|
|
||||||
AddWorker(CleanupMangaconnectorIdsWithoutConnector);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static bool TryGetMangaConnector(string name, [NotNullWhen(true)]out MangaConnector? mangaConnector)
|
internal static bool TryGetMangaConnector(string name, [NotNullWhen(true)]out MangaConnector? mangaConnector)
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
using API.Schema.MangaContext;
|
using API.Schema.MangaContext;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace API.Workers.PeriodicWorkers.MaintenanceWorkers;
|
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 DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
|
||||||
public TimeSpan Interval { get; set; } = interval ?? TimeSpan.FromHours(24);
|
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...");
|
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.CoverImageCacheOriginal);
|
||||||
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheLarge);
|
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheLarge);
|
||||||
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheMedium);
|
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheMedium);
|
||||||
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheSmall);
|
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheSmall);
|
||||||
return new Task<BaseWorker[]>(() => []);
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CleanupImageCache(string[] retainFilenames, string imageCachePath)
|
private void CleanupImageCache(string[] retainFilenames, string imageCachePath)
|
||||||
|
@@ -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 [];
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user