From 8e207c3119c38d75e489da7d2d8ace58a036aaf4 Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 11 Jun 2023 17:27:33 +0200 Subject: [PATCH] Better way of handling progress, and childProgress. --- Tranga/TaskManager.cs | 2 +- Tranga/TrangaTask.cs | 21 ++++++++++++++++----- Tranga/TrangaTasks/DownloadChapterTask.cs | 9 +-------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 71bef39..d96416e 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -135,7 +135,7 @@ public class TaskManager foreach (DownloadChapterTask taskToRemove in toRemove) { DeleteTask(taskToRemove); - DownloadChapterTask newTask = new DownloadChapterTask(taskToRemove.task, taskToRemove.connectorName, + DownloadChapterTask newTask = new (taskToRemove.task, taskToRemove.connectorName, taskToRemove.publication, taskToRemove.chapter, taskToRemove.language, taskToRemove.parentTask); AddTask(newTask); diff --git a/Tranga/TrangaTask.cs b/Tranga/TrangaTask.cs index a24b345..3d65699 100644 --- a/Tranga/TrangaTask.cs +++ b/Tranga/TrangaTask.cs @@ -24,8 +24,9 @@ public abstract class TrangaTask public string taskId { get; set; } [Newtonsoft.Json.JsonIgnore]public ExecutionState state { get; set; } [Newtonsoft.Json.JsonIgnore]protected HashSet childTasks { get; } - [Newtonsoft.Json.JsonIgnore]public double progress => childTasks.Count > 0 ? childTasks.Sum(childTask => childTask.progress) / childTasks.Count : _myProgress; - [Newtonsoft.Json.JsonIgnore]private double _myProgress = 0; + [Newtonsoft.Json.JsonIgnore]public TrangaTask? parentTask { get; set; } + public string? parentTaskId { get; set; } + [Newtonsoft.Json.JsonIgnore]public double progress { get; private set; } [Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence); [Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; } @@ -39,7 +40,7 @@ public abstract class TrangaTask public enum ExecutionState { Waiting, Enqueued, Running } - protected TrangaTask(Task task, TimeSpan reoccurrence) + protected TrangaTask(Task task, TimeSpan reoccurrence, TrangaTask? parentTask = null) { this.reoccurrence = reoccurrence; this.lastExecuted = DateTime.Now.Subtract(reoccurrence); @@ -48,6 +49,8 @@ public abstract class TrangaTask this.lastChange = DateTime.MaxValue; this.taskId = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(this.executionStarted.ToString(CultureInfo.InvariantCulture))); this.childTasks = new(); + this.parentTask = parentTask; + this.parentTaskId = parentTask?.taskId; } /// @@ -82,16 +85,24 @@ public abstract class TrangaTask throw new ArgumentException($"Task {failed} is not childTask of {this}"); this.childTasks.Remove(failed); this.childTasks.Add(newTask); + this.DecrementProgress(failed.progress); } public void AddChildTask(DownloadChapterTask childTask) { this.childTasks.Add(childTask); } - + public void IncrementProgress(double amount) { - this._myProgress += amount; + this.progress += amount / (childTasks.Count > 0 ? childTasks.Count : 1); + parentTask?.IncrementProgress(amount); + } + + public void DecrementProgress(double amount) + { + this.progress -= amount / childTasks.Count > 0 ? childTasks.Count : 1; + parentTask?.DecrementProgress(amount); } public enum Task : byte diff --git a/Tranga/TrangaTasks/DownloadChapterTask.cs b/Tranga/TrangaTasks/DownloadChapterTask.cs index 2e1bf4d..f320139 100644 --- a/Tranga/TrangaTasks/DownloadChapterTask.cs +++ b/Tranga/TrangaTasks/DownloadChapterTask.cs @@ -9,20 +9,13 @@ public class DownloadChapterTask : TrangaTask public Publication publication { get; } public string language { get; } public Chapter chapter { get; } - [JsonIgnore]public DownloadNewChaptersTask? parentTask { get; set; } - public string? parentTaskId { get; set; } - [JsonIgnore]public new double progress { get; private set; } - - public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", DownloadNewChaptersTask? parentTask = null) : base(task, TimeSpan.Zero) + public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", TrangaTask? parentTask = null) : base(task, TimeSpan.Zero, parentTask) { this.chapter = chapter; this.connectorName = connectorName; this.publication = publication; this.language = language; - this.parentTask = parentTask; - this.parentTaskId = parentTask?.taskId; - this.progress = 0; } protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)