Renamed DownloadNewChaptersTask to MonitorPublicationTask

Added TrangaTask.Clone() method
Rewrote TrangaTask.progress for the billionth+1 time.
Removed Increment and DecrementProgress methods
Removed TrangaTask.ReplaceFailedChildTask method
Changed return type of TrangaTask.ExecuteTask to bool, signifying success.
Added Failed Execution state to TrangaTask
Replaced taskManager failed-task logic
Removed TaskManager bulky AddTask and DeleteTask methods
Removed TaskManager bulky Constructor
This commit is contained in:
2023-06-20 14:57:44 +02:00
parent 23dfdc0933
commit e883277400
8 changed files with 195 additions and 262 deletions

View File

@ -10,7 +10,9 @@ public class DownloadChapterTask : TrangaTask
public string language { get; }
public Chapter chapter { get; }
public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", DownloadNewChaptersTask? parentTask = null) : base(task, TimeSpan.Zero, parentTask)
private double _dctProgress = 0;
public DownloadChapterTask(string connectorName, Publication publication, Chapter chapter, string language = "en", MonitorPublicationTask? parentTask = null) : base(Task.DownloadChapter, TimeSpan.Zero, parentTask)
{
this.chapter = chapter;
this.connectorName = connectorName;
@ -18,21 +20,34 @@ public class DownloadChapterTask : TrangaTask
this.language = language;
}
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
protected override bool ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
{
if (cancellationToken?.IsCancellationRequested??false)
return;
if(this.parentTask is not null)
this.parentTask.state = ExecutionState.Running;
return false;
Connector connector = taskManager.GetConnector(this.connectorName);
connector.CopyCoverFromCacheToDownloadLocation(this.publication, taskManager.settings);
bool downloadSuccess = connector.DownloadChapter(this.publication, this.chapter, this, cancellationToken);
if(this.parentTask is not null)
this.parentTask.state = ExecutionState.Waiting;
taskManager.DeleteTask(this);
if(downloadSuccess && parentTask is not null)
foreach(NotificationManager nm in taskManager.settings.notificationManagers)
nm.SendNotification("New Chapter downloaded", $"{this.publication.sortName} {this.chapter.chapterNumber} {this.chapter.name}");
return downloadSuccess;
}
public override TrangaTask Clone()
{
return new DownloadChapterTask(this.connectorName, this.publication, this.chapter,
this.language, (MonitorPublicationTask?)this.parentTask);
}
protected override double GetProgress()
{
return _dctProgress;
}
internal void IncrementProgress(double amount)
{
this._dctProgress += amount;
}
public override string ToString()

View File

@ -1,45 +0,0 @@
using Logging;
namespace Tranga.TrangaTasks;
public class DownloadNewChaptersTask : TrangaTask
{
public string connectorName { get; }
public Publication publication { get; }
public string language { get; }
public DownloadNewChaptersTask(Task task, string connectorName, Publication publication, TimeSpan reoccurrence, string language = "en") : base(task, reoccurrence)
{
this.connectorName = connectorName;
this.publication = publication;
this.language = language;
}
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
{
if (cancellationToken?.IsCancellationRequested??false)
return;
Publication pub = publication!;
Connector connector = taskManager.GetConnector(this.connectorName);
//Check if Publication already has a Folder
pub.CreatePublicationFolder(taskManager.settings.downloadLocation);
List<Chapter> newChapters = taskManager.GetNewChaptersList(connector, pub, language!);
connector.CopyCoverFromCacheToDownloadLocation(pub, taskManager.settings);
pub.SaveSeriesInfoJson(connector.downloadLocation);
foreach (Chapter newChapter in newChapters)
{
DownloadChapterTask newTask = new (Task.DownloadChapter, this.connectorName, pub, newChapter, this.language, this);
this.childTasks.Add(newTask);
newTask.state = ExecutionState.Enqueued;
taskManager.AddTask(newTask);
}
}
public override string ToString()
{
return $"{base.ToString()}, {connectorName}, {publication.sortName} {publication.internalId}";
}
}

View File

@ -0,0 +1,59 @@
using Logging;
namespace Tranga.TrangaTasks;
public class MonitorPublicationTask : TrangaTask
{
public string connectorName { get; }
public Publication publication { get; }
public string language { get; }
public MonitorPublicationTask(string connectorName, Publication publication, TimeSpan reoccurrence, string language = "en") : base(Task.MonitorPublication, reoccurrence)
{
this.connectorName = connectorName;
this.publication = publication;
this.language = language;
}
protected override bool ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
{
if (cancellationToken?.IsCancellationRequested??false)
return false;
Connector connector = taskManager.GetConnector(this.connectorName);
//Check if Publication already has a Folder
publication.CreatePublicationFolder(taskManager.settings.downloadLocation);
List<Chapter> newChapters = taskManager.GetNewChaptersList(connector, publication, language);
connector.CopyCoverFromCacheToDownloadLocation(publication, taskManager.settings);
publication.SaveSeriesInfoJson(connector.downloadLocation);
foreach (Chapter newChapter in newChapters)
{
DownloadChapterTask newTask = new (this.connectorName, publication, newChapter, this.language, this);
this.childTasks.Add(newTask);
newTask.state = ExecutionState.Enqueued;
taskManager.AddTask(newTask);
}
return true;
}
public override TrangaTask Clone()
{
return new MonitorPublicationTask(this.connectorName, this.publication, this.reoccurrence,
this.language);
}
protected override double GetProgress()
{
if (this.childTasks.Count > 0)
return this.childTasks.Sum(ct => ct.progress) / childTasks.Count;
return 1;
}
public override string ToString()
{
return $"{base.ToString()}, {connectorName}, {publication.sortName} {publication.internalId}";
}
}

View File

@ -4,16 +4,26 @@ namespace Tranga.TrangaTasks;
public class UpdateLibrariesTask : TrangaTask
{
public UpdateLibrariesTask(Task task, TimeSpan reoccurrence) : base(task, reoccurrence)
public UpdateLibrariesTask(TimeSpan reoccurrence) : base(Task.UpdateLibraries, reoccurrence)
{
}
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
protected override bool ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
{
if (cancellationToken?.IsCancellationRequested??false)
return;
return false;
foreach(LibraryManager lm in taskManager.settings.libraryManagers)
lm.UpdateLibrary();
IncrementProgress(1);
return true;
}
public override TrangaTask Clone()
{
return new UpdateLibrariesTask(this.reoccurrence);
}
protected override double GetProgress()
{
return 1;
}
}