Revert serialization attempt

This commit is contained in:
glax 2023-05-18 21:08:09 +02:00
parent 1f8e8fb740
commit 86bb49508a
3 changed files with 59 additions and 79 deletions

View File

@ -42,17 +42,17 @@ public struct Publication
internal struct SeriesInfo internal struct SeriesInfo
{ {
[JsonInclude]public Metadata metadata { get; } [JsonRequired]public Metadata metadata { get; }
public SeriesInfo(Metadata metadata) => this.metadata = metadata; public SeriesInfo(Metadata metadata) => this.metadata = metadata;
} }
internal struct Metadata internal struct Metadata
{ {
[JsonInclude]public string name { get; } [JsonRequired]public string name { get; }
[JsonInclude]public string year { get; } [JsonRequired]public string year { get; }
[JsonInclude]public string status { get; } [JsonRequired]public string status { get; }
// ReSharper disable twice InconsistentNaming // ReSharper disable twice InconsistentNaming
[JsonInclude]public string description_text { get; } [JsonRequired]public string description_text { get; }
public Metadata(string name, string year, string status, string description_text) public Metadata(string name, string year, string status, string description_text)
{ {

View File

@ -2,7 +2,7 @@
public class TaskManager public class TaskManager
{ {
private Dictionary<Publication, Chapter[]> _chapterCollection; private readonly 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))
{ {
task.Execute(ref _chapterCollection); if (!task.lastExecutedSuccessfully)
{
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()
{ {
return this._chapterCollection.Keys.ToArray(); throw new NotImplementedException();
} }
public TrangaTask[] GetTasks() public TrangaTask[] GetTasks()

View File

@ -1,84 +1,64 @@
using System.Text.Json.Serialization; namespace Tranga;
namespace Tranga;
public class TrangaTask public class TrangaTask
{ {
[JsonInclude]public TimeSpan reoccurrence { get; } private readonly Action _taskAction;
[JsonInclude]public DateTime lastExecuted { get; private set; } private Task? _task;
[JsonIgnore] private Connector connector { get; } public bool lastExecutedSuccessfully => _task is not null && _task.IsCompleted;
[JsonInclude] public string connectorName; public TimeSpan reoccurrence { get; }
[JsonInclude]public AvailableTasks task { get; } public DateTime lastExecuted { get; private set; }
public enum AvailableTasks
{
DownloadNewChapters,
UpdateChapters,
UpdatePublications
};
[JsonIgnore]public Publication? publication { get; }
[JsonInclude]public string? publicationIdentifier;
[JsonInclude]public string language { get; }
public TrangaTask(Action taskAction, TimeSpan reoccurrence)
public TrangaTask(Connector connector, AvailableTasks task, TimeSpan reoccurrence, Publication? publication = null, string language = "en")
{ {
this.connector = connector; this._taskAction = taskAction;
this.connectorName = connector.name;
this.task = task;
this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
this.reoccurrence = reoccurrence; this.reoccurrence = reoccurrence;
this.publication = publication;
this.publicationIdentifier = publication?.downloadUrl;
this.language = language;
if (publication is null && task 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 Execute(ref Dictionary<Publication, Chapter[]> chapterCollection) public void Abort()
{ {
switch (this.task) if(_task is not null && !_task.IsCompleted)
{ _task.Dispose();
case AvailableTasks.UpdateChapters:
UpdateChapters(ref chapterCollection);
break;
case AvailableTasks.UpdatePublications:
UpdatePublications(ref chapterCollection);
break;
case AvailableTasks.DownloadNewChapters:
DownloadNewChapters(UpdateChapters(ref chapterCollection));
break;
} }
this.lastExecuted = DateTime.Now; public void Execute()
{
lastExecuted = DateTime.Now;
_task = new (_taskAction);
_task.Start();
} }
private Chapter[] UpdateChapters(ref Dictionary<Publication, Chapter[]> chapterCollection) public static TrangaTask CreateDownloadChapterTask(Connector connector, Publication publication, Chapter chapter, TimeSpan reoccurrence)
{ {
Publication pPublication = (Publication)this.publication!; void TaskAction()
Chapter[] presentChapters = chapterCollection[pPublication]; {
Chapter[] allChapters = connector.GetChapters(pPublication); connector.DownloadChapter(publication, chapter);
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();
} }
private void UpdatePublications(ref Dictionary<Publication, Chapter[]> chapterCollection) public static TrangaTask CreateUpdateChaptersTask(ref Dictionary<Publication, Chapter[]> chapterCollection, Connector connector, Publication publication, string language, TimeSpan reoccurrence)
{ {
Publication[] allPublications = connector.GetPublications(); Dictionary<Publication, Chapter[]> pChapterCollection = chapterCollection;
foreach(Publication publication in allPublications)
chapterCollection.TryAdd(publication, Array.Empty<Chapter>()); void TaskAction()
{
Chapter[] chapters = connector.GetChapters(publication, language);
if(pChapterCollection.TryAdd(publication, chapters))
pChapterCollection[publication] = chapters;
}
return new TrangaTask(TaskAction, reoccurrence);
} }
private void DownloadNewChapters(Chapter[] newChapters) public static TrangaTask CreateUpdatePublicationsTask(ref Dictionary<Publication, Chapter[]> chapterCollection, Connector connector, TimeSpan reoccurrence)
{ {
Publication pPublication = (Publication)this.publication!; Dictionary<Publication, Chapter[]> pChapterCollection = chapterCollection;
foreach(Chapter chapter in newChapters)
connector.DownloadChapter(pPublication, chapter); void TaskAction()
{
Publication[] publications = connector.GetPublications();
foreach (Publication publication in publications)
pChapterCollection.TryAdd(publication, Array.Empty<Chapter>());
}
return new TrangaTask(TaskAction, reoccurrence);
} }
} }