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:
2023-05-19 14:00:30 +02:00
parent 3d9e3d019d
commit 6de6d060c4
3 changed files with 76 additions and 59 deletions

View File

@ -1,64 +1,38 @@
namespace Tranga;
using System.Text.Json.Serialization;
public class TrangaTask
namespace Tranga;
public struct TrangaTask
{
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; }
[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(Action taskAction, TimeSpan reoccurrence)
public TrangaTask(Connector connector, Task task, Publication publication, TimeSpan reoccurrence, string language = "")
{
this._taskAction = taskAction;
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();
bool ret = (DateTime.Now - lastExecuted) > reoccurrence;
if (ret && willBeExecuted)
lastExecuted = DateTime.Now;
return ret;
}
public void Execute()
public enum Task
{
lastExecuted = DateTime.Now;
_task = new (_taskAction);
_task.Start();
}
public static TrangaTask CreateDownloadChapterTask(Connector connector, Publication publication, Chapter chapter, TimeSpan reoccurrence)
{
void TaskAction()
{
connector.DownloadChapter(publication, chapter);
}
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);
UpdatePublications,
UpdateChapters,
DownloadNewChapters
}
}