Added "Remove task from queue"-Menu

Added "Remove task from queue" to TaskManager

Better naming for deleting tasks and the taskqueue
This commit is contained in:
glax 2023-05-21 03:04:32 +02:00
parent e72efa3731
commit 95c65c981e
3 changed files with 78 additions and 15 deletions

View File

@ -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");

View File

@ -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)
{

View File

@ -14,7 +14,7 @@ public class TaskManager
private readonly HashSet<TrangaTask> _allTasks;
private bool _continueRunning = true;
private readonly Connector[] _connectors;
private Dictionary<Connector, List<TrangaTask>> tasksToExecute = new();
private readonly Dictionary<Connector, List<TrangaTask>> _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<TrangaTask>());
_taskQueue.Add(cConnector, new List<TrangaTask>());
_allTasks = new HashSet<TrangaTask>();
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<TrangaTask>());
_taskQueue.Add(cConnector, new List<TrangaTask>());
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<Connector, List<TrangaTask>> connectorTaskQueue in tasksToExecute)
foreach (KeyValuePair<Connector, List<TrangaTask>> 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
/// <param name="task">TrangaTask.Task type</param>
/// <param name="connectorName">Name of Connector that was used</param>
/// <param name="publication">Publication that was used</param>
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());
}
/// <summary>
/// Removes a Task from the queue
/// </summary>
/// <param name="task"></param>
public void RemoveTaskFromQueue(TrangaTask task)
{
task.lastExecuted = DateTime.Now;
foreach (List<TrangaTask> taskList in this._taskQueue.Values)
taskList.Remove(task);
task.state = TrangaTask.ExecutionState.Waiting;
}
/// <returns>All available Connectors</returns>
public Dictionary<string, Connector> GetAvailableConnectors()