commit
645db0524f
36
Jellyfin.Server/HealthChecks/JellyfinDbHealthCheck.cs
Normal file
36
Jellyfin.Server/HealthChecks/JellyfinDbHealthCheck.cs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Jellyfin.Server.Implementations;
|
||||||
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||||
|
|
||||||
|
namespace Jellyfin.Server.HealthChecks
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Checks connectivity to the database.
|
||||||
|
/// </summary>
|
||||||
|
public class JellyfinDbHealthCheck : IHealthCheck
|
||||||
|
{
|
||||||
|
private readonly JellyfinDbProvider _dbProvider;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="JellyfinDbHealthCheck"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dbProvider">The jellyfin db provider.</param>
|
||||||
|
public JellyfinDbHealthCheck(JellyfinDbProvider dbProvider)
|
||||||
|
{
|
||||||
|
_dbProvider = dbProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
await using var jellyfinDb = _dbProvider.CreateContext();
|
||||||
|
if (await jellyfinDb.Database.CanConnectAsync(cancellationToken).ConfigureAwait(false))
|
||||||
|
{
|
||||||
|
return HealthCheckResult.Healthy("Database connection successful.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return HealthCheckResult.Unhealthy("Unable to connect to the database.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -43,6 +43,7 @@
|
||||||
<PackageReference Include="CommandLineParser" Version="2.8.0" />
|
<PackageReference Include="CommandLineParser" Version="2.8.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.7" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.7" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.7" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.7" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="3.1.7" />
|
||||||
<PackageReference Include="prometheus-net" Version="3.6.0" />
|
<PackageReference Include="prometheus-net" Version="3.6.0" />
|
||||||
<PackageReference Include="prometheus-net.AspNetCore" Version="3.6.0" />
|
<PackageReference Include="prometheus-net.AspNetCore" Version="3.6.0" />
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
|
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
|
||||||
|
|
|
@ -3,9 +3,9 @@ using System.ComponentModel;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using Jellyfin.Api.TypeConverters;
|
using Jellyfin.Api.TypeConverters;
|
||||||
using Jellyfin.Server.Extensions;
|
using Jellyfin.Server.Extensions;
|
||||||
|
using Jellyfin.Server.HealthChecks;
|
||||||
using Jellyfin.Server.Middleware;
|
using Jellyfin.Server.Middleware;
|
||||||
using Jellyfin.Server.Models;
|
using Jellyfin.Server.Models;
|
||||||
using MediaBrowser.Common;
|
|
||||||
using MediaBrowser.Common.Net;
|
using MediaBrowser.Common.Net;
|
||||||
using MediaBrowser.Controller;
|
using MediaBrowser.Controller;
|
||||||
using MediaBrowser.Controller.Configuration;
|
using MediaBrowser.Controller.Configuration;
|
||||||
|
@ -77,6 +77,9 @@ namespace Jellyfin.Server
|
||||||
c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue($"({_serverApplicationHost.ApplicationUserAgentAddress})"));
|
c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue($"({_serverApplicationHost.ApplicationUserAgentAddress})"));
|
||||||
})
|
})
|
||||||
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
|
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
|
||||||
|
|
||||||
|
services.AddHealthChecks()
|
||||||
|
.AddCheck<JellyfinDbHealthCheck>("JellyfinDb");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -132,6 +135,8 @@ namespace Jellyfin.Server
|
||||||
{
|
{
|
||||||
endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
|
endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
endpoints.MapHealthChecks(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/health");
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add type descriptor for legacy datetime parsing.
|
// Add type descriptor for legacy datetime parsing.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user