Compare commits

...

3 Commits

Author SHA1 Message Date
87eade10cf #40 task timeout criteria 2023-06-07 00:27:53 +02:00
1f3ac41b30 removed unnecessary cast 2023-06-07 00:24:58 +02:00
6a304bb330 #40 task timeout 2023-06-07 00:24:27 +02:00
4 changed files with 33 additions and 5 deletions

View File

@ -13,12 +13,14 @@ namespace Tranga;
public class TaskManager
{
public Dictionary<Publication, List<Chapter>> chapterCollection = new();
private HashSet<TrangaTask> _allTasks = new HashSet<TrangaTask>();
private HashSet<TrangaTask> _allTasks = new();
private bool _continueRunning = true;
private readonly Connector[] _connectors;
public TrangaSettings settings { get; }
private Logger? logger { get; }
private readonly Dictionary<DownloadChapterTask, Task> _runningDownloadChapterTasks = new();
/// <param name="downloadFolderPath">Local path to save data (Manga) to</param>
/// <param name="workingDirectory">Path to the working directory</param>
/// <param name="imageCachePath">Path to the cover-image cache</param>
@ -117,6 +119,25 @@ public class TaskManager
break;
}
}
HashSet<DownloadChapterTask> toRemove = new();
foreach (KeyValuePair<DownloadChapterTask,Task> removeTask in _runningDownloadChapterTasks)
{
if (removeTask.Key.GetType() == typeof(DownloadChapterTask) &&
DateTime.Now.Subtract(removeTask.Key.lastChange) > TimeSpan.FromMinutes(3))//3 Minutes since last update to task -> remove
{
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();
}

View File

@ -23,7 +23,7 @@ public abstract class TrangaTask
[Newtonsoft.Json.JsonIgnore]public ExecutionState state { get; set; }
[Newtonsoft.Json.JsonIgnore]public float progress { get; protected set; }
[Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence);
[Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; protected set; }
[Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; }
[Newtonsoft.Json.JsonIgnore]
public DateTime executionApproximatelyFinished => this.progress != 0
@ -33,6 +33,8 @@ public abstract class TrangaTask
[Newtonsoft.Json.JsonIgnore]
public TimeSpan executionApproximatelyRemaining => this.executionApproximatelyFinished.Subtract(DateTime.Now);
[Newtonsoft.Json.JsonIgnore]public DateTime lastChange { get; protected set; }
public enum ExecutionState
{
Waiting,
@ -47,11 +49,13 @@ public abstract class TrangaTask
this.task = task;
this.progress = 0f;
this.executionStarted = DateTime.Now;
this.lastChange = DateTime.Now;
}
public float IncrementProgress(float amount)
{
this.progress += amount;
this.lastChange = DateTime.Now;
return this.progress;
}

View File

@ -9,7 +9,7 @@ public class DownloadChapterTask : TrangaTask
public Publication publication { get; }
public string language { get; }
public Chapter chapter { get; }
[JsonIgnore]private DownloadNewChaptersTask? parentTask { get; init; }
[JsonIgnore]public DownloadNewChaptersTask? parentTask { get; init; }
public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", DownloadNewChaptersTask? parentTask = null) : base(task, TimeSpan.Zero)
{
@ -22,15 +22,15 @@ public class DownloadChapterTask : TrangaTask
protected override void ExecuteTask(TaskManager taskManager, Logger? logger)
{
Publication pub = (Publication)this.publication!;
Connector connector = taskManager.GetConnector(this.connectorName);
connector.DownloadChapter(pub, this.chapter, this);
connector.DownloadChapter(this.publication, this.chapter, this);
taskManager.DeleteTask(this);
}
public new float IncrementProgress(float amount)
{
this.progress += amount;
this.lastChange = DateTime.Now;
parentTask?.IncrementProgress(amount);
return this.progress;
}

View File

@ -21,6 +21,7 @@ public class DownloadNewChaptersTask : TrangaTask
public new float IncrementProgress(float amount)
{
this.progress += amount / this.childTaskAmount;
this.lastChange = DateTime.Now;
return this.progress;
}