From b52b6b418985536f075ef33809cbf0db9fb3b91f Mon Sep 17 00:00:00 2001 From: Serraniel Date: Wed, 2 May 2018 19:57:10 +0200 Subject: [PATCH] some preparations for some improved logic --- .../Classes/Job.cs | 22 +++++++++++++++---- .../Classes/JobScheduler.cs | 4 +++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Discord Media Loader.Application/Classes/Job.cs b/Discord Media Loader.Application/Classes/Job.cs index cd6dbdb..c4d585a 100644 --- a/Discord Media Loader.Application/Classes/Job.cs +++ b/Discord Media Loader.Application/Classes/Job.cs @@ -61,7 +61,7 @@ namespace DML.AppCore.Classes return (from c in server.TextChannels where c.Id == id select c).FirstOrDefault(); } - internal async Task Scan() + internal async Task Scan() { Debug($"Starting scan of guild {GuildId} channel {ChannelId}..."); var result = new List(); @@ -70,6 +70,7 @@ namespace DML.AppCore.Classes var lastId = ulong.MaxValue; var isFirst = true; var finished = false; + var scanStartTimeStamp = DateTime.UtcNow; var guild = FindServerById(GuildId); var channel = FindChannelById(guild, ChannelId); @@ -78,7 +79,7 @@ namespace DML.AppCore.Classes if (channel.GetUser(channel.Guild.CurrentUser.Id) == null) { Info("Skipping channel without access"); - return; + return true; } if (Math.Abs(StopTimestamp) < 0.4) @@ -110,7 +111,7 @@ namespace DML.AppCore.Classes foreach (var m in messages) { if (!IsValid) - return; + return false; Core.Scheduler.MessagesScanned++; @@ -120,7 +121,7 @@ namespace DML.AppCore.Classes Trace($"Updating lastId ({lastId}) to {m.Id}"); lastId = m.Id; } - + if (m.CreatedAt.UtcDateTime.ToUnixTimeStamp() <= StopTimestamp) { Debug("Found a message with a known timestamp...Stopping scan."); @@ -170,6 +171,19 @@ namespace DML.AppCore.Classes StopTimestamp = result[result.Count - 1].CreatedAt.UtcDateTime.ToUnixTimeStamp(); KnownTimestamp = StopTimestamp; Store(); + return false; + } + else + { + // if we found any messages we remember the timestamp of starting so we donĀ“t have to scan all past messages.... + StopTimestamp = scanStartTimeStamp.ToUnixTimeStamp(); + KnownTimestamp = StopTimestamp; + Store(); + + var realLastMessage = await channel.GetMessagesAsync(1).ToArray(); + return scanStartTimeStamp > (realLastMessage.SelectMany(realLastMessageArray => realLastMessageArray) + .FirstOrDefault()?.CreatedAt.UtcDateTime ?? + scanStartTimeStamp); } Debug($"Fisnished 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 21af39c..53df5af 100644 --- a/Discord Media Loader.Application/Classes/JobScheduler.cs +++ b/Discord Media Loader.Application/Classes/JobScheduler.cs @@ -102,7 +102,9 @@ namespace DML.Application.Classes Task.Run(async () => { - await job.Scan(); + var foundCount = await job.Scan(); + while (foundCount > 0) + foundCount = await job.Scan(); }); } catch (Exception ex)