diff --git a/API/Program.cs b/API/Program.cs index b3b1ec7..7acff8f 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -128,8 +128,8 @@ using (var scope = app.Services.CreateScope()) TrangaSettings.Load(); Tranga.StartLogger(); -Tranga.JobStarterThread.Start(app.Services.GetService()!); -Tranga.NotificationSenderThread.Start(app.Services.GetService()!); +Tranga.JobStarterThread.Start(app.Services.CreateScope().ServiceProvider.GetService()); +Tranga.NotificationSenderThread.Start(app.Services.CreateScope().ServiceProvider.GetService()); app.UseCors("AllowAll"); diff --git a/API/Schema/Jobs/DownloadNewChaptersJob.cs b/API/Schema/Jobs/DownloadNewChaptersJob.cs index f767894..0f2ca81 100644 --- a/API/Schema/Jobs/DownloadNewChaptersJob.cs +++ b/API/Schema/Jobs/DownloadNewChaptersJob.cs @@ -12,7 +12,7 @@ public class DownloadNewChaptersJob(ulong recurrenceMs, string mangaId, string? public string MangaId { get; init; } = mangaId; public virtual Manga Manga { get; init; } - public override IEnumerable Run() + protected override IEnumerable RunInternal() { MangaConnector connector = Manga.MangaConnector; Chapter[] newChapters = connector.GetNewChapters(Manga); diff --git a/API/Schema/Jobs/DownloadSingleChapterJob.cs b/API/Schema/Jobs/DownloadSingleChapterJob.cs index 1975cb6..9581ae5 100644 --- a/API/Schema/Jobs/DownloadSingleChapterJob.cs +++ b/API/Schema/Jobs/DownloadSingleChapterJob.cs @@ -18,7 +18,7 @@ public class DownloadSingleChapterJob(string chapterId, string? parentJobId = nu public string ChapterId { get; init; } = chapterId; public virtual Chapter Chapter { get; init; } - public override IEnumerable Run() + protected override IEnumerable RunInternal() { MangaConnector connector = Chapter.ParentManga.MangaConnector; DownloadChapterImages(Chapter); diff --git a/API/Schema/Jobs/Job.cs b/API/Schema/Jobs/Job.cs index 3411f52..f4b9388 100644 --- a/API/Schema/Jobs/Job.cs +++ b/API/Schema/Jobs/Job.cs @@ -36,5 +36,13 @@ public abstract class Job NextExecution = LastExecution.AddMilliseconds(RecurrenceMs); } - public abstract IEnumerable Run(); + public IEnumerable Run() + { + this.state = JobState.Running; + IEnumerable? newJobs = RunInternal(); + this.state = JobState.Completed; + return newJobs; + } + + protected abstract IEnumerable RunInternal(); } \ No newline at end of file diff --git a/API/Schema/Jobs/MoveFileOrFolderJob.cs b/API/Schema/Jobs/MoveFileOrFolderJob.cs index a8dd2f0..9013b6f 100644 --- a/API/Schema/Jobs/MoveFileOrFolderJob.cs +++ b/API/Schema/Jobs/MoveFileOrFolderJob.cs @@ -6,7 +6,7 @@ public class MoveFileOrFolderJob(string fromLocation, string toLocation, string? public string FromLocation { get; init; } = fromLocation; public string ToLocation { get; init; } = toLocation; - public override IEnumerable Run() + protected override IEnumerable RunInternal() { throw new NotImplementedException(); } diff --git a/API/Schema/Jobs/UpdateMetadataJob.cs b/API/Schema/Jobs/UpdateMetadataJob.cs index 12ae59f..792ffa0 100644 --- a/API/Schema/Jobs/UpdateMetadataJob.cs +++ b/API/Schema/Jobs/UpdateMetadataJob.cs @@ -9,7 +9,7 @@ public class UpdateMetadataJob(ulong recurrenceMs, string mangaId, string? paren public string MangaId { get; init; } = mangaId; public virtual Manga Manga { get; init; } - public override IEnumerable Run() + protected override IEnumerable RunInternal() { throw new NotImplementedException(); } diff --git a/API/Tranga.cs b/API/Tranga.cs index 36f4967..e1ab3b7 100644 --- a/API/Tranga.cs +++ b/API/Tranga.cs @@ -1,4 +1,5 @@ using API.Schema; +using API.Schema.Jobs; using API.Schema.NotificationConnectors; using log4net; using log4net.Config; @@ -9,7 +10,8 @@ public static class Tranga { public static Thread NotificationSenderThread { get; } = new (NotificationSender); public static Thread JobStarterThread { get; } = new (JobStarter); - private static ILog Log = LogManager.GetLogger(typeof(Tranga)); + private static readonly List RunningJobs = new(); + private static readonly ILog Log = LogManager.GetLogger(typeof(Tranga)); internal static void StartLogger() { @@ -63,7 +65,36 @@ public static class Tranga Log.Info(TRANGA); while (true) { + List completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList(); + foreach (Job job in completedJobs) + if(job.RecurrenceMs < 1) + context.Jobs.Remove(job); + else + { + job.LastExecution = DateTime.UtcNow; + job.state = JobState.Waiting; + context.Jobs.Update(job); + } + List runJobs = context.Jobs.Where(j => j.state <= JobState.Running && j.NextExecution < DateTime.UtcNow).ToList(); + foreach (Job job in runJobs) + { + Thread t = new (() => + { + IEnumerable newJobs = job.Run(); + context.Jobs.AddRange(newJobs); + }); + RunningJobs.Add(t); + t.Start(); + context.Jobs.Update(job); + } + + Thread[] removeFromThreadsList = RunningJobs.Where(t => !t.IsAlive).ToArray(); + foreach (Thread thread in removeFromThreadsList) + RunningJobs.Remove(thread); + + context.SaveChanges(); + Thread.Sleep(2000); } } } \ No newline at end of file