JellyfinDbHealthCheck.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using Jellyfin.Server.Implementations;
  4. using Microsoft.Extensions.Diagnostics.HealthChecks;
  5. namespace Jellyfin.Server.HealthChecks
  6. {
  7. /// <summary>
  8. /// Checks connectivity to the database.
  9. /// </summary>
  10. public class JellyfinDbHealthCheck : IHealthCheck
  11. {
  12. private readonly JellyfinDbProvider _dbProvider;
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="JellyfinDbHealthCheck"/> class.
  15. /// </summary>
  16. /// <param name="dbProvider">The jellyfin db provider.</param>
  17. public JellyfinDbHealthCheck(JellyfinDbProvider dbProvider)
  18. {
  19. _dbProvider = dbProvider;
  20. }
  21. /// <inheritdoc />
  22. public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
  23. {
  24. await using var jellyfinDb = _dbProvider.CreateContext();
  25. if (await jellyfinDb.Database.CanConnectAsync(cancellationToken).ConfigureAwait(false))
  26. {
  27. return HealthCheckResult.Healthy("Database connection successful.");
  28. }
  29. return HealthCheckResult.Unhealthy("Unable to connect to the database.");
  30. }
  31. }
  32. }