2017-10-04 13:14:44 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2018-06-25 21:14:10 +02:00
|
|
|
|
using System.Diagnostics;
|
2017-10-04 13:14:44 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2017-10-04 15:01:54 +02:00
|
|
|
|
using Discord;
|
2018-02-01 21:36:16 +01:00
|
|
|
|
using DML.AppCore.Classes;
|
2018-04-23 21:40:17 +02:00
|
|
|
|
using System.Globalization;
|
2017-10-04 13:14:44 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2017-10-04 15:01:54 +02:00
|
|
|
|
using Discord;
|
2017-10-04 13:14:44 +02:00
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using DML.Application.Classes;
|
|
|
|
|
using SweetLib.Utils;
|
2018-04-23 21:40:17 +02:00
|
|
|
|
using SweetLib.Utils.Extensions;
|
2017-10-04 13:14:44 +02:00
|
|
|
|
using SweetLib.Utils.Logger;
|
|
|
|
|
|
2018-02-01 21:36:16 +01:00
|
|
|
|
namespace DML.Application.Classes
|
2017-10-04 13:14:44 +02:00
|
|
|
|
{
|
|
|
|
|
public class JobScheduler
|
|
|
|
|
{
|
|
|
|
|
private ulong messageScanned = 0;
|
|
|
|
|
private ulong totalAttachments = 0;
|
|
|
|
|
private ulong attachmentsDownloaded = 0;
|
|
|
|
|
|
|
|
|
|
private bool Run { get; set; } = false;
|
|
|
|
|
public List<Job> JobList { get; set; } = new List<Job>();
|
2017-10-04 15:01:54 +02:00
|
|
|
|
public Dictionary<int, Queue<IMessage>> RunningJobs = new Dictionary<int, Queue<IMessage>>();
|
2017-10-04 13:14:44 +02:00
|
|
|
|
internal int RunningThreads { get; set; } = 0;
|
|
|
|
|
|
|
|
|
|
internal ulong MessagesScanned
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
return messageScanned;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
messageScanned = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal ulong TotalAttachments
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
return totalAttachments;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
totalAttachments = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal ulong AttachmentsDownloaded
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
return attachmentsDownloaded;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
attachmentsDownloaded = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
Run = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 21:36:16 +01:00
|
|
|
|
public void ScanAll()
|
2017-10-04 13:14:44 +02:00
|
|
|
|
{
|
2018-02-01 21:17:12 +01:00
|
|
|
|
Logger.Info("Started JobScheduler...");
|
2017-10-04 13:14:44 +02:00
|
|
|
|
|
2018-02-01 21:17:12 +01:00
|
|
|
|
Logger.Debug("Entering job list handler loop...");
|
|
|
|
|
//foreach (var job in JobList)
|
|
|
|
|
for (var i = JobList.Count - 1; i >= 0; i--)
|
2017-10-04 13:14:44 +02:00
|
|
|
|
{
|
2018-06-25 21:14:10 +02:00
|
|
|
|
if (JobList[i].State == JobState.Idle)
|
2017-10-04 13:14:44 +02:00
|
|
|
|
{
|
2018-06-25 21:14:10 +02:00
|
|
|
|
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}");
|
2018-01-22 20:07:10 +01:00
|
|
|
|
|
2018-06-25 21:14:10 +02:00
|
|
|
|
while (!scanFinished)
|
|
|
|
|
{
|
|
|
|
|
scanFinished = await job.Scan();
|
|
|
|
|
Logger.Trace($"Scan result of {job.Id}: {scanFinished}");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2018-01-22 20:07:10 +01:00
|
|
|
|
{
|
2018-06-25 21:14:10 +02:00
|
|
|
|
Logger.Error(ex.Message);
|
|
|
|
|
}
|
2017-10-04 13:14:44 +02:00
|
|
|
|
}
|
2018-02-01 21:17:12 +01:00
|
|
|
|
}
|
2017-10-04 13:14:44 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|