Instead of relying on concreate tasks to do chores, create method in abstract class that calls the BL in concrete class and does chores before and after execution

This commit is contained in:
glax 2023-06-01 10:35:23 +02:00
parent 48b7371a18
commit 8bc23f7c69
3 changed files with 21 additions and 23 deletions

View File

@ -36,13 +36,29 @@ public abstract class TrangaTask
this.task = task;
this.language = language;
}
/// <summary>
/// Should call StartExecutionChores and EndExecutionChores
/// BL for concrete Tasks
/// </summary>
/// <param name="taskManager"></param>
/// <param name="logger"></param>
public abstract void Execute(TaskManager taskManager, Logger? logger);
protected abstract void ExecuteTask(TaskManager taskManager, Logger? logger);
/// <summary>
/// Execute the task
/// </summary>
/// <param name="taskManager">Should be the parent taskManager</param>
/// <param name="logger"></param>
public void Execute(TaskManager taskManager, Logger? logger)
{
logger?.WriteLine(this.GetType().ToString(), $"Executing Task {this}");
this.state = ExecutionState.Running;
ExecuteTask(taskManager, logger);
this.lastExecuted = DateTime.Now;
this.state = ExecutionState.Waiting;
logger?.WriteLine(this.GetType().ToString(), $"Finished Executing Task {this}");
}
/// <returns>True if elapsed time since last execution is greater than set interval</returns>
public bool ShouldExecute()
@ -50,19 +66,6 @@ public abstract class TrangaTask
return DateTime.Now.Subtract(this.lastExecuted) > reoccurrence && state is ExecutionState.Waiting;
}
protected void StartExecutionChores(Logger? logger)
{
logger?.WriteLine(this.GetType().ToString(), $"Executing Task {this}");
this.state = ExecutionState.Running;
}
protected void EndExecutionChores(Logger? logger)
{
logger?.WriteLine(this.GetType().ToString(), $"Finished Executing Task {this}");
this.lastExecuted = DateTime.Now;
this.state = ExecutionState.Waiting;
}
public enum Task : byte
{
DownloadNewChapters = 2,

View File

@ -8,9 +8,8 @@ public class DownloadNewChaptersTask : TrangaTask
{
}
public override void Execute(TaskManager taskManager, Logger? logger)
protected override void ExecuteTask(TaskManager taskManager, Logger? logger)
{
StartExecutionChores(logger);
Publication pub = (Publication)this.publication!;
Connector connector = taskManager.GetConnector(this.connectorName);
@ -26,8 +25,6 @@ public class DownloadNewChaptersTask : TrangaTask
foreach(Chapter newChapter in newChapters)
connector.DownloadChapter(pub, newChapter);
EndExecutionChores(logger);
}
/// <summary>

View File

@ -8,9 +8,8 @@ public class UpdateKomgaLibraryTask : TrangaTask
{
}
public override void Execute(TaskManager taskManager, Logger? logger)
protected override void ExecuteTask(TaskManager taskManager, Logger? logger)
{
StartExecutionChores(logger);
if (taskManager.komga is null)
return;
Komga komga = taskManager.komga;
@ -18,6 +17,5 @@ public class UpdateKomgaLibraryTask : TrangaTask
Komga.KomgaLibrary[] allLibraries = komga.GetLibraries();
foreach (Komga.KomgaLibrary lib in allLibraries)
komga.UpdateLibrary(lib.id);
EndExecutionChores(logger);
}
}