From 6a304bb330666d8899662bbdf7971b95a3dde27d Mon Sep 17 00:00:00 2001 From: glax Date: Wed, 7 Jun 2023 00:24:27 +0200 Subject: [PATCH] #40 task timeout --- Tranga/TaskManager.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 57473dd..077cf7b 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -19,6 +19,8 @@ public class TaskManager public TrangaSettings settings { get; } private Logger? logger { get; } + private readonly Dictionary _runningDownloadChapterTasks = new(); + /// Local path to save data (Manga) to /// Path to the working directory /// Path to the cover-image cache @@ -117,6 +119,25 @@ public class TaskManager break; } } + + HashSet toRemove = new(); + foreach (KeyValuePair removeTask in _runningDownloadChapterTasks) + { + if (removeTask.Key.GetType() == typeof(DownloadChapterTask) && + DateTime.Now.Subtract(removeTask.Key.executionStarted) > TimeSpan.FromMinutes(10))//TODO better way to check if task has failed? + { + logger?.WriteLine(this.GetType().ToString(), $"Removing failed task {removeTask}."); + removeTask.Value.Dispose(); + DeleteTask(removeTask.Key); + AddTask(new DownloadChapterTask(removeTask.Key.task, removeTask.Key.connectorName, + removeTask.Key.publication, removeTask.Key.chapter, removeTask.Key.language, + removeTask.Key.parentTask)); + toRemove.Add(removeTask.Key); + } + } + foreach (DownloadChapterTask taskToRemove in toRemove) + _runningDownloadChapterTasks.Remove(taskToRemove); + if(allTasksWaitingLength != _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting)) ExportDataAndSettings(); allTasksWaitingLength = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting); @@ -135,6 +156,8 @@ public class TaskManager { task.Execute(this, this.logger); }); + if(task.GetType() == typeof(DownloadChapterTask)) + _runningDownloadChapterTasks.Add((DownloadChapterTask)task, t); t.Start(); }