mirror of
https://github.com/C9Glax/tranga.git
synced 2025-01-12 03:17:33 +01:00
Fix infinite loop of DownloadNewChaptersTask
This commit is contained in:
parent
a4c9168551
commit
6cdccdf66b
@ -55,22 +55,25 @@ public class TaskManager
|
|||||||
private void TaskCheckerThread()
|
private void TaskCheckerThread()
|
||||||
{
|
{
|
||||||
logger?.WriteLine(this.GetType().ToString(), "Starting 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)
|
while (_continueRunning)
|
||||||
{
|
{
|
||||||
TrangaTask[] tmp = _allTasks.Where(taskQuery =>
|
foreach (TrangaTask waitingButExecute in _allTasks.Where(taskQuery =>
|
||||||
taskQuery.nextExecution < DateTime.Now &&
|
taskQuery.nextExecution < DateTime.Now &&
|
||||||
taskQuery.state is TrangaTask.ExecutionState.Waiting or TrangaTask.ExecutionState.Enqueued)
|
taskQuery.state is TrangaTask.ExecutionState.Waiting))
|
||||||
.OrderBy(tmpTask => tmpTask.nextExecution).ToArray();
|
{
|
||||||
foreach (TrangaTask task in tmp)
|
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)
|
switch (task.task)
|
||||||
{
|
{
|
||||||
case TrangaTask.Task.DownloadNewChapters:
|
case TrangaTask.Task.DownloadNewChapters:
|
||||||
if (!_allTasks.Any(taskQuery => taskQuery.task == TrangaTask.Task.DownloadNewChapters &&
|
if (!_allTasks.Any(taskQuery =>
|
||||||
taskQuery.state is TrangaTask.ExecutionState.Running &&
|
taskQuery.task == TrangaTask.Task.DownloadNewChapters &&
|
||||||
((DownloadNewChaptersTask)taskQuery).connectorName == ((DownloadNewChaptersTask)task).connectorName))
|
taskQuery.state is TrangaTask.ExecutionState.Running &&
|
||||||
|
((DownloadNewChaptersTask)taskQuery).connectorName == ((DownloadNewChaptersTask)task).connectorName))
|
||||||
{
|
{
|
||||||
ExecuteTaskNow(task);
|
ExecuteTaskNow(task);
|
||||||
}
|
}
|
||||||
@ -79,8 +82,7 @@ public class TaskManager
|
|||||||
if (!_allTasks.Any(taskQuery =>
|
if (!_allTasks.Any(taskQuery =>
|
||||||
taskQuery.task == TrangaTask.Task.DownloadChapter &&
|
taskQuery.task == TrangaTask.Task.DownloadChapter &&
|
||||||
taskQuery.state is TrangaTask.ExecutionState.Running &&
|
taskQuery.state is TrangaTask.ExecutionState.Running &&
|
||||||
((DownloadChapterTask)taskQuery).connectorName ==
|
((DownloadChapterTask)taskQuery).connectorName == ((DownloadChapterTask)task).connectorName))
|
||||||
((DownloadChapterTask)task).connectorName))
|
|
||||||
{
|
{
|
||||||
ExecuteTaskNow(task);
|
ExecuteTaskNow(task);
|
||||||
}
|
}
|
||||||
@ -91,7 +93,7 @@ public class TaskManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HashSet<DownloadChapterTask> toRemove = new();
|
HashSet<DownloadChapterTask> failedToRemove = new();
|
||||||
foreach (KeyValuePair<DownloadChapterTask, CancellationTokenSource> removeTask in _runningDownloadChapterTasks)
|
foreach (KeyValuePair<DownloadChapterTask, CancellationTokenSource> removeTask in _runningDownloadChapterTasks)
|
||||||
{
|
{
|
||||||
if (removeTask.Key.GetType() == typeof(DownloadChapterTask) &&
|
if (removeTask.Key.GetType() == typeof(DownloadChapterTask) &&
|
||||||
@ -99,22 +101,22 @@ public class TaskManager
|
|||||||
{
|
{
|
||||||
logger?.WriteLine(this.GetType().ToString(), $"Disposing failed task {removeTask.Key}.");
|
logger?.WriteLine(this.GetType().ToString(), $"Disposing failed task {removeTask.Key}.");
|
||||||
removeTask.Value.Cancel();
|
removeTask.Value.Cancel();
|
||||||
toRemove.Add(removeTask.Key);
|
failedToRemove.Add(removeTask.Key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach (DownloadChapterTask taskToRemove in toRemove)
|
foreach (DownloadChapterTask taskToRestart in failedToRemove)
|
||||||
{
|
{
|
||||||
DeleteTask(taskToRemove);
|
DeleteTask(taskToRestart);
|
||||||
DownloadChapterTask newTask = new (taskToRemove.task, taskToRemove.connectorName,
|
DownloadChapterTask newTask = new (taskToRestart.task, taskToRestart.connectorName,
|
||||||
taskToRemove.publication, taskToRemove.chapter, taskToRemove.language,
|
taskToRestart.publication, taskToRestart.chapter, taskToRestart.language,
|
||||||
(DownloadNewChaptersTask?)taskToRemove.parentTask);
|
(DownloadNewChaptersTask?)taskToRestart.parentTask);
|
||||||
AddTask(newTask);
|
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();
|
ExportDataAndSettings();
|
||||||
allTasksWaitingLength = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting);
|
waitingTasksCount = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting);
|
||||||
Thread.Sleep(1000);
|
Thread.Sleep(1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,8 +133,8 @@ public class TaskManager
|
|||||||
{
|
{
|
||||||
task.Execute(this, this.logger, cToken.Token);
|
task.Execute(this, this.logger, cToken.Token);
|
||||||
}, cToken.Token);
|
}, cToken.Token);
|
||||||
if(task.GetType() == typeof(DownloadChapterTask))
|
if(task is DownloadChapterTask chapterTask)
|
||||||
_runningDownloadChapterTasks.Add((DownloadChapterTask)task, cToken);
|
_runningDownloadChapterTasks.Add(chapterTask, cToken);
|
||||||
t.Start();
|
t.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ public abstract class TrangaTask
|
|||||||
this.executionStarted = DateTime.Now;
|
this.executionStarted = DateTime.Now;
|
||||||
this.lastChange = DateTime.Now;
|
this.lastChange = DateTime.Now;
|
||||||
ExecuteTask(taskManager, logger, cancellationToken);
|
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);
|
Thread.Sleep(1000);
|
||||||
this.lastExecuted = DateTime.Now;
|
this.lastExecuted = DateTime.Now;
|
||||||
this.state = ExecutionState.Waiting;
|
this.state = ExecutionState.Waiting;
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
using Logging;
|
using Logging;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Tranga.TrangaTasks;
|
namespace Tranga.TrangaTasks;
|
||||||
|
|
||||||
@ -33,12 +32,10 @@ public class DownloadNewChaptersTask : TrangaTask
|
|||||||
foreach (Chapter newChapter in newChapters)
|
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);
|
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()
|
public override string ToString()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user