2023-05-17 23:23:01 +02:00
|
|
|
|
namespace Tranga;
|
|
|
|
|
|
|
|
|
|
public class TaskManager
|
|
|
|
|
{
|
2023-05-18 20:58:03 +02:00
|
|
|
|
private Dictionary<Publication, Chapter[]> _chapterCollection;
|
2023-05-18 19:25:46 +02:00
|
|
|
|
private readonly HashSet<TrangaTask> _allTasks;
|
2023-05-17 23:23:01 +02:00
|
|
|
|
private bool _continueRunning = true;
|
|
|
|
|
|
|
|
|
|
public TaskManager()
|
|
|
|
|
{
|
2023-05-18 19:25:46 +02:00
|
|
|
|
_chapterCollection = new();
|
|
|
|
|
_allTasks = new ();
|
2023-05-17 23:23:01 +02:00
|
|
|
|
Thread taskChecker = new(TaskCheckerThread);
|
|
|
|
|
taskChecker.Start();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 21:03:03 +02:00
|
|
|
|
public void AddTask(Connector connector, TrangaTask.AvailableTasks task, TimeSpan reoccurrence, Publication? publication = null, string language = "en")
|
2023-05-18 20:58:03 +02:00
|
|
|
|
{
|
|
|
|
|
this._allTasks.Add(new TrangaTask(connector, task, reoccurrence, publication, language));
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 23:23:01 +02:00
|
|
|
|
private void TaskCheckerThread()
|
|
|
|
|
{
|
|
|
|
|
while (_continueRunning)
|
|
|
|
|
{
|
2023-05-18 19:25:46 +02:00
|
|
|
|
foreach (TrangaTask task in _allTasks.Where(trangaTask => (DateTime.Now - trangaTask.lastExecuted) > trangaTask.reoccurrence))
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 20:58:03 +02:00
|
|
|
|
task.Execute(ref _chapterCollection);
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool PublicationAlreadyAdded(Publication publication)
|
|
|
|
|
{
|
2023-05-17 23:51:09 +02:00
|
|
|
|
throw new NotImplementedException();
|
2023-05-17 23:23:01 +02:00
|
|
|
|
//TODO fuzzy check publications
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Publication[] GetAddedPublications()
|
|
|
|
|
{
|
2023-05-18 20:58:03 +02:00
|
|
|
|
return this._chapterCollection.Keys.ToArray();
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TrangaTask[] GetTasks()
|
|
|
|
|
{
|
2023-05-18 19:25:46 +02:00
|
|
|
|
return _allTasks.ToArray();
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Shutdown()
|
|
|
|
|
{
|
|
|
|
|
_continueRunning = false;
|
|
|
|
|
}
|
|
|
|
|
}
|