Rewrite of Task-System for serialization.

This commit is contained in:
glax 2023-05-18 20:58:03 +02:00
parent db5470769c
commit 0fc146019a
2 changed files with 69 additions and 55 deletions

View File

@ -2,7 +2,7 @@
public class TaskManager public class TaskManager
{ {
private readonly Dictionary<Publication, Chapter[]> _chapterCollection; private Dictionary<Publication, Chapter[]> _chapterCollection;
private readonly HashSet<TrangaTask> _allTasks; private readonly HashSet<TrangaTask> _allTasks;
private bool _continueRunning = true; private bool _continueRunning = true;
@ -14,18 +14,18 @@ public class TaskManager
taskChecker.Start(); taskChecker.Start();
} }
public void AddTask(Connector connector, TrangaTask.availableTasks task, TimeSpan reoccurrence, Publication? publication = null, string language = "en")
{
this._allTasks.Add(new TrangaTask(connector, task, reoccurrence, publication, language));
}
private void TaskCheckerThread() private void TaskCheckerThread()
{ {
while (_continueRunning) while (_continueRunning)
{ {
foreach (TrangaTask task in _allTasks.Where(trangaTask => (DateTime.Now - trangaTask.lastExecuted) > trangaTask.reoccurrence)) foreach (TrangaTask task in _allTasks.Where(trangaTask => (DateTime.Now - trangaTask.lastExecuted) > trangaTask.reoccurrence))
{ {
if (!task.lastExecutedSuccessfully) task.Execute(ref _chapterCollection);
{
task.Abort();
//Add logging that task has failed
}
task.Execute();
} }
Thread.Sleep(1000); Thread.Sleep(1000);
} }
@ -39,7 +39,7 @@ public class TaskManager
public Publication[] GetAddedPublications() public Publication[] GetAddedPublications()
{ {
throw new NotImplementedException(); return this._chapterCollection.Keys.ToArray();
} }
public TrangaTask[] GetTasks() public TrangaTask[] GetTasks()

View File

@ -2,63 +2,77 @@
public class TrangaTask public class TrangaTask
{ {
private readonly Action _taskAction;
private Task? _task;
public bool lastExecutedSuccessfully => _task is not null && _task.IsCompleted;
public TimeSpan reoccurrence { get; } public TimeSpan reoccurrence { get; }
public DateTime lastExecuted { get; private set; } public DateTime lastExecuted { get; private set; }
public Connector connector { get; }
public TrangaTask(Action taskAction, TimeSpan reoccurrence) public availableTasks availableTaskToExecute { get; }
public enum availableTasks
{ {
this._taskAction = taskAction; downloadNewChapters,
updateChapters,
updatePublications
};
public Publication? publication { get; }
public string language { get; }
public TrangaTask(Connector connector, availableTasks availableTask, TimeSpan reoccurrence, Publication? publication = null, string language = "en")
{
this.connector = connector;
this.availableTaskToExecute = availableTask;
this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
this.reoccurrence = reoccurrence; this.reoccurrence = reoccurrence;
this.publication = publication;
this.language = language;
if (publication is null && availableTask is availableTasks.updateChapters or availableTasks.downloadNewChapters)
{
if (publication is null)
throw new ArgumentException(
"If task is updateChapters or downloadNewChapters, Argument publication can not be null!");
}
} }
public void Abort() public void Execute(ref Dictionary<Publication, Chapter[]> chapterCollection)
{ {
if(_task is not null && !_task.IsCompleted) switch (this.availableTaskToExecute)
_task.Dispose(); {
case availableTasks.updateChapters:
UpdateChapters(ref chapterCollection);
break;
case availableTasks.updatePublications:
UpdatePublications(ref chapterCollection);
break;
case availableTasks.downloadNewChapters:
DownloadNewChapters(UpdateChapters(ref chapterCollection));
break;
} }
public void Execute() this.lastExecuted = DateTime.Now;
{
lastExecuted = DateTime.Now;
_task = new (_taskAction);
_task.Start();
} }
public static TrangaTask CreateDownloadChapterTask(Connector connector, Publication publication, Chapter chapter, TimeSpan reoccurrence) private Chapter[] UpdateChapters(ref Dictionary<Publication, Chapter[]> chapterCollection)
{ {
void TaskAction() Publication pPublication = (Publication)this.publication!;
{ Chapter[] presentChapters = chapterCollection[pPublication];
connector.DownloadChapter(publication, chapter); Chapter[] allChapters = connector.GetChapters(pPublication);
} chapterCollection[pPublication] = allChapters;
return new TrangaTask(TaskAction, reoccurrence);
Dictionary<string, Chapter> pChapter = presentChapters.ToDictionary(chapter => chapter.url, chapter => chapter);
Dictionary<string, Chapter> aChapter = allChapters.ToDictionary(chapter => chapter.url, chapter => chapter);
return aChapter.Except(pChapter).ToDictionary(pair => pair.Key, pair => pair.Value).Values.ToArray();
} }
public static TrangaTask CreateUpdateChaptersTask(ref Dictionary<Publication, Chapter[]> chapterCollection, Connector connector, Publication publication, string language, TimeSpan reoccurrence) private void UpdatePublications(ref Dictionary<Publication, Chapter[]> chapterCollection)
{ {
Dictionary<Publication, Chapter[]> pChapterCollection = chapterCollection; Publication[] allPublications = connector.GetPublications();
foreach(Publication publication in allPublications)
void TaskAction() chapterCollection.TryAdd(publication, Array.Empty<Chapter>());
{
Chapter[] chapters = connector.GetChapters(publication, language);
if(pChapterCollection.TryAdd(publication, chapters))
pChapterCollection[publication] = chapters;
}
return new TrangaTask(TaskAction, reoccurrence);
} }
public static TrangaTask CreateUpdatePublicationsTask(ref Dictionary<Publication, Chapter[]> chapterCollection, Connector connector, TimeSpan reoccurrence) private void DownloadNewChapters(Chapter[] newChapters)
{ {
Dictionary<Publication, Chapter[]> pChapterCollection = chapterCollection; Publication pPublication = (Publication)this.publication!;
foreach(Chapter chapter in newChapters)
void TaskAction() connector.DownloadChapter(pPublication, chapter);
{
Publication[] publications = connector.GetPublications();
foreach (Publication publication in publications)
pChapterCollection.TryAdd(publication, Array.Empty<Chapter>());
}
return new TrangaTask(TaskAction, reoccurrence);
} }
} }