Rewrite Task-Structure for serialization.

TrangaTask include information on what to execute where, do not execute tasks.
TaskExecutor executes Tasks on information from TrangaTask
This commit is contained in:
glax 2023-05-19 14:00:30 +02:00
parent 3d9e3d019d
commit 6de6d060c4
3 changed files with 76 additions and 59 deletions

48
Tranga/TaskExecutor.cs Normal file
View File

@ -0,0 +1,48 @@
namespace Tranga;
public static class TaskExecutor
{
public static void Execute(TrangaTask trangaTask, Dictionary<Publication, List<Chapter>> chapterCollection)
{
switch (trangaTask.task)
{
case TrangaTask.Task.DownloadNewChapters:
DownloadNewChapters(trangaTask.connector, trangaTask.publication, trangaTask.language, chapterCollection);
break;
case TrangaTask.Task.UpdateChapters:
UpdateChapters(trangaTask.connector, trangaTask.publication, trangaTask.language, chapterCollection);
break;
case TrangaTask.Task.UpdatePublications:
UpdatePublications(trangaTask.connector, chapterCollection);
break;
}
}
private static void UpdatePublications(Connector connector, Dictionary<Publication, List<Chapter>> chapterCollection)
{
Publication[] publications = connector.GetPublications();
foreach (Publication publication in publications)
chapterCollection.TryAdd(publication, new List<Chapter>());
}
private static void DownloadNewChapters(Connector connector, Publication publication, string language, Dictionary<Publication, List<Chapter>> chapterCollection)
{
List<Chapter> newChapters = UpdateChapters(connector, publication, language, chapterCollection);
foreach(Chapter newChapter in newChapters)
connector.DownloadChapter(publication, newChapter);
}
private static List<Chapter> UpdateChapters(Connector connector, Publication publication, string language, Dictionary<Publication, List<Chapter>> chapterCollection)
{
List<Chapter> newChaptersList = new();
if (!chapterCollection.ContainsKey(publication))
return newChaptersList;
List<Chapter> currentChapters = chapterCollection[publication];
Chapter[] newChapters = connector.GetChapters(publication, language);
newChaptersList = newChapters.ToList()
.ExceptBy(currentChapters.Select(cChapter => cChapter.url), nChapter => nChapter.url).ToList();
return newChaptersList;
}
}

View File

@ -2,7 +2,7 @@
public class TaskManager public class TaskManager
{ {
private readonly Dictionary<Publication, Chapter[]> _chapterCollection; private readonly Dictionary<Publication, List<Chapter>> _chapterCollection;
private readonly HashSet<TrangaTask> _allTasks; private readonly HashSet<TrangaTask> _allTasks;
private bool _continueRunning = true; private bool _continueRunning = true;
@ -19,14 +19,9 @@ public class TaskManager
{ {
while (_continueRunning) while (_continueRunning)
{ {
foreach (TrangaTask task in _allTasks.Where(trangaTask => (DateTime.Now - trangaTask.lastExecuted) > trangaTask.reoccurrence)) foreach (TrangaTask task in _allTasks.Where(trangaTask => trangaTask.ShouldExecute(true)))
{ {
if (!task.lastExecutedSuccessfully) TaskExecutor.Execute(task, this._chapterCollection);
{
task.Abort();
//Add logging that task has failed
}
task.Execute();
} }
Thread.Sleep(1000); Thread.Sleep(1000);
} }

View File

@ -1,64 +1,38 @@
namespace Tranga; using System.Text.Json.Serialization;
public class TrangaTask namespace Tranga;
{
private readonly Action _taskAction;
private Task? _task;
public bool lastExecutedSuccessfully => _task is not null && _task.IsCompleted;
public TimeSpan reoccurrence { get; }
public DateTime lastExecuted { get; private set; }
public TrangaTask(Action taskAction, TimeSpan reoccurrence) public struct TrangaTask
{
[JsonInclude]public TimeSpan reoccurrence { get; }
[JsonInclude]public DateTime lastExecuted { get; set; }
[JsonInclude]public Connector connector { get; }
[JsonInclude]public Task task { get; }
[JsonInclude]public Publication publication { get; }
[JsonInclude]public string language { get; }
public TrangaTask(Connector connector, Task task, Publication publication, TimeSpan reoccurrence, string language = "")
{ {
this._taskAction = taskAction;
this.reoccurrence = reoccurrence; this.reoccurrence = reoccurrence;
this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
this.connector = connector;
this.task = task;
this.publication = publication;
this.language = language;
} }
public void Abort() public bool ShouldExecute(bool willBeExecuted)
{
if(_task is not null && !_task.IsCompleted)
_task.Dispose();
}
public void Execute()
{ {
bool ret = (DateTime.Now - lastExecuted) > reoccurrence;
if (ret && willBeExecuted)
lastExecuted = DateTime.Now; lastExecuted = DateTime.Now;
_task = new (_taskAction); return ret;
_task.Start();
} }
public static TrangaTask CreateDownloadChapterTask(Connector connector, Publication publication, Chapter chapter, TimeSpan reoccurrence) public enum Task
{ {
void TaskAction() UpdatePublications,
{ UpdateChapters,
connector.DownloadChapter(publication, chapter); DownloadNewChapters
}
return new TrangaTask(TaskAction, reoccurrence);
}
public static TrangaTask CreateUpdateChaptersTask(ref Dictionary<Publication, Chapter[]> chapterCollection, Connector connector, Publication publication, string language, TimeSpan reoccurrence)
{
Dictionary<Publication, Chapter[]> pChapterCollection = chapterCollection;
void TaskAction()
{
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)
{
Dictionary<Publication, Chapter[]> pChapterCollection = chapterCollection;
void TaskAction()
{
Publication[] publications = connector.GetPublications();
foreach (Publication publication in publications)
pChapterCollection.TryAdd(publication, Array.Empty<Chapter>());
}
return new TrangaTask(TaskAction, reoccurrence);
} }
} }