TaskManager _runningDownloadChapterTasks -> _runningTasks for all TrangaTasks

This commit is contained in:
glax 2023-06-27 22:58:40 +02:00
parent f66ab7d40b
commit b88890817e

View File

@ -13,13 +13,12 @@ public class TaskManager
{ {
public Dictionary<Publication, List<Chapter>> chapterCollection = new(); public Dictionary<Publication, List<Chapter>> chapterCollection = new();
private HashSet<TrangaTask> _allTasks = new(); private HashSet<TrangaTask> _allTasks = new();
private readonly Dictionary<TrangaTask, CancellationTokenSource> _runningTasks = new ();
private bool _continueRunning = true; private bool _continueRunning = true;
private readonly Connector[] _connectors; private readonly Connector[] _connectors;
public TrangaSettings settings { get; } public TrangaSettings settings { get; }
private Logger? logger { get; } private Logger? logger { get; }
private readonly Dictionary<DownloadChapterTask, CancellationTokenSource> _runningDownloadChapterTasks = new();
public TaskManager(TrangaSettings settings, Logger? logger = null) public TaskManager(TrangaSettings settings, Logger? logger = null)
{ {
this.logger = logger; this.logger = logger;
@ -86,17 +85,16 @@ public class TaskManager
foreach (TrangaTask timedOutTask in _allTasks foreach (TrangaTask timedOutTask in _allTasks
.Where(taskQuery => taskQuery.lastChange < DateTime.Now.Subtract(TimeSpan.FromMinutes(3)))) .Where(taskQuery => taskQuery.lastChange < DateTime.Now.Subtract(TimeSpan.FromMinutes(3))))
{ {
if(timedOutTask is DownloadChapterTask dct) _runningTasks[timedOutTask].Cancel();
_runningDownloadChapterTasks[dct].Cancel();
timedOutTask.state = TrangaTask.ExecutionState.Failed; timedOutTask.state = TrangaTask.ExecutionState.Failed;
} }
foreach (TrangaTask failedDownloadChapterTask in _allTasks.Where(taskQuery => foreach (TrangaTask failedTask in _allTasks.Where(taskQuery =>
taskQuery.state is TrangaTask.ExecutionState.Failed && taskQuery is DownloadChapterTask).ToArray()) taskQuery.state is TrangaTask.ExecutionState.Failed).ToArray())
{ {
DeleteTask(failedDownloadChapterTask); DeleteTask(failedTask);
TrangaTask newTask = failedDownloadChapterTask.Clone(); TrangaTask newTask = failedTask.Clone();
failedDownloadChapterTask.parentTask?.AddChildTask(newTask); failedTask.parentTask?.AddChildTask(newTask);
AddTask(newTask); AddTask(newTask);
} }
@ -119,8 +117,7 @@ public class TaskManager
{ {
task.Execute(this, this.logger, cToken.Token); task.Execute(this, this.logger, cToken.Token);
}, cToken.Token); }, cToken.Token);
if(task is DownloadChapterTask chapterTask) _runningTasks.Add(task, cToken);
_runningDownloadChapterTasks.Add(chapterTask, cToken);
t.Start(); t.Start();
} }
@ -157,12 +154,13 @@ public class TaskManager
public void DeleteTask(TrangaTask removeTask) public void DeleteTask(TrangaTask removeTask)
{ {
logger?.WriteLine(this.GetType().ToString(), $"Removing Task {removeTask}"); logger?.WriteLine(this.GetType().ToString(), $"Removing Task {removeTask}");
_allTasks.Remove(removeTask); if(_allTasks.Contains(removeTask))
_allTasks.Remove(removeTask);
removeTask.parentTask?.RemoveChildTask(removeTask); removeTask.parentTask?.RemoveChildTask(removeTask);
if (removeTask is DownloadChapterTask cRemoveTask && _runningDownloadChapterTasks.ContainsKey(cRemoveTask)) if (_runningTasks.ContainsKey(removeTask))
{ {
_runningDownloadChapterTasks[cRemoveTask].Cancel(); _runningTasks[removeTask].Cancel();
_runningDownloadChapterTasks.Remove(cRemoveTask); _runningTasks.Remove(removeTask);
} }
foreach(TrangaTask childTask in removeTask.childTasks) foreach(TrangaTask childTask in removeTask.childTasks)
DeleteTask(childTask); DeleteTask(childTask);