mirror of
https://github.com/C9Glax/tranga.git
synced 2025-09-10 20:08:19 +02:00
Improved CancellationToken usage, Added more logging to Workers, Added Code-comments (please future me, be thankful)
This commit is contained in:
@@ -3,23 +3,28 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Workers;
|
||||
|
||||
/// <summary>
|
||||
/// Creates Jobs to update available Chapters for all Manga that are marked for Download
|
||||
/// </summary>
|
||||
public class CheckForNewChaptersWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
|
||||
: BaseWorkerWithContext<MangaContext>(dependsOn), IPeriodic
|
||||
{
|
||||
public DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
|
||||
public TimeSpan Interval { get; set; } = interval??TimeSpan.FromMinutes(60);
|
||||
|
||||
protected override Task<BaseWorker[]> DoWorkInternal()
|
||||
protected override async Task<BaseWorker[]> DoWorkInternal()
|
||||
{
|
||||
IQueryable<MangaConnectorId<Manga>> connectorIdsManga = DbContext.MangaConnectorToManga
|
||||
Log.Debug("Checking for new chapters...");
|
||||
List<MangaConnectorId<Manga>> connectorIdsManga = await DbContext.MangaConnectorToManga
|
||||
.Include(id => id.Obj)
|
||||
.Where(id => id.UseForDownload);
|
||||
.Where(id => id.UseForDownload)
|
||||
.ToListAsync(CancellationToken);
|
||||
Log.Debug($"Creating {connectorIdsManga.Count} update jobs...");
|
||||
|
||||
List<BaseWorker> newWorkers = new();
|
||||
foreach (MangaConnectorId<Manga> mangaConnectorId in connectorIdsManga)
|
||||
newWorkers.Add(new RetrieveMangaChaptersFromMangaconnectorWorker(mangaConnectorId, Tranga.Settings.DownloadLanguage));
|
||||
|
||||
return new Task<BaseWorker[]>(() => newWorkers.ToArray());
|
||||
List<BaseWorker> newWorkers = connectorIdsManga.Select(id => new RetrieveMangaChaptersFromMangaconnectorWorker(id, Tranga.Settings.DownloadLanguage))
|
||||
.ToList<BaseWorker>();
|
||||
|
||||
return newWorkers.ToArray();
|
||||
}
|
||||
|
||||
}
|
@@ -1,7 +1,11 @@
|
||||
using API.Schema.NotificationsContext;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Workers.MaintenanceWorkers;
|
||||
|
||||
/// <summary>
|
||||
/// Removes sent notifications from database
|
||||
/// </summary>
|
||||
public class RemoveOldNotificationsWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
|
||||
: BaseWorkerWithContext<NotificationsContext>(dependsOn), IPeriodic
|
||||
{
|
||||
@@ -10,10 +14,14 @@ public class RemoveOldNotificationsWorker(TimeSpan? interval = null, IEnumerable
|
||||
|
||||
protected override async Task<BaseWorker[]> DoWorkInternal()
|
||||
{
|
||||
IQueryable<Notification> toRemove = DbContext.Notifications.Where(n => n.IsSent || DateTime.UtcNow - n.Date > Interval);
|
||||
Log.Debug("Removing old notifications...");
|
||||
List<Notification> toRemove = await DbContext.Notifications.Where(n => n.IsSent).ToListAsync(CancellationToken);
|
||||
Log.Debug($"Removing {toRemove.Count} old notifications...");
|
||||
DbContext.RemoveRange(toRemove);
|
||||
|
||||
await DbContext.Sync(CancellationTokenSource.Token);
|
||||
if(await DbContext.Sync(CancellationToken) is { success: false } e)
|
||||
Log.Error($"Failed to save database changes: {e.exceptionMessage}");
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
|
@@ -1,8 +1,14 @@
|
||||
using API.Schema.NotificationsContext;
|
||||
using API.Schema.NotificationsContext.NotificationConnectors;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Workers;
|
||||
|
||||
/// <summary>
|
||||
/// Send notifications to NotificationConnectors
|
||||
/// </summary>
|
||||
/// <param name="interval"></param>
|
||||
/// <param name="dependsOn"></param>
|
||||
public class SendNotificationsWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
|
||||
: BaseWorkerWithContext<NotificationsContext>(dependsOn), IPeriodic
|
||||
{
|
||||
@@ -10,19 +16,26 @@ public class SendNotificationsWorker(TimeSpan? interval = null, IEnumerable<Base
|
||||
public TimeSpan Interval { get; set; } = interval??TimeSpan.FromMinutes(1);
|
||||
protected override async Task<BaseWorker[]> DoWorkInternal()
|
||||
{
|
||||
NotificationConnector[] connectors = DbContext.NotificationConnectors.ToArray();
|
||||
Notification[] notifications = DbContext.Notifications.Where(n => n.IsSent == false).ToArray();
|
||||
Log.Debug("Sending notifications...");
|
||||
List<NotificationConnector> connectors = await DbContext.NotificationConnectors.ToListAsync(CancellationToken);
|
||||
List<Notification> unsentNotifications = await DbContext.Notifications.Where(n => n.IsSent == false).ToListAsync(CancellationToken);
|
||||
|
||||
foreach (Notification notification in notifications)
|
||||
Log.Debug($"Sending {unsentNotifications.Count} notifications to {connectors.Count} connectors...");
|
||||
|
||||
unsentNotifications.ForEach(notification =>
|
||||
{
|
||||
foreach (NotificationConnector connector in connectors)
|
||||
connectors.ForEach(connector =>
|
||||
{
|
||||
connector.SendNotification(notification.Title, notification.Message);
|
||||
notification.IsSent = true;
|
||||
}
|
||||
}
|
||||
DbContext.Entry(notification).Property(n => n.IsSent).CurrentValue = true;
|
||||
});
|
||||
});
|
||||
|
||||
await DbContext.Sync(CancellationTokenSource.Token);
|
||||
Log.Debug("Notifications sent.");
|
||||
|
||||
if(await DbContext.Sync(CancellationToken) is { success: false } e)
|
||||
Log.Error($"Failed to save database changes: {e.exceptionMessage}");
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
|
@@ -3,22 +3,26 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Workers;
|
||||
|
||||
/// <summary>
|
||||
/// Create new Workers for Chapters on Manga marked for Download, that havent been downloaded yet.
|
||||
/// </summary>
|
||||
public class StartNewChapterDownloadsWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
|
||||
: BaseWorkerWithContext<MangaContext>(dependsOn), IPeriodic
|
||||
{
|
||||
|
||||
public DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
|
||||
public TimeSpan Interval { get; set; } = interval ?? TimeSpan.FromMinutes(1);
|
||||
protected override Task<BaseWorker[]> DoWorkInternal()
|
||||
protected override async Task<BaseWorker[]> DoWorkInternal()
|
||||
{
|
||||
IQueryable<MangaConnectorId<Chapter>> mangaConnectorIds = DbContext.MangaConnectorToChapter
|
||||
// Get missing chapters
|
||||
List<MangaConnectorId<Chapter>> missingChapters = await DbContext.MangaConnectorToChapter
|
||||
.Include(id => id.Obj)
|
||||
.Where(id => id.Obj.Downloaded == false && id.UseForDownload);
|
||||
.Where(id => id.Obj.Downloaded == false && id.UseForDownload)
|
||||
.ToListAsync(CancellationToken);
|
||||
|
||||
List<BaseWorker> newWorkers = new();
|
||||
foreach (MangaConnectorId<Chapter> mangaConnectorId in mangaConnectorIds)
|
||||
newWorkers.Add(new DownloadChapterFromMangaconnectorWorker(mangaConnectorId));
|
||||
// Create new jobs
|
||||
List<BaseWorker> newWorkers = missingChapters.Select(mcId => new DownloadChapterFromMangaconnectorWorker(mcId)).ToList<BaseWorker>();
|
||||
|
||||
return new Task<BaseWorker[]>(() => newWorkers.ToArray());
|
||||
return newWorkers.ToArray();
|
||||
}
|
||||
}
|
@@ -3,6 +3,9 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Workers;
|
||||
|
||||
/// <summary>
|
||||
/// Updates the database to reflect changes made on disk
|
||||
/// </summary>
|
||||
public class UpdateChaptersDownloadedWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
|
||||
: BaseWorkerWithContext<MangaContext>(dependsOn), IPeriodic
|
||||
{
|
||||
@@ -10,10 +13,14 @@ public class UpdateChaptersDownloadedWorker(TimeSpan? interval = null, IEnumerab
|
||||
public TimeSpan Interval { get; set; } = interval??TimeSpan.FromMinutes(60);
|
||||
protected override async Task<BaseWorker[]> DoWorkInternal()
|
||||
{
|
||||
foreach (Chapter dbContextChapter in DbContext.Chapters.Include(c => c.ParentManga))
|
||||
dbContextChapter.Downloaded = dbContextChapter.CheckDownloaded();
|
||||
Log.Debug("Checking chapter files...");
|
||||
List<Chapter> chapters = await DbContext.Chapters.Include(c => c.ParentManga).ToListAsync(CancellationToken);
|
||||
Log.Debug($"Checking {chapters.Count} chapters...");
|
||||
chapters.ForEach(chapter => DbContext.Entry(chapter).Property(c => c.Downloaded).CurrentValue = chapter.CheckDownloaded());
|
||||
|
||||
await DbContext.Sync(CancellationTokenSource.Token);
|
||||
if(await DbContext.Sync(CancellationToken) is { success: false } e)
|
||||
Log.Error($"Failed to save database changes: {e.exceptionMessage}");
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
@@ -1,7 +1,13 @@
|
||||
using API.Schema.MangaContext;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Workers;
|
||||
|
||||
/// <summary>
|
||||
/// Creates Workers to update covers for Manga
|
||||
/// </summary>
|
||||
/// <param name="interval"></param>
|
||||
/// <param name="dependsOn"></param>
|
||||
public class UpdateCoversWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
|
||||
: BaseWorkerWithContext<MangaContext>(dependsOn), IPeriodic
|
||||
{
|
||||
@@ -9,11 +15,10 @@ public class UpdateCoversWorker(TimeSpan? interval = null, IEnumerable<BaseWorke
|
||||
public DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
|
||||
public TimeSpan Interval { get; set; } = interval ?? TimeSpan.FromHours(6);
|
||||
|
||||
protected override Task<BaseWorker[]> DoWorkInternal()
|
||||
protected override async Task<BaseWorker[]> DoWorkInternal()
|
||||
{
|
||||
List<BaseWorker> workers = new();
|
||||
foreach (MangaConnectorId<Manga> mangaConnectorId in DbContext.MangaConnectorToManga)
|
||||
workers.Add(new DownloadCoverFromMangaconnectorWorker(mangaConnectorId));
|
||||
return new Task<BaseWorker[]>(() => workers.ToArray());
|
||||
List<MangaConnectorId<Manga>> manga = await DbContext.MangaConnectorToManga.Where(mcId => mcId.UseForDownload).ToListAsync(CancellationToken);
|
||||
List<BaseWorker> newWorkers = manga.Select(m => new DownloadCoverFromMangaconnectorWorker(m)).ToList<BaseWorker>();
|
||||
return newWorkers.ToArray();
|
||||
}
|
||||
}
|
@@ -4,6 +4,11 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Workers;
|
||||
|
||||
/// <summary>
|
||||
/// Updates Metadata for all Manga
|
||||
/// </summary>
|
||||
/// <param name="interval"></param>
|
||||
/// <param name="dependsOn"></param>
|
||||
public class UpdateMetadataWorker(TimeSpan? interval = null, IEnumerable<BaseWorker>? dependsOn = null)
|
||||
: BaseWorkerWithContext<MangaContext>(dependsOn), IPeriodic
|
||||
{
|
||||
@@ -13,18 +18,27 @@ public class UpdateMetadataWorker(TimeSpan? interval = null, IEnumerable<BaseWor
|
||||
|
||||
protected override async Task<BaseWorker[]> DoWorkInternal()
|
||||
{
|
||||
IQueryable<string> mangaIds = DbContext.MangaConnectorToManga
|
||||
.Where(m => m.UseForDownload)
|
||||
.Select(m => m.ObjId);
|
||||
IQueryable<MetadataEntry> metadataEntriesToUpdate = DbContext.MetadataEntries
|
||||
.Include(e => e.MetadataFetcher)
|
||||
.Where(e =>
|
||||
mangaIds.Any(id => id == e.MangaId));
|
||||
|
||||
foreach (MetadataEntry metadataEntry in metadataEntriesToUpdate)
|
||||
await metadataEntry.MetadataFetcher.UpdateMetadata(metadataEntry, DbContext, CancellationTokenSource.Token);
|
||||
Log.Debug("Updating metadata...");
|
||||
// Get MetadataEntries of Manga marked for download
|
||||
List<MetadataEntry> metadataEntriesToUpdate = await DbContext.MangaConnectorToManga
|
||||
.Where(m => m.UseForDownload) // Get marked Manga
|
||||
.Join(
|
||||
DbContext.MetadataEntries.Include(e => e.MetadataFetcher).Include(e => e.Manga),
|
||||
mcId => mcId.ObjId,
|
||||
e => e.MangaId,
|
||||
(mcId, e) => e) // return MetadataEntry
|
||||
.ToListAsync(CancellationToken);
|
||||
Log.Debug($"Updating metadata of {metadataEntriesToUpdate.Count} manga...");
|
||||
|
||||
await DbContext.Sync(CancellationTokenSource.Token);
|
||||
foreach (MetadataEntry metadataEntry in metadataEntriesToUpdate)
|
||||
{
|
||||
Log.Debug($"Updating metadata of {metadataEntry}...");
|
||||
await metadataEntry.MetadataFetcher.UpdateMetadata(metadataEntry, DbContext, CancellationToken);
|
||||
}
|
||||
Log.Debug("Updated metadata.");
|
||||
|
||||
if(await DbContext.Sync(CancellationToken) is { success: false } e)
|
||||
Log.Error($"Failed to save database changes: {e.exceptionMessage}");
|
||||
|
||||
return [];
|
||||
}
|
||||
|
Reference in New Issue
Block a user