Working TaskManager and Tasks

This commit is contained in:
2023-05-19 16:27:56 +02:00
parent cfaf8064cc
commit dd58efce06
4 changed files with 207 additions and 53 deletions

View File

@ -2,18 +2,22 @@
public static class TaskExecutor
{
public static void Execute(TrangaTask trangaTask, Dictionary<Publication, List<Chapter>> chapterCollection)
public static void Execute(Connector[] connectors, TrangaTask trangaTask, Dictionary<Publication, List<Chapter>> chapterCollection)
{
Connector? connector = connectors.FirstOrDefault(c => c.name == trangaTask.connectorName);
if (connector is null)
throw new ArgumentException($"Connector {trangaTask.connectorName} is not a known connector.");
switch (trangaTask.task)
{
case TrangaTask.Task.DownloadNewChapters:
DownloadNewChapters(trangaTask.connector, trangaTask.publication, trangaTask.language, chapterCollection);
DownloadNewChapters(connector, (Publication)trangaTask.publication!, trangaTask.language, chapterCollection);
break;
case TrangaTask.Task.UpdateChapters:
UpdateChapters(trangaTask.connector, trangaTask.publication, trangaTask.language, chapterCollection);
UpdateChapters(connector, (Publication)trangaTask.publication!, trangaTask.language, chapterCollection);
break;
case TrangaTask.Task.UpdatePublications:
UpdatePublications(trangaTask.connector, chapterCollection);
UpdatePublications(connector, chapterCollection);
break;
}
}

View File

@ -8,9 +8,13 @@ public class TaskManager
private readonly Dictionary<Publication, List<Chapter>> _chapterCollection;
private readonly HashSet<TrangaTask> _allTasks;
private bool _continueRunning = true;
public TaskManager()
private readonly Connector[] connectors;
private readonly string folderPath;
public TaskManager(string folderPath)
{
this.folderPath = folderPath;
this.connectors = new Connector[]{ new MangaDex(folderPath) };
_chapterCollection = new();
_allTasks = ImportTasks(Directory.GetCurrentDirectory());
Thread taskChecker = new(TaskCheckerThread);
@ -21,29 +25,43 @@ public class TaskManager
{
while (_continueRunning)
{
foreach (TrangaTask task in _allTasks.Where(trangaTask => trangaTask.ShouldExecute(true)))
foreach (TrangaTask task in _allTasks)
{
TaskExecutor.Execute(task, this._chapterCollection);
if(task.ShouldExecute())
TaskExecutor.Execute(this.connectors, task, this._chapterCollection);
task.lastExecuted = DateTime.Now;
}
Thread.Sleep(1000);
}
}
public bool AddTask(TrangaTask.Task task, Connector connector, Publication publication, TimeSpan reoccurrence,
public bool AddTask(TrangaTask.Task task, Connector connector, Publication? publication, TimeSpan reoccurrence,
string language = "")
{
if(!_allTasks.Any(trangaTask => trangaTask.task != task && trangaTask.publication.downloadUrl != publication.downloadUrl))
return _allTasks.Add(new TrangaTask(connector, task, publication, reoccurrence, language));
if(!_allTasks.Any(trangaTask => trangaTask.task != task && trangaTask.connectorName != connector.name && trangaTask.publication?.downloadUrl != publication?.downloadUrl))
return _allTasks.Add(new TrangaTask(connector.name, task, publication, reoccurrence, language));
return false;
}
public bool RemoveTask(TrangaTask.Task task, Publication publication)
public bool RemoveTask(TrangaTask.Task task, string connectorName, Publication? publication)
{
return (_allTasks.RemoveWhere(trangaTask =>
trangaTask.task == task && trangaTask.publication.downloadUrl == publication.downloadUrl)
trangaTask.task == task && trangaTask.connectorName == connectorName && trangaTask.publication?.downloadUrl == publication?.downloadUrl)
> 0);
}
public Dictionary<string, Connector> GetAvailableConnectors()
{
return this.connectors.ToDictionary(connector => connector.name, connector => connector);
}
public TrangaTask[] GetAllTasks()
{
TrangaTask[] ret = new TrangaTask[_allTasks.Count];
_allTasks.CopyTo(ret);
return ret;
}
public void Shutdown()
{
_continueRunning = false;

View File

@ -1,6 +1,6 @@
namespace Tranga;
public struct TrangaTask
public class TrangaTask
{
public TimeSpan reoccurrence { get; }
public DateTime lastExecuted { get; set; }
@ -9,22 +9,21 @@ public struct TrangaTask
public Publication? publication { get; }
public string language { get; }
public TrangaTask(Connector connector, Task task, Publication publication, TimeSpan reoccurrence, string language = "")
public TrangaTask(string connectorName, Task task, Publication? publication, TimeSpan reoccurrence, string language = "")
{
if (task != Task.UpdatePublications && publication is null)
throw new ArgumentException($"Publication has to be not null for task {task}");
this.publication = publication;
this.reoccurrence = reoccurrence;
this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
this.connector = connector;
this.connectorName = connectorName;
this.task = task;
this.publication = publication;
this.language = language;
}
public bool ShouldExecute(bool willBeExecuted)
public bool ShouldExecute()
{
bool ret = (DateTime.Now - lastExecuted) > reoccurrence;
if (ret && willBeExecuted)
lastExecuted = DateTime.Now;
return ret;
return DateTime.Now.Subtract(this.lastExecuted) > reoccurrence;
}
public enum Task