2
0

Logging and Chores in abstract class

This commit is contained in:
glax 2023-05-31 22:32:37 +02:00
parent 8ff65bf400
commit 61ecefb615
3 changed files with 30 additions and 14 deletions

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json; using Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Tranga.TrangaTasks; using Tranga.TrangaTasks;
@ -37,10 +38,11 @@ public abstract class TrangaTask
} }
/// <summary> /// <summary>
/// Set state to running /// Should call StartExecutionChores and EndExecutionChores
/// </summary> /// </summary>
/// <param name="taskManager"></param> /// <param name="taskManager"></param>
public abstract void Execute(TaskManager taskManager); /// <param name="logger"></param>
public abstract void Execute(TaskManager taskManager, Logger? logger);
/// <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()
@ -48,6 +50,19 @@ 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,

View File

@ -1,4 +1,6 @@
namespace Tranga.TrangaTasks; using Logging;
namespace Tranga.TrangaTasks;
public class DownloadNewChaptersTask : TrangaTask public class DownloadNewChaptersTask : TrangaTask
{ {
@ -6,9 +8,9 @@ public class DownloadNewChaptersTask : TrangaTask
{ {
} }
public override void Execute(TaskManager taskManager) public override void Execute(TaskManager taskManager, Logger? logger)
{ {
this.state = ExecutionState.Running; StartExecutionChores(logger);
Publication pub = (Publication)this.publication!; Publication pub = (Publication)this.publication!;
Connector connector = taskManager.GetConnector(this.connectorName); Connector connector = taskManager.GetConnector(this.connectorName);
@ -25,8 +27,7 @@ public class DownloadNewChaptersTask : TrangaTask
foreach(Chapter newChapter in newChapters) foreach(Chapter newChapter in newChapters)
connector.DownloadChapter(pub, newChapter); connector.DownloadChapter(pub, newChapter);
this.lastExecuted = DateTime.Now; EndExecutionChores(logger);
this.state = ExecutionState.Waiting;
} }
/// <summary> /// <summary>

View File

@ -1,4 +1,6 @@
namespace Tranga.TrangaTasks; using Logging;
namespace Tranga.TrangaTasks;
public class UpdateKomgaLibraryTask : TrangaTask public class UpdateKomgaLibraryTask : TrangaTask
{ {
@ -6,9 +8,9 @@ public class UpdateKomgaLibraryTask : TrangaTask
{ {
} }
public override void Execute(TaskManager taskManager) public override void Execute(TaskManager taskManager, Logger? logger)
{ {
this.state = ExecutionState.Running; StartExecutionChores(logger);
if (taskManager.komga is null) if (taskManager.komga is null)
return; return;
Komga komga = taskManager.komga; Komga komga = taskManager.komga;
@ -16,8 +18,6 @@ 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);
this.lastExecuted = DateTime.Now;
this.state = ExecutionState.Waiting;
} }
} }