Add CleanupMangaconnectorIdsWithoutConnector.cs

This commit is contained in:
2025-09-18 01:14:33 +02:00
parent 55d6b46507
commit 07f95e953d
2 changed files with 47 additions and 6 deletions

View File

@@ -16,13 +16,14 @@ namespace API;
public static class Tranga
{
private static IServiceProvider? ServiceProvider;
private static IServiceProvider? _serviceProvider;
private static readonly ILog Log = LogManager.GetLogger(typeof(Tranga));
internal static readonly MetadataFetcher[] MetadataFetchers = [new MyAnimeList()];
internal static readonly MangaConnector[] MangaConnectors = [new Global(), new MangaDex(), new Mangaworld()];
internal static TrangaSettings Settings = TrangaSettings.Load();
// ReSharper disable MemberCanBePrivate.Global
internal static readonly UpdateMetadataWorker UpdateMetadataWorker = new ();
internal static readonly SendNotificationsWorker SendNotificationsWorker = new();
internal static readonly UpdateChaptersDownloadedWorker UpdateChaptersDownloadedWorker = new();
@@ -32,6 +33,8 @@ public static class Tranga
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
internal static void StartLogger(FileInfo loggerConfigFile)
{
@@ -51,11 +54,12 @@ public static class Tranga
AddWorker(RemoveOldNotificationsWorker);
AddWorker(UpdateCoversWorker);
AddWorker(UpdateLibraryConnectorsWorker);
AddWorker(CleanupMangaconnectorIdsWithoutConnector);
}
internal static void SetServiceProvider(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
_serviceProvider = serviceProvider;
}
internal static bool TryGetMangaConnector(string name, [NotNullWhen(true)]out MangaConnector? mangaConnector)
@@ -112,7 +116,7 @@ public static class Tranga
internal static void StartWorker(BaseWorker worker, Action? callback = null)
{
Log.Debug($"Starting {worker}");
if (ServiceProvider is null)
if (_serviceProvider is null)
{
Log.Fatal("ServiceProvider is null");
return;
@@ -127,15 +131,15 @@ public static class Tranga
if (worker is BaseWorkerWithContext<MangaContext> mangaContextWorker)
{
mangaContextWorker.SetScope(ServiceProvider.CreateScope());
mangaContextWorker.SetScope(_serviceProvider.CreateScope());
RunningWorkers.TryAdd(mangaContextWorker, mangaContextWorker.DoWork(afterWorkCallback));
}else if (worker is BaseWorkerWithContext<NotificationsContext> notificationContextWorker)
{
notificationContextWorker.SetScope(ServiceProvider.CreateScope());
notificationContextWorker.SetScope(_serviceProvider.CreateScope());
RunningWorkers.TryAdd(notificationContextWorker, notificationContextWorker.DoWork(afterWorkCallback));
}else if (worker is BaseWorkerWithContext<LibraryContext> libraryContextWorker)
{
libraryContextWorker.SetScope(ServiceProvider.CreateScope());
libraryContextWorker.SetScope(_serviceProvider.CreateScope());
RunningWorkers.TryAdd(libraryContextWorker, libraryContextWorker.DoWork(afterWorkCallback));
}else
RunningWorkers.TryAdd(worker, worker.DoWork(afterWorkCallback));

View File

@@ -0,0 +1,37 @@
using System.Text;
using API.Schema.MangaContext;
using Microsoft.EntityFrameworkCore;
namespace API.Workers.PeriodicWorkers.MaintenanceWorkers;
public class CleanupMangaconnectorIdsWithoutConnector : BaseWorkerWithContext<MangaContext>
{
protected override async Task<BaseWorker[]> DoWorkInternal()
{
Log.Info("Cleaning up old connector-data");
string[] connectorNames = Tranga.MangaConnectors.Select(c => c.Name).ToArray();
int deletedChapterIds = await DbContext.MangaConnectorToChapter.Where(chId => connectorNames.Any(n => n == chId.MangaConnectorName)).ExecuteDeleteAsync(CancellationToken);
Log.Info($"Deleted {deletedChapterIds} chapterIds.");
// Manga without Connector get printed to file, to not lose data...
if (await DbContext.MangaConnectorToManga.Include(id => id.Obj)
.Where(mcId => connectorNames.Any(name => name == mcId.MangaConnectorName)).ToListAsync() is
{ Count: > 0 } list)
{
string filePath = Path.Join(TrangaSettings.settingsFilePath, $"deletedManga-{DateTime.UtcNow.Ticks}.txt");
Log.Debug($"Writing deleted manga to {filePath}.");
await File.WriteAllLinesAsync(filePath, list.Select(id =>
{
StringBuilder sb = new();
sb.Append(id.MangaConnectorName);
sb.AppendJoin('-', id.IdOnConnectorSite);
sb.AppendJoin('-', id.Obj.Name);
sb.AppendJoin('-', id.WebsiteUrl);
return sb.ToString();
}), CancellationToken);
}
int deletedMangaIds = await DbContext.MangaConnectorToManga.Where(mcId => connectorNames.Any(name => name == mcId.MangaConnectorName)).ExecuteDeleteAsync(CancellationToken);
Log.Info($"Deleted {deletedMangaIds} mangaIds.");
return [];
}
}