Removed TaskExecutor
TrangaTask is now abstract TrangaTask implements Execute Method, that is now called instead of TaskExecutor Created inheriting classes of TrangaTask: UpdateKomgaLibraryTask, DownloadNewChaptersTask
This commit is contained in:
parent
8619630269
commit
1b9ebd096b
@ -344,7 +344,7 @@ public static class Tranga_Cli
|
||||
}
|
||||
|
||||
Publication? publication = null;
|
||||
if (task != TrangaTask.Task.UpdatePublications && task != TrangaTask.Task.UpdateKomgaLibrary)
|
||||
if (task != TrangaTask.Task.UpdateKomgaLibrary)
|
||||
{
|
||||
publication = SelectPublication(taskManager, connector!, logger);
|
||||
if (publication is null)
|
||||
|
@ -1,127 +0,0 @@
|
||||
using Logging;
|
||||
|
||||
namespace Tranga;
|
||||
|
||||
/// <summary>
|
||||
/// Executes TrangaTasks
|
||||
/// Based on the TrangaTask.Task a method is called.
|
||||
/// The chapterCollection is updated with new Publications/Chapters.
|
||||
/// </summary>
|
||||
public static class TaskExecutor
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes TrangaTask.
|
||||
/// </summary>
|
||||
/// <param name="taskManager">Parent</param>
|
||||
/// <param name="trangaTask">Task to execute</param>
|
||||
/// <param name="chapterCollection">Current chapterCollection to update</param>
|
||||
/// <param name="logger"></param>
|
||||
/// <exception cref="ArgumentException">Is thrown when there is no Connector available with the name of the TrangaTask.connectorName</exception>
|
||||
public static void Execute(TaskManager taskManager, TrangaTask trangaTask, Logger? logger)
|
||||
{
|
||||
//Only execute task if it is not already being executed.
|
||||
if (trangaTask.state == TrangaTask.ExecutionState.Running)
|
||||
{
|
||||
logger?.WriteLine("TaskExecutor", $"Task already running {trangaTask}");
|
||||
return;
|
||||
}
|
||||
trangaTask.state = TrangaTask.ExecutionState.Running;
|
||||
logger?.WriteLine("TaskExecutor", $"Starting Task {trangaTask}");
|
||||
|
||||
//Connector is not needed for all tasks
|
||||
Connector? connector = null;
|
||||
if (trangaTask.task != TrangaTask.Task.UpdateKomgaLibrary)
|
||||
connector = taskManager.GetConnector(trangaTask.connectorName!);
|
||||
|
||||
//Call appropriate Method based on TrangaTask.Task
|
||||
switch (trangaTask.task)
|
||||
{
|
||||
case TrangaTask.Task.DownloadNewChapters:
|
||||
DownloadNewChapters(connector!, (Publication)trangaTask.publication!, trangaTask.language, ref taskManager._chapterCollection, taskManager.settings);
|
||||
break;
|
||||
case TrangaTask.Task.UpdateChapters:
|
||||
UpdateChapters(connector!, (Publication)trangaTask.publication!, trangaTask.language, ref taskManager._chapterCollection);
|
||||
break;
|
||||
case TrangaTask.Task.UpdatePublications:
|
||||
UpdatePublications(connector!, ref taskManager._chapterCollection);
|
||||
break;
|
||||
case TrangaTask.Task.UpdateKomgaLibrary:
|
||||
UpdateKomgaLibrary(taskManager);
|
||||
break;
|
||||
}
|
||||
|
||||
logger?.WriteLine("TaskExecutor", $"Task finished! {trangaTask}");
|
||||
trangaTask.lastExecuted = DateTime.Now;
|
||||
trangaTask.state = TrangaTask.ExecutionState.Waiting;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates all Komga-Libraries
|
||||
/// </summary>
|
||||
/// <param name="taskManager">Parent</param>
|
||||
private static void UpdateKomgaLibrary(TaskManager taskManager)
|
||||
{
|
||||
if (taskManager.komga is null)
|
||||
return;
|
||||
Komga komga = taskManager.komga;
|
||||
|
||||
Komga.KomgaLibrary[] allLibraries = komga.GetLibraries();
|
||||
foreach (Komga.KomgaLibrary lib in allLibraries)
|
||||
komga.UpdateLibrary(lib.id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the available Publications from a Connector (all of them)
|
||||
/// </summary>
|
||||
/// <param name="connector">Connector to receive Publications from</param>
|
||||
/// <param name="chapterCollection"></param>
|
||||
private static void UpdatePublications(Connector connector, ref Dictionary<Publication, List<Chapter>> chapterCollection)
|
||||
{
|
||||
Publication[] publications = connector.GetPublications();
|
||||
foreach (Publication publication in publications)
|
||||
chapterCollection.TryAdd(publication, new List<Chapter>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks for new Chapters and Downloads new ones.
|
||||
/// If no Chapters had been downloaded previously, download also cover and create series.json
|
||||
/// </summary>
|
||||
/// <param name="connector">Connector to use</param>
|
||||
/// <param name="publication">Publication to check</param>
|
||||
/// <param name="language">Language to receive chapters for</param>
|
||||
/// <param name="chapterCollection"></param>
|
||||
private static void DownloadNewChapters(Connector connector, Publication publication, string language, ref Dictionary<Publication, List<Chapter>> chapterCollection, TrangaSettings settings)
|
||||
{
|
||||
//Check if Publication already has a Folder
|
||||
string publicationFolder = Path.Join(connector.downloadLocation, publication.folderName);
|
||||
if(!Directory.Exists(publicationFolder))
|
||||
Directory.CreateDirectory(publicationFolder);
|
||||
List<Chapter> newChapters = UpdateChapters(connector, publication, language, ref chapterCollection);
|
||||
|
||||
connector.CopyCoverFromCacheToDownloadLocation(publication, settings);
|
||||
|
||||
publication.SaveSeriesInfoJson(connector.downloadLocation);
|
||||
|
||||
foreach(Chapter newChapter in newChapters)
|
||||
connector.DownloadChapter(publication, newChapter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the available Chapters of a Publication
|
||||
/// </summary>
|
||||
/// <param name="connector">Connector to use</param>
|
||||
/// <param name="publication">Publication to check</param>
|
||||
/// <param name="language">Language to receive chapters for</param>
|
||||
/// <param name="chapterCollection"></param>
|
||||
/// <returns>List of Chapters that were previously not in collection</returns>
|
||||
private static List<Chapter> UpdateChapters(Connector connector, Publication publication, string language, ref Dictionary<Publication, List<Chapter>> chapterCollection)
|
||||
{
|
||||
List<Chapter> newChaptersList = new();
|
||||
chapterCollection.TryAdd(publication, newChaptersList); //To ensure publication is actually in collection
|
||||
|
||||
Chapter[] newChapters = connector.GetChapters(publication, language);
|
||||
newChaptersList = newChapters.Where(nChapter => !connector.CheckChapterIsDownloaded(publication, nChapter)).ToList();
|
||||
|
||||
return newChaptersList;
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Tranga.Connectors;
|
||||
using Tranga.TrangaTasks;
|
||||
|
||||
namespace Tranga;
|
||||
|
||||
@ -116,9 +117,9 @@ public class TaskManager
|
||||
return;
|
||||
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Forcing Execution: {task}");
|
||||
Task t = new Task(() =>
|
||||
Task t = new(() =>
|
||||
{
|
||||
TaskExecutor.Execute(this, task, logger);
|
||||
task.Execute(this);
|
||||
});
|
||||
t.Start();
|
||||
}
|
||||
@ -137,37 +138,36 @@ public class TaskManager
|
||||
{
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Adding new Task {task} {connectorName} {publication?.sortName}");
|
||||
|
||||
TrangaTask newTask;
|
||||
TrangaTask newTask = null;
|
||||
if (task == TrangaTask.Task.UpdateKomgaLibrary)
|
||||
{
|
||||
newTask = new TrangaTask(task, null, null, reoccurrence);
|
||||
newTask = new UpdateKomgaLibraryTask(task, reoccurrence);
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Removing old {task}-Task.");
|
||||
//Only one UpdateKomgaLibrary Task
|
||||
_allTasks.RemoveWhere(trangaTask => trangaTask.task is TrangaTask.Task.UpdateKomgaLibrary);
|
||||
_allTasks.Add(newTask);
|
||||
}
|
||||
else
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Added new Task {newTask}");
|
||||
}else if (task == TrangaTask.Task.DownloadNewChapters)
|
||||
{
|
||||
if(connectorName is null)
|
||||
throw new ArgumentException($"connectorName can not be null for task {task}");
|
||||
|
||||
//Get appropriate Connector from available Connectors for TrangaTask
|
||||
Connector? connector = _connectors.FirstOrDefault(c => c.name == connectorName);
|
||||
if (connector is null)
|
||||
throw new ArgumentException($"Connector {connectorName} is not a known connector.");
|
||||
if (connectorName is null)
|
||||
throw new ArgumentException($"connectorName can not be null for task {task}");
|
||||
|
||||
newTask = new TrangaTask(task, connector.name, publication, reoccurrence, language);
|
||||
if (publication is null)
|
||||
throw new ArgumentException($"publication can not be null for task {task}");
|
||||
Publication pub = (Publication)publication;
|
||||
newTask = new DownloadNewChaptersTask(task, connector!.name, pub, reoccurrence, language);
|
||||
|
||||
//Check if same task already exists
|
||||
if (!_allTasks.Any(trangaTask => trangaTask.task == task && trangaTask.connectorName == connector.name &&
|
||||
trangaTask.publication?.internalId == publication?.internalId))
|
||||
if (!_allTasks.Any(trangaTask =>
|
||||
trangaTask.task == task && trangaTask.publication?.internalId == pub.internalId &&
|
||||
trangaTask.connectorName == connector.name))
|
||||
{
|
||||
if(task != TrangaTask.Task.UpdatePublications)
|
||||
_chapterCollection.TryAdd((Publication)publication!, new List<Chapter>());
|
||||
_allTasks.Add(newTask);
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Added new Task {newTask.ToString()}");
|
||||
}
|
||||
else
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Publication already exists {publication?.internalId}");
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Task already exists {newTask.ToString()}");
|
||||
}
|
||||
ExportDataAndSettings();
|
||||
|
||||
|
@ -5,7 +5,7 @@ namespace Tranga;
|
||||
/// <summary>
|
||||
/// Stores information on Task
|
||||
/// </summary>
|
||||
public class TrangaTask
|
||||
public abstract class TrangaTask
|
||||
{
|
||||
// ReSharper disable once CommentTypo ...Tell me why!
|
||||
// ReSharper disable once MemberCanBePrivate.Global I want it thaaat way
|
||||
@ -14,7 +14,7 @@ public class TrangaTask
|
||||
public string? connectorName { get; }
|
||||
public Task task { get; }
|
||||
public Publication? publication { get; }
|
||||
public string language { get; }
|
||||
public string? language { get; }
|
||||
[JsonIgnore]public ExecutionState state { get; set; }
|
||||
|
||||
public enum ExecutionState
|
||||
@ -24,14 +24,8 @@ public class TrangaTask
|
||||
Running
|
||||
};
|
||||
|
||||
public TrangaTask(Task task, string? connectorName, Publication? publication, TimeSpan reoccurrence, string language = "")
|
||||
protected TrangaTask(Task task, string? connectorName, Publication? publication, TimeSpan reoccurrence, string? language = null)
|
||||
{
|
||||
if(task != Task.UpdateKomgaLibrary && connectorName is null)
|
||||
throw new ArgumentException($"connectorName can not be null for task {task}");
|
||||
|
||||
if (publication is null && task != Task.UpdatePublications && task != Task.UpdateKomgaLibrary)
|
||||
throw new ArgumentException($"Publication can not be null for task {task}");
|
||||
|
||||
this.publication = publication;
|
||||
this.reoccurrence = reoccurrence;
|
||||
this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
|
||||
@ -40,6 +34,8 @@ public class TrangaTask
|
||||
this.language = language;
|
||||
}
|
||||
|
||||
public abstract void Execute(TaskManager taskManager);
|
||||
|
||||
/// <returns>True if elapsed time since last execution is greater than set interval</returns>
|
||||
public bool ShouldExecute()
|
||||
{
|
||||
@ -48,8 +44,6 @@ public class TrangaTask
|
||||
|
||||
public enum Task
|
||||
{
|
||||
UpdatePublications,
|
||||
UpdateChapters,
|
||||
DownloadNewChapters,
|
||||
UpdateKomgaLibrary
|
||||
}
|
||||
|
46
Tranga/TrangaTasks/DownloadNewChaptersTask.cs
Normal file
46
Tranga/TrangaTasks/DownloadNewChaptersTask.cs
Normal file
@ -0,0 +1,46 @@
|
||||
namespace Tranga.TrangaTasks;
|
||||
|
||||
public class DownloadNewChaptersTask : TrangaTask
|
||||
{
|
||||
public DownloadNewChaptersTask(Task task, string connectorName, Publication publication, TimeSpan reoccurrence, string language = "en") : base(task, connectorName, publication, reoccurrence, language)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Execute(TaskManager taskManager)
|
||||
{
|
||||
Publication pub = (Publication)this.publication!;
|
||||
Connector connector = taskManager.GetConnector(this.connectorName);
|
||||
|
||||
//Check if Publication already has a Folder
|
||||
string publicationFolder = Path.Join(connector.downloadLocation, pub.folderName);
|
||||
if(!Directory.Exists(publicationFolder))
|
||||
Directory.CreateDirectory(publicationFolder);
|
||||
List<Chapter> newChapters = UpdateChapters(connector, pub, language!, ref taskManager._chapterCollection);
|
||||
|
||||
connector.CopyCoverFromCacheToDownloadLocation(pub, taskManager.settings);
|
||||
|
||||
pub.SaveSeriesInfoJson(connector.downloadLocation);
|
||||
|
||||
foreach(Chapter newChapter in newChapters)
|
||||
connector.DownloadChapter(pub, newChapter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the available Chapters of a Publication
|
||||
/// </summary>
|
||||
/// <param name="connector">Connector to use</param>
|
||||
/// <param name="publication">Publication to check</param>
|
||||
/// <param name="language">Language to receive chapters for</param>
|
||||
/// <param name="chapterCollection"></param>
|
||||
/// <returns>List of Chapters that were previously not in collection</returns>
|
||||
private static List<Chapter> UpdateChapters(Connector connector, Publication publication, string language, ref Dictionary<Publication, List<Chapter>> chapterCollection)
|
||||
{
|
||||
List<Chapter> newChaptersList = new();
|
||||
chapterCollection.TryAdd(publication, newChaptersList); //To ensure publication is actually in collection
|
||||
|
||||
Chapter[] newChapters = connector.GetChapters(publication, language);
|
||||
newChaptersList = newChapters.Where(nChapter => !connector.CheckChapterIsDownloaded(publication, nChapter)).ToList();
|
||||
|
||||
return newChaptersList;
|
||||
}
|
||||
}
|
19
Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs
Normal file
19
Tranga/TrangaTasks/UpdateKomgaLibraryTask.cs
Normal file
@ -0,0 +1,19 @@
|
||||
namespace Tranga.TrangaTasks;
|
||||
|
||||
public class UpdateKomgaLibraryTask : TrangaTask
|
||||
{
|
||||
public UpdateKomgaLibraryTask(Task task, TimeSpan reoccurrence) : base(task, null, null, reoccurrence)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Execute(TaskManager taskManager)
|
||||
{
|
||||
if (taskManager.komga is null)
|
||||
return;
|
||||
Komga komga = taskManager.komga;
|
||||
|
||||
Komga.KomgaLibrary[] allLibraries = komga.GetLibraries();
|
||||
foreach (Komga.KomgaLibrary lib in allLibraries)
|
||||
komga.UpdateLibrary(lib.id);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user