mirror of
https://github.com/C9Glax/tranga.git
synced 2025-10-17 19:00:45 +02:00
This commit is contained in:
@@ -1,18 +1,27 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using API.Schema.MangaContext;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Workers.PeriodicWorkers.MaintenanceWorkers;
|
||||
|
||||
public class CleanupMangaCoversWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
|
||||
: BaseWorkerWithContext<MangaContext>(dependsOn), IPeriodic
|
||||
: BaseWorkerWithContexts(dependsOn), IPeriodic
|
||||
{
|
||||
public DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
|
||||
public TimeSpan Interval { get; set; } = interval ?? TimeSpan.FromHours(24);
|
||||
|
||||
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
||||
private MangaContext MangaContext = null!;
|
||||
|
||||
protected override void SetContexts(IServiceScope serviceScope)
|
||||
{
|
||||
MangaContext = GetContext<MangaContext>(serviceScope);
|
||||
}
|
||||
|
||||
protected override async Task<BaseWorker[]> DoWorkInternal()
|
||||
{
|
||||
Log.Info("Removing stale files...");
|
||||
string[] usedFiles = await DbContext.Mangas.Where(m => m.CoverFileNameInCache != null).Select(m => m.CoverFileNameInCache!).ToArrayAsync(CancellationToken);
|
||||
string[] usedFiles = await MangaContext.Mangas.Where(m => m.CoverFileNameInCache != null).Select(m => m.CoverFileNameInCache!).ToArrayAsync(CancellationToken);
|
||||
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheOriginal);
|
||||
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheLarge);
|
||||
CleanupImageCache(usedFiles, TrangaSettings.CoverImageCacheMedium);
|
||||
|
@@ -1,29 +1,37 @@
|
||||
using System.Text;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using API.Schema.MangaContext;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Workers.PeriodicWorkers.MaintenanceWorkers;
|
||||
|
||||
public class CleanupMangaconnectorIdsWithoutConnector : BaseWorkerWithContext<MangaContext>
|
||||
public class CleanupMangaconnectorIdsWithoutConnector : BaseWorkerWithContexts
|
||||
{
|
||||
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
||||
private MangaContext MangaContext = null!;
|
||||
|
||||
protected override void SetContexts(IServiceScope serviceScope)
|
||||
{
|
||||
MangaContext = GetContext<MangaContext>(serviceScope);
|
||||
}
|
||||
|
||||
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.All(n => n != chId.MangaConnectorName)).ExecuteDeleteAsync(CancellationToken);
|
||||
int deletedChapterIds = await MangaContext.MangaConnectorToChapter.Where(chId => connectorNames.All(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.All(name => name != mcId.MangaConnectorName)).ToListAsync() is { Count: > 0 } list)
|
||||
if (await MangaContext.MangaConnectorToManga.Include(id => id.Obj) .Where(mcId => connectorNames.All(name => name != mcId.MangaConnectorName)).ToListAsync() is { Count: > 0 } list)
|
||||
{
|
||||
string filePath = Path.Join(TrangaSettings.WorkingDirectory, $"deletedManga-{DateTime.UtcNow.Ticks}.txt");
|
||||
Log.Debug($"Writing deleted manga to {filePath}.");
|
||||
await File.WriteAllLinesAsync(filePath, list.Select(id => string.Join('-', id.MangaConnectorName, id.IdOnConnectorSite, id.Obj.Name, id.WebsiteUrl)), CancellationToken);
|
||||
}
|
||||
int deletedMangaIds = await DbContext.MangaConnectorToManga.Where(mcId => connectorNames.All(name => name != mcId.MangaConnectorName)).ExecuteDeleteAsync(CancellationToken);
|
||||
int deletedMangaIds = await MangaContext.MangaConnectorToManga.Where(mcId => connectorNames.All(name => name != mcId.MangaConnectorName)).ExecuteDeleteAsync(CancellationToken);
|
||||
Log.Info($"Deleted {deletedMangaIds} mangaIds.");
|
||||
|
||||
await DbContext.SaveChangesAsync(CancellationToken);
|
||||
await MangaContext.SaveChangesAsync(CancellationToken);
|
||||
return [];
|
||||
}
|
||||
}
|
@@ -1,3 +1,4 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using API.Schema.NotificationsContext;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -7,18 +8,26 @@ namespace API.Workers.PeriodicWorkers.MaintenanceWorkers;
|
||||
/// Removes sent notifications from database
|
||||
/// </summary>
|
||||
public class RemoveOldNotificationsWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
|
||||
: BaseWorkerWithContext<NotificationsContext>(dependsOn), IPeriodic
|
||||
: BaseWorkerWithContexts(dependsOn), IPeriodic
|
||||
{
|
||||
public DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
|
||||
public TimeSpan Interval { get; set; } = interval ?? TimeSpan.FromHours(1);
|
||||
|
||||
[SuppressMessage("ReSharper", "InconsistentNaming")]
|
||||
private NotificationsContext NotificationsContext = null!;
|
||||
|
||||
protected override void SetContexts(IServiceScope serviceScope)
|
||||
{
|
||||
NotificationsContext = GetContext<NotificationsContext>(serviceScope);
|
||||
}
|
||||
|
||||
protected override async Task<BaseWorker[]> DoWorkInternal()
|
||||
{
|
||||
Log.Debug("Removing old notifications...");
|
||||
int removed = await DbContext.Notifications.Where(n => n.IsSent).ExecuteDeleteAsync(CancellationToken);
|
||||
int removed = await NotificationsContext.Notifications.Where(n => n.IsSent).ExecuteDeleteAsync(CancellationToken);
|
||||
Log.Debug($"Removed {removed} old notifications...");
|
||||
|
||||
if(await DbContext.Sync(CancellationToken, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } e)
|
||||
if(await NotificationsContext.Sync(CancellationToken, GetType(), System.Reflection.MethodBase.GetCurrentMethod()?.Name) is { success: false } e)
|
||||
Log.Error($"Failed to save database changes: {e.exceptionMessage}");
|
||||
|
||||
return [];
|
||||
|
Reference in New Issue
Block a user