Created
March 9, 2021 06:11
-
-
Save NDiiong/385756c5d7dde22c7c76a1048a4cc854 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DbContextHealthCheck : IHealthCheck | |
{ | |
private readonly Func<string, DataContext> _dbContext; | |
private readonly List<ConsolidatorSettings> _consolidatorSettings; | |
public DbContextHealthCheck(Func<string, DataContext> dbContext, IOptions<List<ConsolidatorSettings>> consolidatorSettings) | |
{ | |
_dbContext = dbContext; | |
_consolidatorSettings = consolidatorSettings.Value; | |
} | |
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | |
{ | |
try | |
{ | |
foreach (var consolidator in _consolidatorSettings) | |
{ | |
var connected = await _dbContext(consolidator.Name).Database.CanConnectAsync(cancellationToken).ConfigureAwait(false); | |
if (!connected) | |
return HealthCheckResult.Unhealthy("Unhealthy"); | |
} | |
return HealthCheckResult.Healthy("Healthy"); | |
} | |
catch (Exception ex) | |
{ | |
return HealthCheckResult.Unhealthy("Unhealthy", ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment