Improved CancellationToken usage, Added more logging to Workers, Added Code-comments (please future me, be thankful)

This commit is contained in:
2025-09-02 19:36:06 +02:00
parent 6fa166363a
commit 55f04710a7
16 changed files with 235 additions and 121 deletions

View File

@@ -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 [];
}
}