| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | using Microsoft.Extensions.Diagnostics.HealthChecks; |
| | | 6 | | |
| | | 7 | | namespace Jellyfin.Server.HealthChecks; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Implementation of the <see cref="DbContextHealthCheck{TContext}"/> for a <see cref="IDbContextFactory{TContext}"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <typeparam name="TContext">The type of database context.</typeparam> |
| | | 13 | | public class DbContextFactoryHealthCheck<TContext> : IHealthCheck |
| | | 14 | | where TContext : DbContext |
| | | 15 | | { |
| | | 16 | | private readonly IDbContextFactory<TContext> _dbContextFactory; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="DbContextFactoryHealthCheck{TContext}"/> class. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="contextFactory">Instance of the <see cref="IDbContextFactory{TContext}"/> interface.</param> |
| | | 22 | | public DbContextFactoryHealthCheck(IDbContextFactory<TContext> contextFactory) |
| | | 23 | | { |
| | 0 | 24 | | _dbContextFactory = contextFactory; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToke |
| | | 29 | | { |
| | | 30 | | ArgumentNullException.ThrowIfNull(context); |
| | | 31 | | |
| | | 32 | | var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | | 33 | | await using (dbContext.ConfigureAwait(false)) |
| | | 34 | | { |
| | | 35 | | if (await dbContext.Database.CanConnectAsync(cancellationToken).ConfigureAwait(false)) |
| | | 36 | | { |
| | | 37 | | return HealthCheckResult.Healthy(); |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | return HealthCheckResult.Unhealthy(); |
| | | 42 | | } |
| | | 43 | | } |