diff --git a/src/Indexer/Controllers/CallsController.cs b/src/Indexer/Controllers/CallsController.cs index d048d8c..9155957 100644 --- a/src/Indexer/Controllers/CallsController.cs +++ b/src/Indexer/Controllers/CallsController.cs @@ -41,7 +41,8 @@ public class CallsController : ControllerBase CallListResult callListResult = new() { CallConfig = call.CallConfig, - IsRunning = call.IsRunning, + IsActive = call.IsEnabled, + IsExecuting = call.IsExecuting, LastExecution = call.LastExecution, LastSuccessfulExecution = call.LastSuccessfulExecution, HealthStatus = call.HealthCheck().Status.ToString() diff --git a/src/Indexer/Controllers/WorkerController.cs b/src/Indexer/Controllers/WorkerController.cs index c73779a..3970254 100644 --- a/src/Indexer/Controllers/WorkerController.cs +++ b/src/Indexer/Controllers/WorkerController.cs @@ -35,6 +35,9 @@ public class WorkerController : ControllerBase { Name = worker.Name, Script = worker.Config.Script, + IsExecuting = worker.IsExecuting, + LastExecution = worker.LastExecution, + LastSuccessfulExecution = worker.LastSuccessfulExecution, HealthStatus = worker.HealthCheck().Status.ToString() }; workerListResultList.Add(workerListResult); @@ -65,7 +68,26 @@ public class WorkerController : ControllerBase } _logger.LogInformation("triggering worker {name}.", [name]); ManualTriggerCallbackInfos callbackInfos = new(); - worker.Scriptable.Update(callbackInfos); + lock (worker.Scriptable) + { + worker.IsExecuting = true; + worker.Scriptable.Update(callbackInfos); + worker.IsExecuting = false; + + DateTime beforeExecution = DateTime.Now; + worker.IsExecuting = true; + try + { + worker.Scriptable.Update(callbackInfos); + } + finally + { + worker.IsExecuting = false; + worker.LastExecution = beforeExecution; + } + DateTime afterExecution = DateTime.Now; + WorkerCollection.UpdateWorkerTimestamps(worker, beforeExecution, afterExecution); + } _logger.LogInformation("triggered worker {name}.", [name]); return new WorkerTriggerUpdateResult { Success = true }; diff --git a/src/Indexer/Models/Worker.cs b/src/Indexer/Models/Worker.cs index 9217c27..9e143f3 100644 --- a/src/Indexer/Models/Worker.cs +++ b/src/Indexer/Models/Worker.cs @@ -79,9 +79,20 @@ public class WorkerCollection { try { - call.LastExecution = DateTime.Now; - worker.Scriptable.Update(new IntervalCallbackInfos() { sender = sender, e = e }); - call.LastSuccessfulExecution = DateTime.Now; + DateTime beforeExecution = DateTime.Now; + call.IsExecuting = true; + try + { + worker.Scriptable.Update(new IntervalCallbackInfos() { sender = sender, e = e }); + } + finally + { + call.IsExecuting = false; + call.LastExecution = beforeExecution; + worker.LastExecution = beforeExecution; + } + DateTime afterExecution = DateTime.Now; + UpdateCallAndWorkerTimestamps(call, worker, beforeExecution, afterExecution); } catch (Exception ex) { @@ -106,6 +117,32 @@ public class WorkerCollection } } + public static void UpdateCallAndWorkerTimestamps(ICall call, Worker worker, DateTime beforeExecution, DateTime afterExecution) + { + UpdateCallTimestamps(call, beforeExecution, afterExecution); + UpdateWorkerTimestamps(worker, beforeExecution, afterExecution); + } + + public static void UpdateCallTimestamps(ICall call, DateTime beforeExecution, DateTime afterExecution) + { + call.LastSuccessfulExecution = GetNewestDateTime(call.LastSuccessfulExecution, afterExecution); + } + + public static void UpdateWorkerTimestamps(Worker worker, DateTime beforeExecution, DateTime afterExecution) + { + worker.LastSuccessfulExecution = GetNewestDateTime(worker.LastSuccessfulExecution, afterExecution); + } + + + public static DateTime? GetNewestDateTime(DateTime? preexistingDateTime, DateTime incomingDateTime) + { + if (preexistingDateTime is null || preexistingDateTime.Value.CompareTo(incomingDateTime) < 0) + { + return incomingDateTime; + } + return preexistingDateTime; + } + public IScriptable GetScriptable(ScriptToolSet toolSet) { string fileName = toolSet.filePath; @@ -129,12 +166,16 @@ public class Worker public WorkerConfig Config { get; set; } public IScriptable Scriptable { get; set; } public List Calls { get; set; } + public bool IsExecuting { get; set; } + public DateTime? LastExecution { get; set; } + public DateTime? LastSuccessfulExecution { get; set; } - public Worker(string Name, WorkerConfig workerConfig, IScriptable scriptable) + public Worker(string name, WorkerConfig workerConfig, IScriptable scriptable) { - this.Name = Name; - this.Config = workerConfig; - this.Scriptable = scriptable; + Name = name; + Config = workerConfig; + Scriptable = scriptable; + IsExecuting = false; Calls = []; } @@ -188,7 +229,8 @@ public interface ICall public HealthCheckResult HealthCheck(); public void Start(); public void Stop(); - public bool IsRunning { get; set; } + public bool IsEnabled { get; set; } + public bool IsExecuting { get; set; } public CallConfig CallConfig { get; set; } public DateTime? LastExecution { get; set; } public DateTime? LastSuccessfulExecution { get; set; } @@ -199,7 +241,8 @@ public class IntervalCall : ICall public System.Timers.Timer Timer; public IScriptable Scriptable; public ILogger _logger; - public bool IsRunning { get; set; } + public bool IsEnabled { get; set; } + public bool IsExecuting { get; set; } public CallConfig CallConfig { get; set; } public DateTime? LastExecution { get; set; } public DateTime? LastSuccessfulExecution { get; set; } @@ -210,20 +253,21 @@ public class IntervalCall : ICall Scriptable = scriptable; _logger = logger; CallConfig = callConfig; - IsRunning = true; + IsEnabled = true; + IsExecuting = false; } public void Start() { Timer.Start(); - IsRunning = true; + IsEnabled = true; } public void Stop() { Scriptable.Stop(); Timer.Stop(); - IsRunning = false; + IsEnabled = false; } public HealthCheckResult HealthCheck() @@ -249,7 +293,8 @@ public class IntervalCall : ICall public class ScheduleCall : ICall { - public bool IsRunning { get; set; } + public bool IsEnabled { get; set; } + public bool IsExecuting { get; set; } public CallConfig CallConfig { get; set; } public DateTime? LastExecution { get; set; } public DateTime? LastSuccessfulExecution { get; set; } @@ -257,6 +302,8 @@ public class ScheduleCall : ICall public ScheduleCall(CallConfig callConfig) { CallConfig = callConfig; + IsEnabled = true; + IsExecuting = false; } public void Start() @@ -275,7 +322,8 @@ public class ScheduleCall : ICall public class FileUpdateCall : ICall { - public bool IsRunning { get; set; } + public bool IsEnabled { get; set; } + public bool IsExecuting { get; set; } public CallConfig CallConfig { get; set; } public DateTime? LastExecution { get; set; } public DateTime? LastSuccessfulExecution { get; set; } @@ -283,6 +331,8 @@ public class FileUpdateCall : ICall public FileUpdateCall(CallConfig callConfig) { CallConfig = callConfig; + IsEnabled = true; + IsExecuting = false; } public void Start() diff --git a/src/Indexer/Models/WorkerResults.cs b/src/Indexer/Models/WorkerResults.cs index 8defcf3..c1e05a1 100644 --- a/src/Indexer/Models/WorkerResults.cs +++ b/src/Indexer/Models/WorkerResults.cs @@ -17,6 +17,12 @@ public class WorkerListResult public required string Name { get; set; } [JsonPropertyName("Script")] public required string Script { get; set; } + [JsonPropertyName("IsExecuting")] + public required bool IsExecuting { get; set; } + [JsonPropertyName("LastExecution")] + public required DateTime? LastExecution { get; set; } + [JsonPropertyName("LastSuccessfulExecution")] + public required DateTime? LastSuccessfulExecution { get; set; } [JsonPropertyName("HealthStatus")] public required string HealthStatus { get; set; } } @@ -33,8 +39,10 @@ public class CallListResult { [JsonPropertyName("CallConfig")] public required CallConfig CallConfig { get; set; } - [JsonPropertyName("IsRunning")] - public required bool IsRunning { get; set; } + [JsonPropertyName("IsActive")] + public required bool IsActive { get; set; } + [JsonPropertyName("IsExecuting")] + public required bool IsExecuting { get; set; } [JsonPropertyName("LastExecution")] public required DateTime? LastExecution { get; set; } [JsonPropertyName("LastSuccessfulExecution")]