From 95c65c981e622b3320855d7b1cae810a80e69459 Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 21 May 2023 03:04:32 +0200 Subject: [PATCH] Added "Remove task from queue"-Menu Added "Remove task from queue" to TaskManager Better naming for deleting tasks and the taskqueue --- Tranga-API/Program.cs | 4 +-- Tranga-CLI/Tranga_Cli.cs | 65 +++++++++++++++++++++++++++++++++++----- Tranga/TaskManager.cs | 24 +++++++++++---- 3 files changed, 78 insertions(+), 15 deletions(-) diff --git a/Tranga-API/Program.cs b/Tranga-API/Program.cs index 7f9138e..7d4e186 100644 --- a/Tranga-API/Program.cs +++ b/Tranga-API/Program.cs @@ -63,14 +63,14 @@ app.MapGet("/RemoveTask", (TrangaTask.Task task, string? connectorName, string? switch (task) { case TrangaTask.Task.UpdateKomgaLibrary: - taskManager.RemoveTask(TrangaTask.Task.UpdateKomgaLibrary, null, null); + taskManager.DeleteTask(TrangaTask.Task.UpdateKomgaLibrary, null, null); return JsonSerializer.Serialize("Success"); case TrangaTask.Task.DownloadNewChapters: Publication? publication = taskManager.GetAllPublications().FirstOrDefault(pub => pub.downloadUrl == publicationName); if (publication is null) JsonSerializer.Serialize($"Publication {publicationName} is unknown."); - taskManager.RemoveTask(TrangaTask.Task.DownloadNewChapters, connectorName, publication); + taskManager.DeleteTask(TrangaTask.Task.DownloadNewChapters, connectorName, publication); return JsonSerializer.Serialize("Success"); diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga-CLI/Tranga_Cli.cs index 1240a74..dee8254 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga-CLI/Tranga_Cli.cs @@ -91,7 +91,7 @@ public static class Tranga_Cli Console.ReadKey(); break; case ConsoleKey.D: - RemoveTask (taskManager, logger); + DeleteTask(taskManager, logger); Console.WriteLine("Press any key."); Console.ReadKey(); break; @@ -120,6 +120,11 @@ public static class Tranga_Cli Console.WriteLine("Press any key."); Console.ReadKey(); break; + case ConsoleKey.M: + RemoveTaskFromQueue(taskManager, logger); + Console.WriteLine("Press any key."); + Console.ReadKey(); + break; } selection = PrintMenu(taskManager, settings.downloadLocation, logger); } @@ -148,10 +153,9 @@ public static class Tranga_Cli Console.WriteLine($"Tasks (Running/Queue/Total)): {taskRunningCount}/{taskEnqueuedCount}/{taskCount}"); Console.WriteLine(); Console.WriteLine($"{"C: Create Task",-30}{"L: List tasks",-30}"); - Console.WriteLine($"{"D: Delete Task",-30}{"R: List Running Tasks", -30}"); - Console.WriteLine($"{"E: Execute Task now",-30}{"S: Search Tasks", -30}"); - Console.WriteLine($"{"",-30}{"K: List Task Queue", -30}"); - //Console.WriteLine(); + Console.WriteLine($"{"D: Delete Task",-30}{"R: List Running Tasks", -30}{"K: List Task Queue", -30}"); + Console.WriteLine($"{"E: Execute Task now",-30}{"S: Search Tasks", -30}{"M: Remove Task from Queue", -30}"); + Console.WriteLine(); Console.WriteLine($"{"F: Show Log",-30}{"",-30}"); Console.WriteLine($"{"U: Update this Screen",-30}{"Q: Exit",-30}"); ConsoleKey selection = Console.ReadKey().Key; @@ -176,6 +180,53 @@ public static class Tranga_Cli Console.WriteLine($"{tIndex++:000}: {trangaTask}"); } + private static void RemoveTaskFromQueue(TaskManager taskManager, Logger logger) + { + Console.Clear(); + logger.WriteLine("Tranga_CLI", "Menu: Remove Task from queue"); + + TrangaTask[] tasks = taskManager.GetAllTasks(); + if (tasks.Length < 1) + { + Console.Clear(); + Console.WriteLine("There are no available Tasks."); + logger.WriteLine("Tranga_CLI", "No available Tasks."); + return; + } + PrintTasks(tasks, logger); + + logger.WriteLine("Tranga_CLI", "Selecting Task to Remove (from queue)"); + Console.WriteLine("Enter q to abort"); + Console.WriteLine($"Select Task (0-{tasks.Length - 1}):"); + + string? selectedTask = Console.ReadLine(); + while(selectedTask is null || selectedTask.Length < 1) + selectedTask = Console.ReadLine(); + + if (selectedTask.Length == 1 && selectedTask.ToLower() == "q") + { + Console.Clear(); + Console.WriteLine("aborted."); + logger.WriteLine("Tranga_CLI", "aborted"); + return; + } + + try + { + int selectedTaskIndex = Convert.ToInt32(selectedTask); + logger.WriteLine("Tranga_CLI", "Sending Task to TaskManager"); + taskManager.RemoveTaskFromQueue(tasks[selectedTaskIndex]); + } + catch (Exception e) + { + Console.WriteLine($"Exception: {e.Message}"); + logger.WriteLine("Tranga_CLI", e.Message); + } + + + + } + private static void ShowLastLoglines(Logger logger) { Console.Clear(); @@ -283,7 +334,7 @@ public static class Tranga_Cli } - private static void RemoveTask(TaskManager taskManager, Logger logger) + private static void DeleteTask(TaskManager taskManager, Logger logger) { logger.WriteLine("Tranga_CLI", "Menu: Remove Task"); TrangaTask[] tasks = taskManager.GetAllTasks(); @@ -316,7 +367,7 @@ public static class Tranga_Cli { int selectedTaskIndex = Convert.ToInt32(selectedTask); logger.WriteLine("Tranga_CLI", "Sending Task to TaskManager"); - taskManager.RemoveTask(tasks[selectedTaskIndex].task, tasks[selectedTaskIndex].connectorName, tasks[selectedTaskIndex].publication); + taskManager.DeleteTask(tasks[selectedTaskIndex].task, tasks[selectedTaskIndex].connectorName, tasks[selectedTaskIndex].publication); } catch (Exception e) { diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index a893e4c..8b22faa 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -14,7 +14,7 @@ public class TaskManager private readonly HashSet _allTasks; private bool _continueRunning = true; private readonly Connector[] _connectors; - private Dictionary> tasksToExecute = new(); + private readonly Dictionary> _taskQueue = new(); private string downloadLocation { get; } private Logger? logger { get; } @@ -34,7 +34,7 @@ public class TaskManager this.komga = new Komga(komgaBaseUrl, komgaUsername, komgaPassword, logger); this._connectors = new Connector[]{ new MangaDex(folderPath, logger) }; foreach(Connector cConnector in this._connectors) - tasksToExecute.Add(cConnector, new List()); + _taskQueue.Add(cConnector, new List()); _allTasks = new HashSet(); Thread taskChecker = new(TaskCheckerThread); @@ -46,7 +46,7 @@ public class TaskManager this.logger = logger; this._connectors = new Connector[]{ new MangaDex(settings.downloadLocation, logger) }; foreach(Connector cConnector in this._connectors) - tasksToExecute.Add(cConnector, new List()); + _taskQueue.Add(cConnector, new List()); this.downloadLocation = settings.downloadLocation; this.komga = settings.komga; _allTasks = settings.allTasks; @@ -64,7 +64,7 @@ public class TaskManager while (_continueRunning) { //Check if previous tasks have finished and execute new tasks - foreach (KeyValuePair> connectorTaskQueue in tasksToExecute) + foreach (KeyValuePair> connectorTaskQueue in _taskQueue) { if(connectorTaskQueue.Value.RemoveAll(task => task.state == TrangaTask.ExecutionState.Waiting) > 0) ExportData(Directory.GetCurrentDirectory()); @@ -83,7 +83,7 @@ public class TaskManager else { logger?.WriteLine(this.GetType().ToString(), $"Task due: {task}"); - tasksToExecute[GetConnector(task.connectorName!)].Add(task); + _taskQueue[GetConnector(task.connectorName!)].Add(task); } } Thread.Sleep(1000); @@ -165,7 +165,7 @@ public class TaskManager /// TrangaTask.Task type /// Name of Connector that was used /// Publication that was used - public void RemoveTask(TrangaTask.Task task, string? connectorName, Publication? publication) + public void DeleteTask(TrangaTask.Task task, string? connectorName, Publication? publication) { logger?.WriteLine(this.GetType().ToString(), $"Removing Task {task} {publication?.sortName}"); if (task == TrangaTask.Task.UpdateKomgaLibrary) @@ -186,6 +186,18 @@ public class TaskManager } ExportData(Directory.GetCurrentDirectory()); } + + /// + /// Removes a Task from the queue + /// + /// + public void RemoveTaskFromQueue(TrangaTask task) + { + task.lastExecuted = DateTime.Now; + foreach (List taskList in this._taskQueue.Values) + taskList.Remove(task); + task.state = TrangaTask.ExecutionState.Waiting; + } /// All available Connectors public Dictionary GetAvailableConnectors()