Added Serilog logging, added Elmah error logging, added health checks
This commit is contained in:
44
src/Indexer/IndexerHealthChecks.cs
Normal file
44
src/Indexer/IndexerHealthChecks.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using Indexer.Models;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
|
||||
namespace Indexer;
|
||||
|
||||
public class WorkerHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly WorkerCollection _workerCollection;
|
||||
public WorkerHealthCheck(WorkerCollection workerCollection)
|
||||
{
|
||||
_workerCollection = workerCollection;
|
||||
}
|
||||
public Task<HealthCheckResult> CheckHealthAsync(
|
||||
HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
bool hasDegraded = false;
|
||||
bool hasUnhealthy = false;
|
||||
Dictionary<string, HealthStatus> degradedWorkerList = [];
|
||||
foreach (Worker worker in _workerCollection.Workers)
|
||||
{
|
||||
HealthCheckResult workerHealth = worker.HealthCheck();
|
||||
hasDegraded |= workerHealth.Status == HealthStatus.Degraded;
|
||||
hasUnhealthy |= workerHealth.Status == HealthStatus.Unhealthy;
|
||||
if (workerHealth.Status != HealthStatus.Healthy)
|
||||
{
|
||||
degradedWorkerList[worker.Name] = workerHealth.Status;
|
||||
}
|
||||
}
|
||||
string degradedWorkerListString = "{" + string.Join(",", [.. degradedWorkerList.Select(kv => '"' + kv.Key + "\": " + kv.Value)]) + "}";
|
||||
if (hasUnhealthy)
|
||||
{
|
||||
return Task.FromResult(
|
||||
HealthCheckResult.Unhealthy(degradedWorkerListString));
|
||||
}
|
||||
else if (hasDegraded)
|
||||
{
|
||||
return Task.FromResult(
|
||||
HealthCheckResult.Degraded(degradedWorkerListString));
|
||||
}
|
||||
return Task.FromResult(
|
||||
HealthCheckResult.Healthy());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user