Added CancellationToken to TrangaTask #14
This commit is contained in:
parent
32f89f9dce
commit
1c94625840
@ -127,7 +127,8 @@ public abstract class Connector
|
|||||||
/// <param name="publication">Publication that contains Chapter</param>
|
/// <param name="publication">Publication that contains Chapter</param>
|
||||||
/// <param name="chapter">Chapter with Images to retrieve</param>
|
/// <param name="chapter">Chapter with Images to retrieve</param>
|
||||||
/// <param name="parentTask">Will be used for progress-tracking</param>
|
/// <param name="parentTask">Will be used for progress-tracking</param>
|
||||||
public abstract void DownloadChapter(Publication publication, Chapter chapter, DownloadChapterTask parentTask);
|
/// <param name="cancellationToken"></param>
|
||||||
|
public abstract void DownloadChapter(Publication publication, Chapter chapter, DownloadChapterTask parentTask, CancellationToken? cancellationToken = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Copies the already downloaded cover from cache to downloadLocation
|
/// Copies the already downloaded cover from cache to downloadLocation
|
||||||
@ -227,8 +228,10 @@ public abstract class Connector
|
|||||||
/// <param name="comicInfoPath">Path of the generate Chapter ComicInfo.xml, if it was generated</param>
|
/// <param name="comicInfoPath">Path of the generate Chapter ComicInfo.xml, if it was generated</param>
|
||||||
/// <param name="requestType">RequestType for RateLimits</param>
|
/// <param name="requestType">RequestType for RateLimits</param>
|
||||||
/// <param name="referrer">Used in http request header</param>
|
/// <param name="referrer">Used in http request header</param>
|
||||||
protected void DownloadChapterImages(string[] imageUrls, string saveArchiveFilePath, byte requestType, DownloadChapterTask parentTask, string? comicInfoPath = null, string? referrer = null)
|
protected void DownloadChapterImages(string[] imageUrls, string saveArchiveFilePath, byte requestType, DownloadChapterTask parentTask, string? comicInfoPath = null, string? referrer = null, CancellationToken? cancellationToken = null)
|
||||||
{
|
{
|
||||||
|
if (cancellationToken?.IsCancellationRequested??false)
|
||||||
|
return;
|
||||||
logger?.WriteLine("Connector", $"Downloading Images for {saveArchiveFilePath}");
|
logger?.WriteLine("Connector", $"Downloading Images for {saveArchiveFilePath}");
|
||||||
//Check if Publication Directory already exists
|
//Check if Publication Directory already exists
|
||||||
string directoryPath = Path.GetDirectoryName(saveArchiveFilePath)!;
|
string directoryPath = Path.GetDirectoryName(saveArchiveFilePath)!;
|
||||||
@ -250,6 +253,8 @@ public abstract class Connector
|
|||||||
logger?.WriteLine("Connector", $"Downloading Image {chapter + 1:000}/{imageUrls.Length:000} {parentTask.publication.sortName} {parentTask.publication.internalId} Vol.{parentTask.chapter.volumeNumber} Ch.{parentTask.chapter.chapterNumber} {parentTask.progress:P2}");
|
logger?.WriteLine("Connector", $"Downloading Image {chapter + 1:000}/{imageUrls.Length:000} {parentTask.publication.sortName} {parentTask.publication.internalId} Vol.{parentTask.chapter.volumeNumber} Ch.{parentTask.chapter.chapterNumber} {parentTask.progress:P2}");
|
||||||
DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}.{extension}"), requestType, referrer);
|
DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}.{extension}"), requestType, referrer);
|
||||||
parentTask.IncrementProgress(1f / imageUrls.Length);
|
parentTask.IncrementProgress(1f / imageUrls.Length);
|
||||||
|
if (cancellationToken?.IsCancellationRequested??false)
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(comicInfoPath is not null)
|
if(comicInfoPath is not null)
|
||||||
|
@ -207,8 +207,10 @@ public class MangaDex : Connector
|
|||||||
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray();
|
return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void DownloadChapter(Publication publication, Chapter chapter, DownloadChapterTask parentTask)
|
public override void DownloadChapter(Publication publication, Chapter chapter, DownloadChapterTask parentTask, CancellationToken? cancellationToken = null)
|
||||||
{
|
{
|
||||||
|
if (cancellationToken?.IsCancellationRequested??false)
|
||||||
|
return;
|
||||||
logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}");
|
logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}");
|
||||||
//Request URLs for Chapter-Images
|
//Request URLs for Chapter-Images
|
||||||
DownloadClient.RequestResult requestResult =
|
DownloadClient.RequestResult requestResult =
|
||||||
@ -231,7 +233,7 @@ public class MangaDex : Connector
|
|||||||
File.WriteAllText(comicInfoPath, GetComicInfoXmlString(publication, chapter, logger));
|
File.WriteAllText(comicInfoPath, GetComicInfoXmlString(publication, chapter, logger));
|
||||||
|
|
||||||
//Download Chapter-Images
|
//Download Chapter-Images
|
||||||
DownloadChapterImages(imageUrls.ToArray(), GetArchiveFilePath(publication, chapter), (byte)RequestType.AtHomeServer, parentTask, comicInfoPath);
|
DownloadChapterImages(imageUrls.ToArray(), GetArchiveFilePath(publication, chapter), (byte)RequestType.AtHomeServer, parentTask, comicInfoPath, cancellationToken:cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string? GetCoverUrl(string publicationId, string? posterId)
|
private string? GetCoverUrl(string publicationId, string? posterId)
|
||||||
|
@ -169,8 +169,10 @@ public class Manganato : Connector
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void DownloadChapter(Publication publication, Chapter chapter, DownloadChapterTask parentTask)
|
public override void DownloadChapter(Publication publication, Chapter chapter, DownloadChapterTask parentTask, CancellationToken? cancellationToken = null)
|
||||||
{
|
{
|
||||||
|
if (cancellationToken?.IsCancellationRequested??false)
|
||||||
|
return;
|
||||||
logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}");
|
logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}");
|
||||||
string requestUrl = chapter.url;
|
string requestUrl = chapter.url;
|
||||||
DownloadClient.RequestResult requestResult =
|
DownloadClient.RequestResult requestResult =
|
||||||
@ -183,7 +185,7 @@ public class Manganato : Connector
|
|||||||
string comicInfoPath = Path.GetTempFileName();
|
string comicInfoPath = Path.GetTempFileName();
|
||||||
File.WriteAllText(comicInfoPath, GetComicInfoXmlString(publication, chapter, logger));
|
File.WriteAllText(comicInfoPath, GetComicInfoXmlString(publication, chapter, logger));
|
||||||
|
|
||||||
DownloadChapterImages(imageUrls, GetArchiveFilePath(publication, chapter), (byte)1, parentTask, comicInfoPath, "https://chapmanganato.com/");
|
DownloadChapterImages(imageUrls, GetArchiveFilePath(publication, chapter), (byte)1, parentTask, comicInfoPath, "https://chapmanganato.com/", cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string[] ParseImageUrlsFromHtml(Stream html)
|
private string[] ParseImageUrlsFromHtml(Stream html)
|
||||||
|
@ -209,16 +209,20 @@ public class Mangasee : Connector
|
|||||||
return ret.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray();
|
return ret.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, chapterNumberFormatInfo)).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void DownloadChapter(Publication publication, Chapter chapter, DownloadChapterTask parentTask)
|
public override void DownloadChapter(Publication publication, Chapter chapter, DownloadChapterTask parentTask, CancellationToken? cancellationToken = null)
|
||||||
{
|
{
|
||||||
while (this._browser is null)
|
if (cancellationToken?.IsCancellationRequested??false)
|
||||||
|
return;
|
||||||
|
while (this._browser is null && !(cancellationToken?.IsCancellationRequested??false))
|
||||||
{
|
{
|
||||||
logger?.WriteLine(this.GetType().ToString(), "Waiting for headless browser to download...");
|
logger?.WriteLine(this.GetType().ToString(), "Waiting for headless browser to download...");
|
||||||
Thread.Sleep(1000);
|
Thread.Sleep(1000);
|
||||||
}
|
}
|
||||||
|
if (cancellationToken?.IsCancellationRequested??false)
|
||||||
|
return;
|
||||||
|
|
||||||
logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}");
|
logger?.WriteLine(this.GetType().ToString(), $"Downloading Chapter-Info {publication.sortName} {publication.internalId} {chapter.volumeNumber}-{chapter.chapterNumber}");
|
||||||
IPage page = _browser.NewPageAsync().Result;
|
IPage page = _browser!.NewPageAsync().Result;
|
||||||
IResponse response = page.GoToAsync(chapter.url).Result;
|
IResponse response = page.GoToAsync(chapter.url).Result;
|
||||||
if (response.Ok)
|
if (response.Ok)
|
||||||
{
|
{
|
||||||
@ -234,7 +238,7 @@ public class Mangasee : Connector
|
|||||||
string comicInfoPath = Path.GetTempFileName();
|
string comicInfoPath = Path.GetTempFileName();
|
||||||
File.WriteAllText(comicInfoPath, GetComicInfoXmlString(publication, chapter, logger));
|
File.WriteAllText(comicInfoPath, GetComicInfoXmlString(publication, chapter, logger));
|
||||||
|
|
||||||
DownloadChapterImages(urls.ToArray(), GetArchiveFilePath(publication, chapter), (byte)1, parentTask, comicInfoPath);
|
DownloadChapterImages(urls.ToArray(), GetArchiveFilePath(publication, chapter), (byte)1, parentTask, comicInfoPath, cancellationToken:cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -129,16 +129,18 @@ public class TaskManager
|
|||||||
{
|
{
|
||||||
logger?.WriteLine(this.GetType().ToString(), $"Disposing failed task {removeTask.Key}.");
|
logger?.WriteLine(this.GetType().ToString(), $"Disposing failed task {removeTask.Key}.");
|
||||||
removeTask.Key.parentTask?.DecrementProgress(removeTask.Key.progress);
|
removeTask.Key.parentTask?.DecrementProgress(removeTask.Key.progress);
|
||||||
//removeTask.Value.Dispose(); Currently not available, however since task is removed from _allTasks should work. Memory leak however...
|
removeTask.Value.Dispose();
|
||||||
toRemove.Add(removeTask.Key);
|
toRemove.Add(removeTask.Key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach (DownloadChapterTask taskToRemove in toRemove)
|
foreach (DownloadChapterTask taskToRemove in toRemove)
|
||||||
{
|
{
|
||||||
DeleteTask(taskToRemove);
|
DeleteTask(taskToRemove);
|
||||||
AddTask(new DownloadChapterTask(taskToRemove.task, taskToRemove.connectorName,
|
DownloadChapterTask newTask = new DownloadChapterTask(taskToRemove.task, taskToRemove.connectorName,
|
||||||
taskToRemove.publication, taskToRemove.chapter, taskToRemove.language,
|
taskToRemove.publication, taskToRemove.chapter, taskToRemove.language,
|
||||||
taskToRemove.parentTask));
|
taskToRemove.parentTask);
|
||||||
|
AddTask(newTask);
|
||||||
|
taskToRemove.parentTask?.ReplaceFailedChildTask(taskToRemove, newTask);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(allTasksWaitingLength != _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting))
|
if(allTasksWaitingLength != _allTasks.Count(task => task.state is TrangaTask.ExecutionState.Waiting))
|
||||||
@ -155,10 +157,11 @@ public class TaskManager
|
|||||||
public void ExecuteTaskNow(TrangaTask task)
|
public void ExecuteTaskNow(TrangaTask task)
|
||||||
{
|
{
|
||||||
task.state = TrangaTask.ExecutionState.Running;
|
task.state = TrangaTask.ExecutionState.Running;
|
||||||
|
CancellationToken cToken = new CancellationToken();
|
||||||
Task t = new(() =>
|
Task t = new(() =>
|
||||||
{
|
{
|
||||||
task.Execute(this, this.logger);
|
task.Execute(this, this.logger, cToken);
|
||||||
});
|
}, cToken);
|
||||||
if(task.GetType() == typeof(DownloadChapterTask))
|
if(task.GetType() == typeof(DownloadChapterTask))
|
||||||
_runningDownloadChapterTasks.Add((DownloadChapterTask)task, t);
|
_runningDownloadChapterTasks.Add((DownloadChapterTask)task, t);
|
||||||
t.Start();
|
t.Start();
|
||||||
|
@ -71,20 +71,22 @@ public abstract class TrangaTask
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="taskManager"></param>
|
/// <param name="taskManager"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
protected abstract void ExecuteTask(TaskManager taskManager, Logger? logger);
|
/// <param name="cancellationToken"></param>
|
||||||
|
protected abstract void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Execute the task
|
/// Execute the task
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="taskManager">Should be the parent taskManager</param>
|
/// <param name="taskManager">Should be the parent taskManager</param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public void Execute(TaskManager taskManager, Logger? logger)
|
/// <param name="cancellationToken"></param>
|
||||||
|
public void Execute(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
|
||||||
{
|
{
|
||||||
logger?.WriteLine(this.GetType().ToString(), $"Executing Task {this}");
|
logger?.WriteLine(this.GetType().ToString(), $"Executing Task {this}");
|
||||||
this.state = ExecutionState.Running;
|
this.state = ExecutionState.Running;
|
||||||
this.executionStarted = DateTime.Now;
|
this.executionStarted = DateTime.Now;
|
||||||
this.lastChange = DateTime.Now;
|
this.lastChange = DateTime.Now;
|
||||||
ExecuteTask(taskManager, logger);
|
ExecuteTask(taskManager, logger, cancellationToken);
|
||||||
this.lastExecuted = DateTime.Now;
|
this.lastExecuted = DateTime.Now;
|
||||||
this.state = ExecutionState.Waiting;
|
this.state = ExecutionState.Waiting;
|
||||||
logger?.WriteLine(this.GetType().ToString(), $"Finished Executing Task {this}");
|
logger?.WriteLine(this.GetType().ToString(), $"Finished Executing Task {this}");
|
||||||
|
@ -20,10 +20,12 @@ public class DownloadChapterTask : TrangaTask
|
|||||||
this.parentTask = parentTask;
|
this.parentTask = parentTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ExecuteTask(TaskManager taskManager, Logger? logger)
|
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
|
||||||
{
|
{
|
||||||
|
if (cancellationToken?.IsCancellationRequested??false)
|
||||||
|
return;
|
||||||
Connector connector = taskManager.GetConnector(this.connectorName);
|
Connector connector = taskManager.GetConnector(this.connectorName);
|
||||||
connector.DownloadChapter(this.publication, this.chapter, this);
|
connector.DownloadChapter(this.publication, this.chapter, this, cancellationToken);
|
||||||
taskManager.DeleteTask(this);
|
taskManager.DeleteTask(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,46 +8,59 @@ public class DownloadNewChaptersTask : TrangaTask
|
|||||||
public string connectorName { get; }
|
public string connectorName { get; }
|
||||||
public Publication publication { get; }
|
public Publication publication { get; }
|
||||||
public string language { get; }
|
public string language { get; }
|
||||||
[JsonIgnore]private int childTaskAmount { get; set; }
|
[JsonIgnore]private HashSet<DownloadChapterTask> childTasks { get; }
|
||||||
|
|
||||||
public DownloadNewChaptersTask(Task task, string connectorName, Publication publication, TimeSpan reoccurrence, string language = "en") : base(task, reoccurrence)
|
public DownloadNewChaptersTask(Task task, string connectorName, Publication publication, TimeSpan reoccurrence, string language = "en") : base(task, reoccurrence)
|
||||||
{
|
{
|
||||||
this.connectorName = connectorName;
|
this.connectorName = connectorName;
|
||||||
this.publication = publication;
|
this.publication = publication;
|
||||||
this.language = language;
|
this.language = language;
|
||||||
childTaskAmount = 0;
|
this.childTasks = new();
|
||||||
}
|
}
|
||||||
|
|
||||||
public new float IncrementProgress(float amount)
|
public new float IncrementProgress(float amount)
|
||||||
{
|
{
|
||||||
this.progress += amount / this.childTaskAmount;
|
this.progress += amount / this.childTasks.Count;
|
||||||
this.lastChange = DateTime.Now;
|
this.lastChange = DateTime.Now;
|
||||||
return this.progress;
|
return this.progress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public new float DecrementProgress(float amount)
|
public new float DecrementProgress(float amount)
|
||||||
{
|
{
|
||||||
this.progress -= amount / this.childTaskAmount;
|
this.progress -= amount / this.childTasks.Count;
|
||||||
this.lastChange = DateTime.Now;
|
this.lastChange = DateTime.Now;
|
||||||
return this.progress;
|
return this.progress;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ExecuteTask(TaskManager taskManager, Logger? logger)
|
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
|
||||||
{
|
{
|
||||||
|
if (cancellationToken?.IsCancellationRequested??false)
|
||||||
|
return;
|
||||||
Publication pub = publication!;
|
Publication pub = publication!;
|
||||||
Connector connector = taskManager.GetConnector(this.connectorName);
|
Connector connector = taskManager.GetConnector(this.connectorName);
|
||||||
|
|
||||||
//Check if Publication already has a Folder
|
//Check if Publication already has a Folder
|
||||||
pub.CreatePublicationFolder(taskManager.settings.downloadLocation);
|
pub.CreatePublicationFolder(taskManager.settings.downloadLocation);
|
||||||
List<Chapter> newChapters = GetNewChaptersList(connector, pub, language!, ref taskManager.chapterCollection);
|
List<Chapter> newChapters = GetNewChaptersList(connector, pub, language!, ref taskManager.chapterCollection);
|
||||||
this.childTaskAmount = newChapters.Count;
|
|
||||||
|
|
||||||
connector.CopyCoverFromCacheToDownloadLocation(pub, taskManager.settings);
|
connector.CopyCoverFromCacheToDownloadLocation(pub, taskManager.settings);
|
||||||
|
|
||||||
pub.SaveSeriesInfoJson(connector.downloadLocation);
|
pub.SaveSeriesInfoJson(connector.downloadLocation);
|
||||||
|
|
||||||
foreach (Chapter newChapter in newChapters)
|
foreach (Chapter newChapter in newChapters)
|
||||||
taskManager.AddTask(new DownloadChapterTask(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ReplaceFailedChildTask(DownloadChapterTask failed, DownloadChapterTask newTask)
|
||||||
|
{
|
||||||
|
if (!this.childTasks.Contains(failed))
|
||||||
|
throw new ArgumentException($"Task {failed} is not childTask of {this}");
|
||||||
|
this.childTasks.Remove(failed);
|
||||||
|
this.childTasks.Add(newTask);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -8,8 +8,10 @@ public class UpdateLibrariesTask : TrangaTask
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ExecuteTask(TaskManager taskManager, Logger? logger)
|
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
|
||||||
{
|
{
|
||||||
|
if (cancellationToken?.IsCancellationRequested??false)
|
||||||
|
return;
|
||||||
foreach(LibraryManager lm in taskManager.settings.libraryManagers)
|
foreach(LibraryManager lm in taskManager.settings.libraryManagers)
|
||||||
lm.UpdateLibrary();
|
lm.UpdateLibrary();
|
||||||
this.progress = 1f;
|
this.progress = 1f;
|
||||||
|
Loading…
Reference in New Issue
Block a user