Compare commits

...

5 Commits

6 changed files with 39 additions and 50 deletions

View File

@ -252,7 +252,7 @@ public abstract class Connector
string extension = split[^1];
logger?.WriteLine("Connector", $"Downloading Image {chapter + 1:000}/{imageUrls.Length:000} {parentTask.publication.sortName} {parentTask.publication.internalId} Vol.{parentTask.chapter.volumeNumber} Ch.{parentTask.chapter.chapterNumber} {parentTask.progress:P2}");
DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}.{extension}"), requestType, referrer);
parentTask.IncrementProgress(1f / imageUrls.Length);
parentTask.IncrementProgress(1.0 / imageUrls.Length);
if (cancellationToken?.IsCancellationRequested??false)
return;
}

View File

@ -428,6 +428,16 @@ public class TaskManager
this._allTasks = JsonConvert.DeserializeObject<HashSet<TrangaTask>>(buffer, new JsonSerializerSettings() { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!;
}
foreach (TrangaTask task in this._allTasks.Where(task => task.GetType() == typeof(DownloadChapterTask)))
{
DownloadChapterTask dcTask = (DownloadChapterTask)task;
IEnumerable<TrangaTask> dncTasks = this._allTasks.Where(pTask => pTask.GetType() == typeof(DownloadNewChaptersTask));
DownloadNewChaptersTask? parentTask = (DownloadNewChaptersTask?)dncTasks.FirstOrDefault(pTask => pTask.taskId.Equals(dcTask.parentTaskId));
dcTask.parentTask = parentTask;
parentTask?.AddChildTask(dcTask);
}
if (File.Exists(settings.knownPublicationsPath))
{
logger?.WriteLine(this.GetType().ToString(), $"Importing known publications from {settings.knownPublicationsPath}");

View File

@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System.Globalization;
using System.Text.Json.Serialization;
using Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@ -20,8 +21,9 @@ public abstract class TrangaTask
public TimeSpan reoccurrence { get; }
public DateTime lastExecuted { get; set; }
public Task task { get; }
public string taskId { get; set; }
[Newtonsoft.Json.JsonIgnore]public ExecutionState state { get; set; }
[Newtonsoft.Json.JsonIgnore]public float progress { get; protected set; }
[Newtonsoft.Json.JsonIgnore]public double progress { get; protected set; }
[Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence);
[Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; }
@ -33,7 +35,7 @@ public abstract class TrangaTask
[Newtonsoft.Json.JsonIgnore]
public TimeSpan executionApproximatelyRemaining => this.executionApproximatelyFinished.Subtract(DateTime.Now);
[Newtonsoft.Json.JsonIgnore]public DateTime lastChange { get; protected set; }
[Newtonsoft.Json.JsonIgnore]public DateTime lastChange { get; private set; }
public enum ExecutionState
{
@ -50,16 +52,17 @@ public abstract class TrangaTask
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 float IncrementProgress(float amount)
public double IncrementProgress(double amount)
{
this.progress += amount;
this.lastChange = DateTime.Now;
return this.progress;
}
public float DecrementProgress(float amount)
public double DecrementProgress(double amount)
{
this.progress -= amount;
this.lastChange = DateTime.Now;

View File

@ -9,7 +9,9 @@ public class DownloadChapterTask : TrangaTask
public Publication publication { get; }
public string language { get; }
public Chapter chapter { get; }
[JsonIgnore]public DownloadNewChaptersTask? parentTask { get; init; }
[JsonIgnore]public DownloadNewChaptersTask? parentTask { get; set; }
public string? parentTaskId { get; set; }
public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", DownloadNewChaptersTask? parentTask = null) : base(task, TimeSpan.Zero)
{
@ -18,25 +20,22 @@ public class DownloadChapterTask : TrangaTask
this.publication = publication;
this.language = language;
this.parentTask = parentTask;
this.parentTaskId = parentTask?.taskId;
}
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
{
if (cancellationToken?.IsCancellationRequested??false)
return;
if(this.parentTask is not null)
this.parentTask.state = ExecutionState.Running;
Connector connector = taskManager.GetConnector(this.connectorName);
connector.DownloadChapter(this.publication, this.chapter, this, cancellationToken);
if(this.parentTask is not null)
this.parentTask.state = ExecutionState.Waiting;
taskManager.DeleteTask(this);
}
public new float IncrementProgress(float amount)
{
this.progress += amount;
this.lastChange = DateTime.Now;
parentTask?.IncrementProgress(amount);
return this.progress;
}
public override string ToString()
{
return $"{base.ToString()}, {connectorName}, {publication.sortName} {publication.internalId}, Vol.{chapter.volumeNumber} Ch.{chapter.chapterNumber}";

View File

@ -9,6 +9,7 @@ public class DownloadNewChaptersTask : TrangaTask
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)
{
@ -18,20 +19,6 @@ public class DownloadNewChaptersTask : TrangaTask
this.childTasks = new();
}
public new float IncrementProgress(float amount)
{
this.progress += amount / this.childTasks.Count;
this.lastChange = DateTime.Now;
return this.progress;
}
public new float DecrementProgress(float amount)
{
this.progress -= amount / this.childTasks.Count;
this.lastChange = DateTime.Now;
return this.progress;
}
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
{
if (cancellationToken?.IsCancellationRequested??false)
@ -63,6 +50,11 @@ public class DownloadNewChaptersTask : TrangaTask
this.childTasks.Add(newTask);
}
public void AddChildTask(DownloadChapterTask childTask)
{
this.childTasks.Add(childTask);
}
/// <summary>
/// Updates the available Chapters of a Publication
/// </summary>

View File

@ -367,14 +367,8 @@ function ShowTasksQueue(){
tagTasksRunning.innerText = json.length;
json.forEach(task => {
downloadTasksOutput.appendChild(CreateProgressChild(task));
if(task.chapter != undefined){
document.querySelector(`#progress${task.publication.internalId.replace('=','')}-${task.chapter.chapterNumber}`).value = task.progress;
document.querySelector(`#progressStr${task.publication.internalId.replace('=','')}-${task.chapter.chapterNumber}`).innerText = task.progress.toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2});
}else{
document.querySelector(`#progress${task.publication.internalId.replace('=','')}`).value = task.progress;
document.querySelector(`#progressStr${task.publication.internalId.replace('=','')}`).innerText = task.progress.toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2});
}
document.querySelector(`#progress${task.taskId.replaceAll('=','')}`).value = task.progress;
document.querySelector(`#progressStr${task.taskId.replaceAll('=','')}`).innerText = task.progress.toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2});
});
});
@ -401,11 +395,13 @@ function CreateProgressChild(task){
var progress = document.createElement("progress");
progress.id = `progress${task.taskId.replaceAll('=','')}`;
child.appendChild(progress);
var progressStr = document.createElement("span");
progressStr.innerText = "00.00%";
progressStr.className = "progressStr";
progressStr.id = `progressStr${task.taskId.replaceAll('=','')}`;
child.appendChild(progressStr);
if(task.chapter != undefined){
@ -418,12 +414,6 @@ function CreateProgressChild(task){
chapterName.className = "chapterName";
chapterName.innerText = task.chapter.name;
child.appendChild(chapterName);
progress.id = `progress${task.publication.internalId.replace('=','')}-${task.chapter.chapterNumber}`;
progressStr.id = `progressStr${task.publication.internalId.replace('=','')}-${task.chapter.chapterNumber}`;
}else{
progress.id = `progress${task.publication.internalId.replace('=','')}`;
progressStr.id = `progressStr${task.publication.internalId.replace('=','')}`;
}
@ -494,13 +484,8 @@ setInterval(() => {
setInterval(() => {
GetRunningTasks().then((json) => {
json.forEach(task => {
if(task.chapter != undefined){
document.querySelector(`#progress${task.publication.internalId.replace('=','')}-${task.chapter.chapterNumber}`).value = task.progress;
document.querySelector(`#progressStr${task.publication.internalId.replace('=','')}-${task.chapter.chapterNumber}`).innerText = task.progress.toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2});
}else{
document.querySelector(`#progress${task.publication.internalId.replace('=','')}`).value = task.progress;
document.querySelector(`#progressStr${task.publication.internalId.replace('=','')}`).innerText = task.progress.toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2});
}
document.querySelector(`#progress${task.taskId.replaceAll('=','')}`).value = task.progress;
document.querySelector(`#progressStr${task.taskId.replaceAll('=','')}`).innerText = task.progress.toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2});
});
});
},500);