2
0

Better way of handling progress, and childProgress.

This commit is contained in:
glax 2023-06-11 17:27:33 +02:00
parent b6f8c8aab5
commit 8e207c3119
3 changed files with 18 additions and 14 deletions

View File

@ -135,7 +135,7 @@ public class TaskManager
foreach (DownloadChapterTask taskToRemove in toRemove) foreach (DownloadChapterTask taskToRemove in toRemove)
{ {
DeleteTask(taskToRemove); DeleteTask(taskToRemove);
DownloadChapterTask newTask = new DownloadChapterTask(taskToRemove.task, taskToRemove.connectorName, DownloadChapterTask newTask = new (taskToRemove.task, taskToRemove.connectorName,
taskToRemove.publication, taskToRemove.chapter, taskToRemove.language, taskToRemove.publication, taskToRemove.chapter, taskToRemove.language,
taskToRemove.parentTask); taskToRemove.parentTask);
AddTask(newTask); AddTask(newTask);

View File

@ -24,8 +24,9 @@ public abstract class TrangaTask
public string taskId { get; set; } public string taskId { get; set; }
[Newtonsoft.Json.JsonIgnore]public ExecutionState state { get; set; } [Newtonsoft.Json.JsonIgnore]public ExecutionState state { get; set; }
[Newtonsoft.Json.JsonIgnore]protected HashSet<TrangaTask> childTasks { get; } [Newtonsoft.Json.JsonIgnore]protected HashSet<TrangaTask> childTasks { get; }
[Newtonsoft.Json.JsonIgnore]public double progress => childTasks.Count > 0 ? childTasks.Sum(childTask => childTask.progress) / childTasks.Count : _myProgress; [Newtonsoft.Json.JsonIgnore]public TrangaTask? parentTask { get; set; }
[Newtonsoft.Json.JsonIgnore]private double _myProgress = 0; 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 nextExecution => lastExecuted.Add(reoccurrence);
[Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; } [Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; }
@ -39,7 +40,7 @@ public abstract class TrangaTask
public enum ExecutionState { Waiting, Enqueued, Running } 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.reoccurrence = reoccurrence;
this.lastExecuted = DateTime.Now.Subtract(reoccurrence); this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
@ -48,6 +49,8 @@ public abstract class TrangaTask
this.lastChange = DateTime.MaxValue; this.lastChange = DateTime.MaxValue;
this.taskId = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(this.executionStarted.ToString(CultureInfo.InvariantCulture))); this.taskId = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(this.executionStarted.ToString(CultureInfo.InvariantCulture)));
this.childTasks = new(); this.childTasks = new();
this.parentTask = parentTask;
this.parentTaskId = parentTask?.taskId;
} }
/// <summary> /// <summary>
@ -82,16 +85,24 @@ public abstract class TrangaTask
throw new ArgumentException($"Task {failed} is not childTask of {this}"); throw new ArgumentException($"Task {failed} is not childTask of {this}");
this.childTasks.Remove(failed); this.childTasks.Remove(failed);
this.childTasks.Add(newTask); this.childTasks.Add(newTask);
this.DecrementProgress(failed.progress);
} }
public void AddChildTask(DownloadChapterTask childTask) public void AddChildTask(DownloadChapterTask childTask)
{ {
this.childTasks.Add(childTask); this.childTasks.Add(childTask);
} }
public void IncrementProgress(double amount) 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 public enum Task : byte

View File

@ -9,20 +9,13 @@ public class DownloadChapterTask : TrangaTask
public Publication publication { get; } public Publication publication { get; }
public string language { get; } public string language { get; }
public Chapter chapter { 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", TrangaTask? parentTask = null) : base(task, TimeSpan.Zero, parentTask)
public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", DownloadNewChaptersTask? parentTask = null) : base(task, TimeSpan.Zero)
{ {
this.chapter = chapter; this.chapter = chapter;
this.connectorName = connectorName; this.connectorName = connectorName;
this.publication = publication; this.publication = publication;
this.language = language; this.language = language;
this.parentTask = parentTask;
this.parentTaskId = parentTask?.taskId;
this.progress = 0;
} }
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null) protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)