some preparations for some improved logic

This commit is contained in:
Serraniel 2018-05-02 19:57:10 +02:00
parent ec4ee45182
commit b52b6b4189
2 changed files with 21 additions and 5 deletions

View file

@ -61,7 +61,7 @@ namespace DML.AppCore.Classes
return (from c in server.TextChannels where c.Id == id select c).FirstOrDefault(); return (from c in server.TextChannels where c.Id == id select c).FirstOrDefault();
} }
internal async Task Scan() internal async Task<bool> Scan()
{ {
Debug($"Starting scan of guild {GuildId} channel {ChannelId}..."); Debug($"Starting scan of guild {GuildId} channel {ChannelId}...");
var result = new List<IMessage>(); var result = new List<IMessage>();
@ -70,6 +70,7 @@ namespace DML.AppCore.Classes
var lastId = ulong.MaxValue; var lastId = ulong.MaxValue;
var isFirst = true; var isFirst = true;
var finished = false; var finished = false;
var scanStartTimeStamp = DateTime.UtcNow;
var guild = FindServerById(GuildId); var guild = FindServerById(GuildId);
var channel = FindChannelById(guild, ChannelId); var channel = FindChannelById(guild, ChannelId);
@ -78,7 +79,7 @@ namespace DML.AppCore.Classes
if (channel.GetUser(channel.Guild.CurrentUser.Id) == null) if (channel.GetUser(channel.Guild.CurrentUser.Id) == null)
{ {
Info("Skipping channel without access"); Info("Skipping channel without access");
return; return true;
} }
if (Math.Abs(StopTimestamp) < 0.4) if (Math.Abs(StopTimestamp) < 0.4)
@ -110,7 +111,7 @@ namespace DML.AppCore.Classes
foreach (var m in messages) foreach (var m in messages)
{ {
if (!IsValid) if (!IsValid)
return; return false;
Core.Scheduler.MessagesScanned++; Core.Scheduler.MessagesScanned++;
@ -120,7 +121,7 @@ namespace DML.AppCore.Classes
Trace($"Updating lastId ({lastId}) to {m.Id}"); Trace($"Updating lastId ({lastId}) to {m.Id}");
lastId = m.Id; lastId = m.Id;
} }
if (m.CreatedAt.UtcDateTime.ToUnixTimeStamp() <= StopTimestamp) if (m.CreatedAt.UtcDateTime.ToUnixTimeStamp() <= StopTimestamp)
{ {
Debug("Found a message with a known timestamp...Stopping scan."); 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(); StopTimestamp = result[result.Count - 1].CreatedAt.UtcDateTime.ToUnixTimeStamp();
KnownTimestamp = StopTimestamp; KnownTimestamp = StopTimestamp;
Store(); 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}."); Debug($"Fisnished scan of guild {GuildId} channel {ChannelId}.");

View file

@ -102,7 +102,9 @@ namespace DML.Application.Classes
Task.Run(async () => Task.Run(async () =>
{ {
await job.Scan(); var foundCount = await job.Scan();
while (foundCount > 0)
foundCount = await job.Scan();
}); });
} }
catch (Exception ex) catch (Exception ex)