glax
4b0a1c0a9d
Added TaskManager and TrangaTask TaskManager manages all TrangaTasks and starts Tasks when necessary. Execution is necessary when time elapsed between last execution and now is greater than TrangaTask.reoccurrence.
52 lines
1.2 KiB
C#
52 lines
1.2 KiB
C#
namespace Tranga;
|
|
|
|
public class TaskManager
|
|
{
|
|
private readonly Dictionary<Publication, TrangaTask> _allTasks;
|
|
private bool _continueRunning = true;
|
|
|
|
public TaskManager()
|
|
{
|
|
_allTasks = new Dictionary<Publication, TrangaTask>();
|
|
Thread taskChecker = new(TaskCheckerThread);
|
|
taskChecker.Start();
|
|
}
|
|
|
|
private void TaskCheckerThread()
|
|
{
|
|
while (_continueRunning)
|
|
{
|
|
foreach (TrangaTask task in _allTasks.Values.Where(tt => (DateTime.Now - tt.lastExecuted) > tt.reoccurrence))
|
|
{
|
|
if (!task.lastExecutedSuccessfully)
|
|
{
|
|
task.Abort();
|
|
//Add logging that task has failed
|
|
}
|
|
task.Execute();
|
|
}
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
|
|
public bool PublicationAlreadyAdded(Publication publication)
|
|
{
|
|
return false;
|
|
//TODO fuzzy check publications
|
|
}
|
|
|
|
public Publication[] GetAddedPublications()
|
|
{
|
|
return _allTasks.Keys.ToArray();
|
|
}
|
|
|
|
public TrangaTask[] GetTasks()
|
|
{
|
|
return _allTasks.Values.ToArray();
|
|
}
|
|
|
|
public void Shutdown()
|
|
{
|
|
_continueRunning = false;
|
|
}
|
|
} |