using API.Schema.MangaContext;
using Microsoft.EntityFrameworkCore;
namespace API.Workers;
///
/// Updates the database to reflect changes made on disk
///
public class UpdateChaptersDownloadedWorker(TimeSpan? interval = null, IEnumerable? dependsOn = null)
: BaseWorkerWithContext(dependsOn), IPeriodic
{
public DateTime LastExecution { get; set; } = DateTime.UnixEpoch;
public TimeSpan Interval { get; set; } = interval??TimeSpan.FromMinutes(60);
protected override async Task DoWorkInternal()
{
Log.Debug("Checking chapter files...");
List chapters = await DbContext.Chapters.ToListAsync(CancellationToken);
Log.Debug($"Checking {chapters.Count} chapters...");
foreach (Chapter chapter in chapters)
{
try
{
chapter.Downloaded = await chapter.CheckDownloaded(DbContext, CancellationToken);
}
catch (Exception exception)
{
Log.Error(exception);
}
}
if(await DbContext.Sync(CancellationToken) is { success: false } e)
Log.Error($"Failed to save database changes: {e.exceptionMessage}");
return [];
}
}