diff --git a/Discord Media Loader.Application/Classes/Job.cs b/Discord Media Loader.Application/Classes/Job.cs index c4d585a..0f9927a 100644 --- a/Discord Media Loader.Application/Classes/Job.cs +++ b/Discord Media Loader.Application/Classes/Job.cs @@ -12,6 +12,13 @@ using static SweetLib.Utils.Logger.Logger; namespace DML.AppCore.Classes { + internal enum JobState + { + Idle, + Scanning, + Listening + } + public class Job { public int Id { get; set; } @@ -20,6 +27,8 @@ namespace DML.AppCore.Classes public double KnownTimestamp { get; set; } = 0; private double StopTimestamp { get; set; } = 0; private bool IsValid { get; set; } = true; + internal JobState State { get; set; } = JobState.Idle; + internal void Store() { @@ -61,6 +70,10 @@ namespace DML.AppCore.Classes return (from c in server.TextChannels where c.Id == id select c).FirstOrDefault(); } + /// + /// Performs scanning task of the job. + /// + /// Returns true if the newest messages have been scanned. internal async Task Scan() { Debug($"Starting scan of guild {GuildId} channel {ChannelId}..."); diff --git a/Discord Media Loader.Application/Classes/JobScheduler.cs b/Discord Media Loader.Application/Classes/JobScheduler.cs index 53df5af..ef47d62 100644 --- a/Discord Media Loader.Application/Classes/JobScheduler.cs +++ b/Discord Media Loader.Application/Classes/JobScheduler.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Threading.Tasks; using Discord; using DML.AppCore.Classes; @@ -7,7 +8,6 @@ using System.Globalization; using System.IO; using System.Linq; using System.Net; -using System.Threading.Tasks; using Discord; using Discord.WebSocket; using DML.Application.Classes; @@ -95,21 +95,29 @@ namespace DML.Application.Classes //foreach (var job in JobList) for (var i = JobList.Count - 1; i >= 0; i--) { - try + if (JobList[i].State == JobState.Idle) { - var job = JobList[i]; - Logger.Debug($"Checking job {job}"); - - Task.Run(async () => + try { - var foundCount = await job.Scan(); - while (foundCount > 0) - foundCount = await job.Scan(); - }); - } - catch (Exception ex) - { - Logger.Error(ex.Message); + var job = JobList[i]; + Logger.Debug($"Checking job {job.Id}"); + + Task.Run(async () => + { + var scanFinished = await job.Scan(); + Logger.Trace($"Scan result of {job.Id}: {scanFinished}"); + + while (!scanFinished) + { + scanFinished = await job.Scan(); + Logger.Trace($"Scan result of {job.Id}: {scanFinished}"); + } + }); + } + catch (Exception ex) + { + Logger.Error(ex.Message); + } } } }