9 Commits

Author SHA1 Message Date
1be10b310d Fix Regex Bug on downlaod volumes 2023-06-11 19:17:03 +02:00
a0469f3145 Cancel DownloadChapter-Task on removal 2023-06-11 19:16:05 +02:00
fcd81f03b3 resolves #17 no cover image 2023-06-11 19:05:08 +02:00
76604d84d8 Better way of handling progress, and childProgress. 2023-06-11 18:24:26 +02:00
af822febbe fixed nullable warning 2023-06-11 18:01:04 +02:00
8e207c3119 Better way of handling progress, and childProgress. 2023-06-11 17:27:33 +02:00
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
8 changed files with 101 additions and 89 deletions

View File

@ -82,10 +82,13 @@ public abstract class Connector
Convert.ToInt32(aCh.volumeNumber) >= start &&
Convert.ToInt32(aCh.volumeNumber) <= end).ToArray();
}
else if(singleResultRegex.IsMatch(volume))
else if (singleResultRegex.IsMatch(volume))
{
string volumeNumber = singleResultRegex.Match(volume).Value;
return availableChapters.Where(aCh =>
aCh.volumeNumber is not null &&
aCh.volumeNumber.Equals(volume, StringComparison.InvariantCultureIgnoreCase)).ToArray();
aCh.volumeNumber.Equals(volumeNumber, StringComparison.InvariantCultureIgnoreCase)).ToArray();
}
}
else if (chapterRegex.IsMatch(searchTerm))
@ -100,10 +103,13 @@ public abstract class Connector
Convert.ToInt32(aCh.chapterNumber) >= start &&
Convert.ToInt32(aCh.chapterNumber) <= end).ToArray();
}
else if(singleResultRegex.IsMatch(chapter))
else if (singleResultRegex.IsMatch(chapter))
{
string chapterNumber = singleResultRegex.Match(chapter).Value;
return availableChapters.Where(aCh =>
aCh.chapterNumber is not null &&
aCh.chapterNumber.Equals(chapter, StringComparison.InvariantCultureIgnoreCase)).ToArray();
aCh.chapterNumber.Equals(chapterNumber, StringComparison.InvariantCultureIgnoreCase)).ToArray();
}
}
else
{
@ -137,11 +143,11 @@ public abstract class Connector
/// <param name="settings">TrangaSettings</param>
public void CopyCoverFromCacheToDownloadLocation(Publication publication, TrangaSettings settings)
{
logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {publication.sortName} {publication.internalId}");
logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {publication.sortName} -> {publication.internalId}");
//Check if Publication already has a Folder and cover
string publicationFolder = publication.CreatePublicationFolder(downloadLocation);
DirectoryInfo dirInfo = new (publicationFolder);
if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover.")))
if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover", StringComparison.InvariantCultureIgnoreCase)))
{
logger?.WriteLine(this.GetType().ToString(), $"Cover exists {publication.sortName}");
return;

View File

@ -98,7 +98,7 @@ public class MangaDex : Connector
{
JsonArray relationships = manga["relationships"]!.AsArray();
posterId = relationships.FirstOrDefault(relationship => relationship!["type"]!.GetValue<string>() == "cover_art")!["id"]!.GetValue<string>();
foreach (JsonNode node in relationships.Where(relationship =>
foreach (JsonNode? node in relationships.Where(relationship =>
relationship!["type"]!.GetValue<string>() == "author"))
authorIds.Add(node!["id"]!.GetValue<string>());
}

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);
}
@ -136,9 +135,9 @@ 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);
(DownloadNewChaptersTask?)taskToRemove.parentTask);
AddTask(newTask);
taskToRemove.parentTask?.ReplaceFailedChildTask(taskToRemove, newTask);
}
@ -208,7 +207,10 @@ public class TaskManager
logger?.WriteLine(this.GetType().ToString(), $"Removing Task {removeTask}");
_allTasks.Remove(removeTask);
if (removeTask.GetType() == typeof(DownloadChapterTask))
{
_runningDownloadChapterTasks[(DownloadChapterTask)removeTask].Cancel();
_runningDownloadChapterTasks.Remove((DownloadChapterTask)removeTask);
}
}
public TrangaTask? AddTask(TrangaTask.Task taskType, string? connectorName, string? internalId,
@ -264,11 +266,12 @@ public class TaskManager
_allTasks.RemoveWhere(mTask =>
mTask.GetType() == typeof(DownloadNewChaptersTask) &&
((DownloadNewChaptersTask)mTask).publication.internalId == publicationId &&
((DownloadNewChaptersTask)mTask).connectorName == connectorName!);
_allTasks.RemoveWhere(mTask =>
mTask.GetType() == typeof(DownloadChapterTask) &&
((DownloadChapterTask)mTask).publication.internalId == publicationId &&
((DownloadChapterTask)mTask).connectorName == connectorName!);
((DownloadNewChaptersTask)mTask).connectorName == connectorName);
foreach(TrangaTask rTask in _allTasks.Where(mTask =>
mTask.GetType() == typeof(DownloadChapterTask) &&
((DownloadChapterTask)mTask).publication.internalId == publicationId &&
((DownloadChapterTask)mTask).connectorName == connectorName))
DeleteTask(rTask);
}
break;
}
@ -428,13 +431,14 @@ 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)))
foreach (TrangaTask task in this._allTasks.Where(tTask => tTask.parentTaskId is not null))
{
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);
TrangaTask? parentTask = this._allTasks.FirstOrDefault(pTask => pTask.taskId == task.parentTaskId);
if (parentTask is not null)
{
task.parentTask = parentTask;
parentTask.AddChildTask(task);
}
}

View File

@ -22,51 +22,30 @@ public abstract class TrangaTask
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 double progress { get; protected set; }
[Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence);
[Newtonsoft.Json.JsonIgnore] public ExecutionState state { get; set; }
[Newtonsoft.Json.JsonIgnore] protected HashSet<TrangaTask> childTasks { get; }
[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 executionStarted { get; private set; }
[Newtonsoft.Json.JsonIgnore]
public DateTime executionApproximatelyFinished => this.progress != 0
? this.executionStarted.Add(DateTime.Now.Subtract(this.executionStarted) / this.progress)
: DateTime.MaxValue;
[Newtonsoft.Json.JsonIgnore]
public TimeSpan executionApproximatelyRemaining => this.executionApproximatelyFinished.Subtract(DateTime.Now);
[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 TimeSpan executionApproximatelyRemaining => executionApproximatelyFinished.Subtract(DateTime.Now);
[Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence);
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.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();
this.parentTask = parentTask;
this.parentTaskId = parentTask?.taskId;
}
/// <summary>
@ -95,10 +74,44 @@ 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);
this.DecrementProgress(failed.progress);
}
public void AddChildTask(TrangaTask childTask)
{
this.childTasks.Add(childTask);
}
public void IncrementProgress(double amount)
{
this.lastChange = DateTime.Now;
this.progress += amount / (childTasks.Count > 0 ? childTasks.Count : 1);
if (parentTask is not null)
{
parentTask.IncrementProgress(amount);
parentTask.state = ExecutionState.Running;
}
}
public void DecrementProgress(double amount)
{
this.lastChange = DateTime.Now;
this.progress -= amount / childTasks.Count > 0 ? childTasks.Count : 1;
parentTask?.DecrementProgress(amount);
}
private TimeSpan GetRemainingTime()
{
if(progress == 0)
return DateTime.MaxValue.Subtract(DateTime.Now);
TimeSpan elapsed = lastChange.Subtract(executionStarted);
return elapsed.Divide(progress).Subtract(elapsed);
}
public enum Task : byte

View File

@ -9,18 +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; }
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", DownloadNewChaptersTask? 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;
}
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
@ -30,6 +25,7 @@ public class DownloadChapterTask : TrangaTask
if(this.parentTask is not null)
this.parentTask.state = ExecutionState.Running;
Connector connector = taskManager.GetConnector(this.connectorName);
connector.CopyCoverFromCacheToDownloadLocation(this.publication, taskManager.settings);
connector.DownloadChapter(this.publication, this.chapter, this, cancellationToken);
if(this.parentTask is not null)
this.parentTask.state = ExecutionState.Waiting;

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)
@ -36,25 +32,12 @@ public class DownloadNewChaptersTask : TrangaTask
foreach (Chapter newChapter in newChapters)
{
DownloadChapterTask newTask = new (Task.DownloadChapter, this.connectorName!, pub, newChapter, this.language, this);
DownloadChapterTask newTask = new (Task.DownloadChapter, this.connectorName, pub, newChapter, this.language, this);
taskManager.AddTask(newTask);
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
/// </summary>

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);