2
0

Compare commits

...

3 Commits

Author SHA1 Message Date
b6f8c8aab5 TaskType check 2023-06-11 17:05:24 +02:00
36f7cbd3e9 Better way of handling progress, and childProgress.
More reliable taskFinishTime
2023-06-11 17:04:33 +02:00
3b2643d949 Website show remaining time instead of percentage 2023-06-11 16:38:12 +02:00
6 changed files with 41 additions and 54 deletions

View File

@ -128,7 +128,6 @@ public class TaskManager
DateTime.Now.Subtract(removeTask.Key.lastChange) > TimeSpan.FromMinutes(3))//3 Minutes since last update to task -> remove
{
logger?.WriteLine(this.GetType().ToString(), $"Disposing failed task {removeTask.Key}.");
removeTask.Key.parentTask?.DecrementProgress(removeTask.Key.progress);
removeTask.Value.Cancel();
toRemove.Add(removeTask.Key);
}

View File

@ -23,50 +23,31 @@ public abstract class TrangaTask
public Task task { get; }
public string taskId { get; set; }
[Newtonsoft.Json.JsonIgnore]public ExecutionState state { get; set; }
[Newtonsoft.Json.JsonIgnore]public double progress { get; protected set; }
[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]private double _myProgress = 0;
[Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence);
[Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; }
[Newtonsoft.Json.JsonIgnore]
public DateTime executionApproximatelyFinished => this.progress != 0
? this.executionStarted.Add(DateTime.Now.Subtract(this.executionStarted) / this.progress)
? this.executionStarted.Add(DateTime.Now.Subtract(this.executionStarted).Divide(this.progress))
: DateTime.MaxValue;
[Newtonsoft.Json.JsonIgnore]
public TimeSpan executionApproximatelyRemaining => this.executionApproximatelyFinished.Subtract(DateTime.Now);
[Newtonsoft.Json.JsonIgnore]public TimeSpan executionApproximatelyRemaining => this.executionApproximatelyFinished.Subtract(DateTime.Now);
[Newtonsoft.Json.JsonIgnore]public DateTime lastChange { get; private set; }
public enum ExecutionState
{
Waiting,
Enqueued,
Running
};
public enum ExecutionState { Waiting, Enqueued, Running }
protected TrangaTask(Task task, TimeSpan reoccurrence)
{
this.reoccurrence = reoccurrence;
this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
this.task = task;
this.progress = 0f;
this.executionStarted = DateTime.Now;
this.lastChange = DateTime.MaxValue;
this.taskId = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(this.executionStarted.ToString(CultureInfo.InvariantCulture)));
}
public double IncrementProgress(double amount)
{
this.progress += amount;
this.lastChange = DateTime.Now;
return this.progress;
}
public double DecrementProgress(double amount)
{
this.progress -= amount;
this.lastChange = DateTime.Now;
return this.progress;
this.childTasks = new();
}
/// <summary>
@ -95,10 +76,22 @@ public abstract class TrangaTask
logger?.WriteLine(this.GetType().ToString(), $"Finished Executing Task {this}");
}
/// <returns>True if elapsed time since last execution is greater than set interval</returns>
public bool ShouldExecute()
public void ReplaceFailedChildTask(DownloadChapterTask failed, DownloadChapterTask newTask)
{
return nextExecution < DateTime.Now && state is ExecutionState.Waiting;
if (!this.childTasks.Contains(failed))
throw new ArgumentException($"Task {failed} is not childTask of {this}");
this.childTasks.Remove(failed);
this.childTasks.Add(newTask);
}
public void AddChildTask(DownloadChapterTask childTask)
{
this.childTasks.Add(childTask);
}
public void IncrementProgress(double amount)
{
this._myProgress += amount;
}
public enum Task : byte

View File

@ -11,6 +11,7 @@ public class DownloadChapterTask : TrangaTask
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)
@ -21,6 +22,7 @@ public class DownloadChapterTask : TrangaTask
this.language = language;
this.parentTask = parentTask;
this.parentTaskId = parentTask?.taskId;
this.progress = 0;
}
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)

View File

@ -8,15 +8,11 @@ public class DownloadNewChaptersTask : TrangaTask
public string connectorName { get; }
public Publication publication { get; }
public string language { get; }
[JsonIgnore]private HashSet<DownloadChapterTask> childTasks { get; }
[JsonIgnore]public new double progress => childTasks.Count > 0 ? childTasks.Sum(childTask => childTask.progress) / childTasks.Count : 0;
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;
this.childTasks = new();
}
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
@ -41,19 +37,6 @@ public class DownloadNewChaptersTask : TrangaTask
this.childTasks.Add(newTask);
}
}
public void ReplaceFailedChildTask(DownloadChapterTask failed, DownloadChapterTask newTask)
{
if (!this.childTasks.Contains(failed))
throw new ArgumentException($"Task {failed} is not childTask of {this}");
this.childTasks.Remove(failed);
this.childTasks.Add(newTask);
}
public void AddChildTask(DownloadChapterTask childTask)
{
this.childTasks.Add(childTask);
}
/// <summary>
/// Updates the available Chapters of a Publication

View File

@ -14,6 +14,6 @@ public class UpdateLibrariesTask : TrangaTask
return;
foreach(LibraryManager lm in taskManager.settings.libraryManagers)
lm.UpdateLibrary();
this.progress = 1f;
IncrementProgress(1);
}
}

View File

@ -366,9 +366,14 @@ function ShowTasksQueue(){
.then(json => {
tagTasksRunning.innerText = json.length;
json.forEach(task => {
downloadTasksOutput.appendChild(CreateProgressChild(task));
document.querySelector(`#progress${task.taskId.replaceAll('=','')}`).value = task.progress;
document.querySelector(`#progressStr${task.taskId.replaceAll('=','')}`).innerText = task.progress.toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2});
if(task.task == 2 || task.task == 4) {
downloadTasksOutput.appendChild(CreateProgressChild(task));
document.querySelector(`#progress${task.taskId.replaceAll('=', '')}`).value = task.progress;
var finishedHours = task.executionApproximatelyRemaining.split(':')[0];
var finishedMinutes = task.executionApproximatelyRemaining.split(':')[1];
var finishedSeconds = task.executionApproximatelyRemaining.split(':')[2].split('.')[0];
document.querySelector(`#progressStr${task.taskId.replaceAll('=', '')}`).innerText = `${finishedHours}:${finishedMinutes}:${finishedSeconds}`;
}
});
});
@ -399,7 +404,7 @@ function CreateProgressChild(task){
child.appendChild(progress);
var progressStr = document.createElement("span");
progressStr.innerText = "00.00%";
progressStr.innerText = " \t∞";
progressStr.className = "progressStr";
progressStr.id = `progressStr${task.taskId.replaceAll('=','')}`;
child.appendChild(progressStr);
@ -484,8 +489,13 @@ setInterval(() => {
setInterval(() => {
GetRunningTasks().then((json) => {
json.forEach(task => {
document.querySelector(`#progress${task.taskId.replaceAll('=','')}`).value = task.progress;
document.querySelector(`#progressStr${task.taskId.replaceAll('=','')}`).innerText = task.progress.toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2});
if(task.task == 2 || task.task == 4){
document.querySelector(`#progress${task.taskId.replaceAll('=','')}`).value = task.progress;
var finishedHours = task.executionApproximatelyRemaining.split(':')[0];
var finishedMinutes = task.executionApproximatelyRemaining.split(':')[1];
var finishedSeconds = task.executionApproximatelyRemaining.split(':')[2].split('.')[0];
document.querySelector(`#progressStr${task.taskId.replaceAll('=','')}`).innerText = `${finishedHours}:${finishedMinutes}:${finishedSeconds}`;
}
});
});
},500);