2
0

Fix infinite loop of DownloadNewChaptersTask

This commit is contained in:
glax 2023-06-19 22:32:32 +02:00
parent a4c9168551
commit 6cdccdf66b
3 changed files with 29 additions and 30 deletions

View File

@ -55,22 +55,25 @@ public class TaskManager
private void TaskCheckerThread()
{
logger?.WriteLine(this.GetType().ToString(), "Starting TaskCheckerThread.");
int allTasksWaitingLength = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting);
int waitingTasksCount = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting);
while (_continueRunning)
{
TrangaTask[] tmp = _allTasks.Where(taskQuery =>
taskQuery.nextExecution < DateTime.Now &&
taskQuery.state is TrangaTask.ExecutionState.Waiting or TrangaTask.ExecutionState.Enqueued)
.OrderBy(tmpTask => tmpTask.nextExecution).ToArray();
foreach (TrangaTask task in tmp)
foreach (TrangaTask waitingButExecute in _allTasks.Where(taskQuery =>
taskQuery.nextExecution < DateTime.Now &&
taskQuery.state is TrangaTask.ExecutionState.Waiting))
{
waitingButExecute.state = TrangaTask.ExecutionState.Enqueued;
}
foreach (TrangaTask task in _allTasks.Where(enqueuedTask => enqueuedTask.state is TrangaTask.ExecutionState.Enqueued))
{
task.state = TrangaTask.ExecutionState.Enqueued;
switch (task.task)
{
case TrangaTask.Task.DownloadNewChapters:
if (!_allTasks.Any(taskQuery => taskQuery.task == TrangaTask.Task.DownloadNewChapters &&
taskQuery.state is TrangaTask.ExecutionState.Running &&
((DownloadNewChaptersTask)taskQuery).connectorName == ((DownloadNewChaptersTask)task).connectorName))
if (!_allTasks.Any(taskQuery =>
taskQuery.task == TrangaTask.Task.DownloadNewChapters &&
taskQuery.state is TrangaTask.ExecutionState.Running &&
((DownloadNewChaptersTask)taskQuery).connectorName == ((DownloadNewChaptersTask)task).connectorName))
{
ExecuteTaskNow(task);
}
@ -79,8 +82,7 @@ public class TaskManager
if (!_allTasks.Any(taskQuery =>
taskQuery.task == TrangaTask.Task.DownloadChapter &&
taskQuery.state is TrangaTask.ExecutionState.Running &&
((DownloadChapterTask)taskQuery).connectorName ==
((DownloadChapterTask)task).connectorName))
((DownloadChapterTask)taskQuery).connectorName == ((DownloadChapterTask)task).connectorName))
{
ExecuteTaskNow(task);
}
@ -91,7 +93,7 @@ public class TaskManager
}
}
HashSet<DownloadChapterTask> toRemove = new();
HashSet<DownloadChapterTask> failedToRemove = new();
foreach (KeyValuePair<DownloadChapterTask, CancellationTokenSource> removeTask in _runningDownloadChapterTasks)
{
if (removeTask.Key.GetType() == typeof(DownloadChapterTask) &&
@ -99,22 +101,22 @@ public class TaskManager
{
logger?.WriteLine(this.GetType().ToString(), $"Disposing failed task {removeTask.Key}.");
removeTask.Value.Cancel();
toRemove.Add(removeTask.Key);
failedToRemove.Add(removeTask.Key);
}
}
foreach (DownloadChapterTask taskToRemove in toRemove)
foreach (DownloadChapterTask taskToRestart in failedToRemove)
{
DeleteTask(taskToRemove);
DownloadChapterTask newTask = new (taskToRemove.task, taskToRemove.connectorName,
taskToRemove.publication, taskToRemove.chapter, taskToRemove.language,
(DownloadNewChaptersTask?)taskToRemove.parentTask);
DeleteTask(taskToRestart);
DownloadChapterTask newTask = new (taskToRestart.task, taskToRestart.connectorName,
taskToRestart.publication, taskToRestart.chapter, taskToRestart.language,
(DownloadNewChaptersTask?)taskToRestart.parentTask);
AddTask(newTask);
taskToRemove.parentTask?.ReplaceFailedChildTask(taskToRemove, newTask);
taskToRestart.parentTask?.ReplaceFailedChildTask(taskToRestart, newTask);
}
if(allTasksWaitingLength != _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting))
if(waitingTasksCount != _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting))
ExportDataAndSettings();
allTasksWaitingLength = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting);
waitingTasksCount = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting);
Thread.Sleep(1000);
}
}
@ -131,8 +133,8 @@ public class TaskManager
{
task.Execute(this, this.logger, cToken.Token);
}, cToken.Token);
if(task.GetType() == typeof(DownloadChapterTask))
_runningDownloadChapterTasks.Add((DownloadChapterTask)task, cToken);
if(task is DownloadChapterTask chapterTask)
_runningDownloadChapterTasks.Add(chapterTask, cToken);
t.Start();
}

View File

@ -69,7 +69,7 @@ public abstract class TrangaTask
this.executionStarted = DateTime.Now;
this.lastChange = DateTime.Now;
ExecuteTask(taskManager, logger, cancellationToken);
while(this.childTasks.Any(childTask => childTask.state is ExecutionState.Running or ExecutionState.Enqueued))
while(childTasks.Any(ct => ct.state is ExecutionState.Enqueued or ExecutionState.Running))
Thread.Sleep(1000);
this.lastExecuted = DateTime.Now;
this.state = ExecutionState.Waiting;

View File

@ -1,5 +1,4 @@
using Logging;
using Newtonsoft.Json;
namespace Tranga.TrangaTasks;
@ -33,12 +32,10 @@ public class DownloadNewChaptersTask : TrangaTask
foreach (Chapter newChapter in newChapters)
{
DownloadChapterTask newTask = new (Task.DownloadChapter, this.connectorName, pub, newChapter, this.language, this);
taskManager.AddTask(newTask);
this.childTasks.Add(newTask);
newTask.state = ExecutionState.Enqueued;
taskManager.AddTask(newTask);
}
if(newChapters.Count > 0)
foreach(NotificationManager nm in taskManager.settings.notificationManagers)
nm.SendNotification(pub.sortName, $"Downloading {newChapters.Count} new Chapters.");
}
public override string ToString()