Merge pull request #4047 from crobibero/health-2.0

Use efcore library for health check
This commit is contained in:
Bond-009 2020-09-04 02:24:26 +02:00 committed by GitHub
commit 250216cc53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 38 deletions

View File

@ -1,36 +0,0 @@
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.");
}
}
}

View File

@ -44,6 +44,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.Diagnostics.HealthChecks" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="3.1.7" />
<PackageReference Include="prometheus-net" Version="3.6.0" />
<PackageReference Include="prometheus-net.AspNetCore" Version="3.6.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />

View File

@ -3,7 +3,7 @@ using System.ComponentModel;
using System.Net.Http.Headers;
using Jellyfin.Api.TypeConverters;
using Jellyfin.Server.Extensions;
using Jellyfin.Server.HealthChecks;
using Jellyfin.Server.Implementations;
using Jellyfin.Server.Middleware;
using Jellyfin.Server.Models;
using MediaBrowser.Common.Net;
@ -79,7 +79,7 @@ namespace Jellyfin.Server
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
services.AddHealthChecks()
.AddCheck<JellyfinDbHealthCheck>("JellyfinDb");
.AddDbContextCheck<JellyfinDb>();
}
/// <summary>