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:
parent
48b7371a18
commit
8bc23f7c69
@ -36,13 +36,29 @@ public abstract class TrangaTask
|
|||||||
this.task = task;
|
this.task = task;
|
||||||
this.language = language;
|
this.language = language;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Should call StartExecutionChores and EndExecutionChores
|
/// BL for concrete Tasks
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="taskManager"></param>
|
/// <param name="taskManager"></param>
|
||||||
/// <param name="logger"></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>
|
/// <returns>True if elapsed time since last execution is greater than set interval</returns>
|
||||||
public bool ShouldExecute()
|
public bool ShouldExecute()
|
||||||
@ -50,19 +66,6 @@ public abstract class TrangaTask
|
|||||||
return DateTime.Now.Subtract(this.lastExecuted) > reoccurrence && state is ExecutionState.Waiting;
|
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
|
public enum Task : byte
|
||||||
{
|
{
|
||||||
DownloadNewChapters = 2,
|
DownloadNewChapters = 2,
|
||||||
|
@ -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!;
|
Publication pub = (Publication)this.publication!;
|
||||||
Connector connector = taskManager.GetConnector(this.connectorName);
|
Connector connector = taskManager.GetConnector(this.connectorName);
|
||||||
|
|
||||||
@ -26,8 +25,6 @@ public class DownloadNewChaptersTask : TrangaTask
|
|||||||
|
|
||||||
foreach(Chapter newChapter in newChapters)
|
foreach(Chapter newChapter in newChapters)
|
||||||
connector.DownloadChapter(pub, newChapter);
|
connector.DownloadChapter(pub, newChapter);
|
||||||
|
|
||||||
EndExecutionChores(logger);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -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)
|
if (taskManager.komga is null)
|
||||||
return;
|
return;
|
||||||
Komga komga = taskManager.komga;
|
Komga komga = taskManager.komga;
|
||||||
@ -18,6 +17,5 @@ public class UpdateKomgaLibraryTask : TrangaTask
|
|||||||
Komga.KomgaLibrary[] allLibraries = komga.GetLibraries();
|
Komga.KomgaLibrary[] allLibraries = komga.GetLibraries();
|
||||||
foreach (Komga.KomgaLibrary lib in allLibraries)
|
foreach (Komga.KomgaLibrary lib in allLibraries)
|
||||||
komga.UpdateLibrary(lib.id);
|
komga.UpdateLibrary(lib.id);
|
||||||
EndExecutionChores(logger);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user