Added Serilog logging, added Elmah error logging, added health checks

This commit is contained in:
2025-06-09 17:09:55 +02:00
parent 60bfcff539
commit ce168cf9e4
10 changed files with 246 additions and 36 deletions

View File

@@ -1,3 +1,5 @@
using System.Diagnostics;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace Indexer.Models;
public class WorkerCollection
@@ -13,13 +15,41 @@ public class WorkerCollection
public class Worker
{
public string Name { get; set; }
public WorkerConfig Config { get; set; }
public IScriptable Scriptable { get; set; }
public List<ICall> Calls { get; set; }
public Worker(WorkerConfig workerConfig, IScriptable scriptable)
public Worker(string Name, WorkerConfig workerConfig, IScriptable scriptable)
{
this.Name = Name;
this.Config = workerConfig;
this.Scriptable = scriptable;
Calls = [];
}
public HealthCheckResult HealthCheck()
{
bool hasDegraded = false;
bool hasUnhealthy = false;
foreach (ICall call in Calls)
{
HealthCheckResult callHealth = call.HealthCheck();
if (callHealth.Status != HealthStatus.Healthy)
{
hasDegraded |= callHealth.Status == HealthStatus.Degraded;
hasUnhealthy |= callHealth.Status == HealthStatus.Unhealthy;
}
}
if (hasUnhealthy)
{
return HealthCheckResult.Unhealthy(); // TODO: Retrieve and forward the error message for each call
}
else if (hasDegraded)
{
return HealthCheckResult.Degraded();
}
return HealthCheckResult.Healthy();
}
}
@@ -33,13 +63,64 @@ public class WorkerConfig
public required string Name { get; set; }
public required List<string> Searchdomains { get; set; }
public required string Script { get; set; }
public required List<Call> Calls { get; set; }
public required List<CallConfig> Calls { get; set; }
}
public class Call
public class CallConfig
{
public required string Type { get; set; }
public long? Interval { get; set; } // For Type: Interval
public string? Path { get; set; } // For Type: FileSystemWatcher
}
public interface ICall
{
public HealthCheckResult HealthCheck();
}
public class IntervalCall : ICall
{
public System.Timers.Timer Timer;
public IScriptable Scriptable;
public IntervalCall(System.Timers.Timer timer, IScriptable scriptable)
{
Timer = timer;
Scriptable = scriptable;
}
public HealthCheckResult HealthCheck()
{
if (!Scriptable.UpdateInfo.Successful)
{
Debug.WriteLine(Scriptable.UpdateInfo.Exception);
return HealthCheckResult.Unhealthy();
}
double timerInterval = Timer.Interval; // In ms
DateTime lastRunDateTime = Scriptable.UpdateInfo.DateTime;
DateTime now = DateTime.Now;
double millisecondsSinceLastExecution = now.Subtract(lastRunDateTime).TotalMilliseconds;
if (millisecondsSinceLastExecution >= 2 * timerInterval)
{
return HealthCheckResult.Unhealthy();
}
return HealthCheckResult.Healthy();
}
}
public class ScheduleCall : ICall
{
public HealthCheckResult HealthCheck()
{
return HealthCheckResult.Unhealthy(); // Not implemented yet
}
}
public class FileUpdateCall : ICall
{
public HealthCheckResult HealthCheck()
{
return HealthCheckResult.Unhealthy(); // Not implemented yet
}
}