Added healthchecks to server
This commit is contained in:
@@ -3,7 +3,9 @@ The server by default
|
|||||||
- runs on port 5146
|
- runs on port 5146
|
||||||
- Uses Swagger UI in development mode (`/swagger/index.html`)
|
- Uses Swagger UI in development mode (`/swagger/index.html`)
|
||||||
- Ignores API keys when in development mode
|
- Ignores API keys when in development mode
|
||||||
|
- Uses Elmah error logging (endpoint: `/elmah`, local files: `~/logs`)
|
||||||
|
- Uses serilog logging (local files: `~/logs`)
|
||||||
|
- Uses HealthChecks (endpoint: `/healthz`)
|
||||||
# Installing the dependencies
|
# Installing the dependencies
|
||||||
## Ubuntu 24.04
|
## Ubuntu 24.04
|
||||||
1. Install the .NET SDK: `sudo apt update && sudo apt install dotnet-sdk-8.0 -y`
|
1. Install the .NET SDK: `sudo apt update && sudo apt install dotnet-sdk-8.0 -y`
|
||||||
|
|||||||
31
src/Server/HealthChecks/AIProviderHealthChecks.cs
Normal file
31
src/Server/HealthChecks/AIProviderHealthChecks.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||||
|
using Server.Migrations;
|
||||||
|
|
||||||
|
namespace Server.HealthChecks;
|
||||||
|
|
||||||
|
public class AIProviderHealthCheck : IHealthCheck
|
||||||
|
{
|
||||||
|
private readonly SearchdomainManager _searchdomainManager;
|
||||||
|
private readonly ILogger<DatabaseHealthCheck> _logger;
|
||||||
|
public AIProviderHealthCheck(SearchdomainManager searchdomainManager, ILogger<DatabaseHealthCheck> logger)
|
||||||
|
{
|
||||||
|
_searchdomainManager = searchdomainManager;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
public Task<HealthCheckResult> CheckHealthAsync(
|
||||||
|
HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var _ = _searchdomainManager.client.ListLocalModelsAsync(cancellationToken).Result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogCritical("AIProviderHealthCheck - Exception occurred when listing local models: {ex}", ex.Message);
|
||||||
|
return Task.FromResult(
|
||||||
|
HealthCheckResult.Unhealthy());
|
||||||
|
}
|
||||||
|
return Task.FromResult(
|
||||||
|
HealthCheckResult.Healthy());
|
||||||
|
}
|
||||||
|
}
|
||||||
44
src/Server/HealthChecks/ServerHealthChecks.cs
Normal file
44
src/Server/HealthChecks/ServerHealthChecks.cs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||||
|
using Server.Migrations;
|
||||||
|
|
||||||
|
namespace Server.HealthChecks;
|
||||||
|
|
||||||
|
public class DatabaseHealthCheck : IHealthCheck
|
||||||
|
{
|
||||||
|
private readonly SearchdomainManager _searchdomainManager;
|
||||||
|
private readonly ILogger<DatabaseHealthCheck> _logger;
|
||||||
|
public DatabaseHealthCheck(SearchdomainManager searchdomainManager, ILogger<DatabaseHealthCheck> logger)
|
||||||
|
{
|
||||||
|
_searchdomainManager = searchdomainManager;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
public Task<HealthCheckResult> CheckHealthAsync(
|
||||||
|
HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DatabaseMigrations.DatabaseGetVersion(_searchdomainManager.helper);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogCritical("DatabaseHealthCheck - Exception occurred when retrieving and parsing database version: {ex}", ex.Message);
|
||||||
|
return Task.FromResult(
|
||||||
|
HealthCheckResult.Unhealthy());
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_searchdomainManager.helper.ExecuteSQLNonQuery("INSERT INTO settings (name, value) VALUES ('test', 'x');", []);
|
||||||
|
_searchdomainManager.helper.ExecuteSQLNonQuery("DELETE FROM settings WHERE name = 'test';", []);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogCritical("DatabaseHealthCheck - Exception occurred when executing INSERT/DELETE query: {ex}", ex.Message);
|
||||||
|
return Task.FromResult(
|
||||||
|
HealthCheckResult.Unhealthy());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.FromResult(
|
||||||
|
HealthCheckResult.Healthy());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ using ElmahCore;
|
|||||||
using ElmahCore.Mvc;
|
using ElmahCore.Mvc;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Server;
|
using Server;
|
||||||
|
using Server.HealthChecks;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@@ -16,6 +17,9 @@ Log.Logger = new LoggerConfiguration()
|
|||||||
.CreateLogger();
|
.CreateLogger();
|
||||||
builder.Logging.AddSerilog();
|
builder.Logging.AddSerilog();
|
||||||
builder.Services.AddSingleton<SearchdomainManager>();
|
builder.Services.AddSingleton<SearchdomainManager>();
|
||||||
|
builder.Services.AddHealthChecks()
|
||||||
|
.AddCheck<DatabaseHealthCheck>("DatabaseHealthCheck")
|
||||||
|
.AddCheck<AIProviderHealthCheck>("AIProviderHealthChecck");
|
||||||
|
|
||||||
builder.Services.AddElmah<XmlFileErrorLog>(Options =>
|
builder.Services.AddElmah<XmlFileErrorLog>(Options =>
|
||||||
{
|
{
|
||||||
@@ -48,6 +52,8 @@ app.Use(async (context, next) =>
|
|||||||
|
|
||||||
app.UseElmah();
|
app.UseElmah();
|
||||||
|
|
||||||
|
app.MapHealthChecks("/healthz");
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user