Compare commits
3 Commits
b0642d1251
...
87eade10cf
Author | SHA1 | Date | |
---|---|---|---|
87eade10cf | |||
1f3ac41b30 | |||
6a304bb330 |
@ -13,12 +13,14 @@ 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 HashSet<TrangaTask>();
|
private HashSet<TrangaTask> _allTasks = 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, 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>
|
||||||
@ -117,6 +119,25 @@ 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);
|
||||||
@ -135,6 +156,8 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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; protected set; }
|
[Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; }
|
||||||
|
|
||||||
[Newtonsoft.Json.JsonIgnore]
|
[Newtonsoft.Json.JsonIgnore]
|
||||||
public DateTime executionApproximatelyFinished => this.progress != 0
|
public DateTime executionApproximatelyFinished => this.progress != 0
|
||||||
@ -33,6 +33,8 @@ 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
|
||||||
{
|
{
|
||||||
Waiting,
|
Waiting,
|
||||||
@ -47,11 +49,13 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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]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)
|
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(pub, this.chapter, this);
|
connector.DownloadChapter(this.publication, 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;
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user