Compare commits

..

No commits in common. "87eade10cf7fd54dc7a6d896a1132869b29a63ec" and "b0642d12511105aa51cc6c5337a0f5db8198db7f" have entirely different histories.

4 changed files with 5 additions and 33 deletions

View File

@ -13,14 +13,12 @@ namespace Tranga;
public class TaskManager 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 HashSet<TrangaTask>();
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, Task> _runningDownloadChapterTasks = new();
/// <param name="downloadFolderPath">Local path to save data (Manga) to</param> /// <param name="downloadFolderPath">Local path to save data (Manga) to</param>
/// <param name="workingDirectory">Path to the working directory</param> /// <param name="workingDirectory">Path to the working directory</param>
/// <param name="imageCachePath">Path to the cover-image cache</param> /// <param name="imageCachePath">Path to the cover-image cache</param>
@ -119,25 +117,6 @@ public class TaskManager
break; 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)) if(allTasksWaitingLength != _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting))
ExportDataAndSettings(); ExportDataAndSettings();
allTasksWaitingLength = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting); allTasksWaitingLength = _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting);
@ -156,8 +135,6 @@ public class TaskManager
{ {
task.Execute(this, this.logger); task.Execute(this, this.logger);
}); });
if(task.GetType() == typeof(DownloadChapterTask))
_runningDownloadChapterTasks.Add((DownloadChapterTask)task, t);
t.Start(); t.Start();
} }

View File

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

View File

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

View File

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