diff --git a/src/Discord Media Loader.Application/Classes/Job.cs b/src/Discord Media Loader.Application/Classes/Job.cs index e69fce4..ba9e16e 100644 --- a/src/Discord Media Loader.Application/Classes/Job.cs +++ b/src/Discord Media Loader.Application/Classes/Job.cs @@ -92,6 +92,7 @@ namespace DML.AppCore.Classes Debug($"Starting scan of guild {GuildId} channel {ChannelId}..."); var result = new List(); const ushort limit = 100; + State = JobState.Scanning; var finished = false; var scanStartTimeStamp = DateTime.UtcNow; diff --git a/src/Discord Media Loader.Application/Classes/JobScheduler.cs b/src/Discord Media Loader.Application/Classes/JobScheduler.cs index c999141..a0a05dd 100644 --- a/src/Discord Media Loader.Application/Classes/JobScheduler.cs +++ b/src/Discord Media Loader.Application/Classes/JobScheduler.cs @@ -16,7 +16,6 @@ using Discord; using DML.AppCore.Classes; using SweetLib.Utils.Logger; -using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -30,8 +29,10 @@ namespace DML.Application.Classes private bool Run { get; set; } = false; public List JobList { get; set; } = new List(); - public Dictionary> RunningJobs = new Dictionary>(); + public Dictionary> RunningJobs { get; } = new Dictionary>(); internal int RunningThreads { get; set; } = 0; + internal Task SchedulerTask { get; private set; } + internal Task DownloadTask { get; private set; } internal ulong MessagesScanned { @@ -87,44 +88,46 @@ namespace DML.Application.Classes } } + public void StartScheduler() + { + Logger.Info("Starting scheduler jobs"); + SchedulerTask = Task.Run(() => + { + PerformSchedulerTask(); + }); + + DownloadTask = Task.Run(() => + { + PerformDownloads(); + }); + } + public void Stop() { Run = false; } - public void ScanAll() + private async void PerformSchedulerTask() { - Logger.Info("Started JobScheduler..."); - - Logger.Debug("Entering job list handler loop..."); - //foreach (var job in JobList) - for (var i = JobList.Count - 1; i >= 0; i--) + Logger.Trace("SchedulerTask started"); + foreach (var job in JobList) { - if (JobList[i].State == JobState.Idle) + if (job.State == JobState.Idle) { - try - { - 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); - } + // scan all old messages first + Logger.Debug($"Starting scan for job {job.Id}"); + await job.Scan(); + job.State = JobState.Listening; // set to listening now + Logger.Debug($"Scan for job {job.Id} finished"); } } + Logger.Trace("All jobs have been scanned"); + } + + private async void PerformDownloads() + { + Logger.Trace("SchedulerTask started"); + // TODO } } } diff --git a/src/Discord Media Loader.Application/Core/Core.cs b/src/Discord Media Loader.Application/Core/Core.cs index 5c9193b..130e714 100644 --- a/src/Discord Media Loader.Application/Core/Core.cs +++ b/src/Discord Media Loader.Application/Core/Core.cs @@ -280,8 +280,9 @@ namespace DML.Application.Core } } - Settings.RescanRequired = false; - Settings.Store(); + // TODO + // Settings.RescanRequired = false; + // Settings.Store(); if (Settings.UseRPC) { @@ -309,7 +310,7 @@ namespace DML.Application.Core splash.Close(); Logger.Info("Starting scheduler..."); - Scheduler.ScanAll(); + Scheduler.StartScheduler(); System.Windows.Forms.Application.Run(new MainForm());