Added Structs Chapter and Publication
Added TaskManager and TrangaTask TaskManager manages all TrangaTasks and starts Tasks when necessary. Execution is necessary when time elapsed between last execution and now is greater than TrangaTask.reoccurrence.
This commit is contained in:
parent
a0d2cb92bf
commit
4b0a1c0a9d
22
Chapter.cs
Normal file
22
Chapter.cs
Normal file
@ -0,0 +1,22 @@
|
||||
namespace Tranga;
|
||||
|
||||
public struct Chapter
|
||||
{
|
||||
public Publication publication { get; }
|
||||
public string name { get; }
|
||||
public uint volumeNumber { get; }
|
||||
public uint chapterNumber { get; }
|
||||
public string summary { get; }
|
||||
public string posterUrl { get; }//Better way?
|
||||
|
||||
public Chapter(Publication publication, string name, uint volumeNumber, uint chapterNumber, string summary,
|
||||
string posterUrl)
|
||||
{
|
||||
this.publication = publication;
|
||||
this.name = name;
|
||||
this.volumeNumber = volumeNumber;
|
||||
this.chapterNumber = chapterNumber;
|
||||
this.summary = summary;
|
||||
this.posterUrl = posterUrl;
|
||||
}
|
||||
}
|
9
Connector.cs
Normal file
9
Connector.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace Tranga;
|
||||
|
||||
public abstract class Connector
|
||||
{
|
||||
public abstract string name { get; }
|
||||
public abstract bool GetPublications(out Publication[] publications);
|
||||
public abstract bool GetChapters(Publication publication, out Chapter[] chapters);
|
||||
public abstract bool DownloadChapter(Chapter chapter); //where to?
|
||||
}
|
20
Publication.cs
Normal file
20
Publication.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace Tranga;
|
||||
|
||||
public struct Publication
|
||||
{
|
||||
public string sortName { get; }
|
||||
public string[] titles { get; }
|
||||
public string description { get; }
|
||||
public string[] tags { get; }
|
||||
public string posterUrl { get; } //maybe there is a better way?
|
||||
|
||||
|
||||
public Publication(string sortName, string description, string[] titles, string[] tags, string posterUrl)
|
||||
{
|
||||
this.sortName = sortName;
|
||||
this.description = description;
|
||||
this.titles = titles;
|
||||
this.tags = tags;
|
||||
this.posterUrl = posterUrl;
|
||||
}
|
||||
}
|
52
TaskManager.cs
Normal file
52
TaskManager.cs
Normal file
@ -0,0 +1,52 @@
|
||||
namespace Tranga;
|
||||
|
||||
public class TaskManager
|
||||
{
|
||||
private readonly Dictionary<Publication, TrangaTask> _allTasks;
|
||||
private bool _continueRunning = true;
|
||||
|
||||
public TaskManager()
|
||||
{
|
||||
_allTasks = new Dictionary<Publication, TrangaTask>();
|
||||
Thread taskChecker = new(TaskCheckerThread);
|
||||
taskChecker.Start();
|
||||
}
|
||||
|
||||
private void TaskCheckerThread()
|
||||
{
|
||||
while (_continueRunning)
|
||||
{
|
||||
foreach (TrangaTask task in _allTasks.Values.Where(tt => (DateTime.Now - tt.lastExecuted) > tt.reoccurrence))
|
||||
{
|
||||
if (!task.lastExecutedSuccessfully)
|
||||
{
|
||||
task.Abort();
|
||||
//Add logging that task has failed
|
||||
}
|
||||
task.Execute();
|
||||
}
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
public bool PublicationAlreadyAdded(Publication publication)
|
||||
{
|
||||
return false;
|
||||
//TODO fuzzy check publications
|
||||
}
|
||||
|
||||
public Publication[] GetAddedPublications()
|
||||
{
|
||||
return _allTasks.Keys.ToArray();
|
||||
}
|
||||
|
||||
public TrangaTask[] GetTasks()
|
||||
{
|
||||
return _allTasks.Values.ToArray();
|
||||
}
|
||||
|
||||
public void Shutdown()
|
||||
{
|
||||
_continueRunning = false;
|
||||
}
|
||||
}
|
49
TrangaTask.cs
Normal file
49
TrangaTask.cs
Normal file
@ -0,0 +1,49 @@
|
||||
namespace Tranga;
|
||||
|
||||
public class 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; }
|
||||
|
||||
public TrangaTask(Action taskAction, TimeSpan reoccurrence)
|
||||
{
|
||||
this._taskAction = taskAction;
|
||||
this.reoccurrence = reoccurrence;
|
||||
}
|
||||
|
||||
public void Abort()
|
||||
{
|
||||
if(_task is not null && !_task.IsCompleted)
|
||||
_task.Dispose();
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
lastExecuted = DateTime.Now;
|
||||
_task = new (_taskAction);
|
||||
_task.Start();
|
||||
}
|
||||
|
||||
public static TrangaTask CreateDownloadChapterTask(Connector connector, Chapter chapter, TimeSpan reoccurrence)
|
||||
{
|
||||
void TaskAction()
|
||||
{
|
||||
connector.DownloadChapter(chapter);
|
||||
}
|
||||
return new TrangaTask(TaskAction, reoccurrence);
|
||||
}
|
||||
|
||||
|
||||
public static TrangaTask CreateUpdateChaptersTask(Connector connector, Publication publication, TimeSpan reoccurrence)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public static TrangaTask CreateUpdatePublicationsTask(Connector connector, TimeSpan reoccurrence)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user