From abc66511d882443b752dee7cbd05d928dfbce23e Mon Sep 17 00:00:00 2001 From: glax Date: Wed, 21 Jun 2023 17:30:31 +0200 Subject: [PATCH] Fixed progress tracking this time for realsies. resolves #5 --- Tranga/TrangaTask.cs | 17 +++++++++++------ Tranga/TrangaTasks/DownloadChapterTask.cs | 3 +++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Tranga/TrangaTask.cs b/Tranga/TrangaTask.cs index 6f0f39a..8e142a0 100644 --- a/Tranga/TrangaTask.cs +++ b/Tranga/TrangaTask.cs @@ -25,11 +25,11 @@ public abstract class TrangaTask public string taskId { get; init; } [Newtonsoft.Json.JsonIgnore] public TrangaTask? parentTask { get; set; } public string? parentTaskId { get; set; } - [Newtonsoft.Json.JsonIgnore] protected HashSet childTasks { get; } + [Newtonsoft.Json.JsonIgnore] internal HashSet childTasks { get; } public double progress => GetProgress(); [Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; } - [Newtonsoft.Json.JsonIgnore]public DateTime lastChange { get; private set; } - [Newtonsoft.Json.JsonIgnore]public DateTime executionApproximatelyFinished => progress != 0 ? lastChange.Add(GetRemainingTime()) : DateTime.MaxValue; + [Newtonsoft.Json.JsonIgnore]public DateTime lastChange { get; internal set; } + [Newtonsoft.Json.JsonIgnore]public DateTime executionApproximatelyFinished => lastChange.Add(GetRemainingTime()); public TimeSpan executionApproximatelyRemaining => executionApproximatelyFinished.Subtract(DateTime.Now); [Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence); @@ -72,11 +72,16 @@ public abstract class TrangaTask this.state = ExecutionState.Running; this.executionStarted = DateTime.Now; this.lastChange = DateTime.Now; + if(parentTask is not null && parentTask.childTasks.All(ct => ct.state is ExecutionState.Waiting)) + parentTask.executionStarted = DateTime.Now; + HttpStatusCode statusCode = ExecuteTask(taskManager, logger, cancellationToken); + while(childTasks.Any(ct => ct.state is ExecutionState.Enqueued or ExecutionState.Running)) Thread.Sleep(1000); foreach(TrangaTask childTask in this.childTasks.ToArray()) taskManager.DeleteTask(childTask); + if ((int)statusCode >= 200 && (int)statusCode < 300) { this.lastExecuted = DateTime.Now; @@ -108,10 +113,10 @@ public abstract class TrangaTask private TimeSpan GetRemainingTime() { - if(progress == 0 || lastChange == DateTime.MaxValue || executionStarted == DateTime.UnixEpoch) - return TimeSpan.Zero; + if(progress == 0 || state is ExecutionState.Enqueued or ExecutionState.Waiting or ExecutionState.Failed || lastChange == DateTime.MaxValue) + return DateTime.MaxValue.Subtract(lastChange).Subtract(TimeSpan.FromHours(1)); TimeSpan elapsed = lastChange.Subtract(executionStarted); - return elapsed.Divide(progress).Subtract(elapsed); + return elapsed.Divide(progress).Multiply(1 - progress); } public enum Task : byte diff --git a/Tranga/TrangaTasks/DownloadChapterTask.cs b/Tranga/TrangaTasks/DownloadChapterTask.cs index c870be6..4fd4ef8 100644 --- a/Tranga/TrangaTasks/DownloadChapterTask.cs +++ b/Tranga/TrangaTasks/DownloadChapterTask.cs @@ -47,6 +47,9 @@ public class DownloadChapterTask : TrangaTask internal void IncrementProgress(double amount) { this._dctProgress += amount; + this.lastChange = DateTime.Now; + if(this.parentTask is not null) + this.parentTask.lastChange = DateTime.Now; } public override string ToString()