-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace ModelRepoBrowser; | ||
|
||
/// <summary> | ||
/// HealthCheck for <see cref="DbUpdateService"/>. | ||
/// Reports a degraded health if the last model repository database update exited with an exception. | ||
/// </summary> | ||
public class DbUpdateServiceHealthCheck : IHealthCheck | ||
{ | ||
private volatile bool lastDbUpdateSuccessful; | ||
|
||
public bool LastDbUpdateSuccessful | ||
{ | ||
get { return lastDbUpdateSuccessful; } | ||
set { lastDbUpdateSuccessful = value; } | ||
} | ||
|
||
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
if (lastDbUpdateSuccessful) | ||
{ | ||
return Task.FromResult(HealthCheckResult.Healthy()); | ||
} | ||
else | ||
{ | ||
return Task.FromResult(HealthCheckResult.Degraded()); | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace ModelRepoBrowser; | ||
|
||
/// <summary> | ||
/// Health check for model repository database. Checks whether the database is accessible and contains data. | ||
/// </summary> | ||
public class RepoBrowserDbHealthCheck : IHealthCheck | ||
{ | ||
private readonly RepoBrowserContext repoBrowserContext; | ||
|
||
public RepoBrowserDbHealthCheck(RepoBrowserContext context) | ||
{ | ||
repoBrowserContext = context; | ||
} | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
var canConnectToDb = await repoBrowserContext.Database.CanConnectAsync(cancellationToken).ConfigureAwait(false); | ||
var dbContainsData = false; | ||
|
||
if (canConnectToDb) | ||
{ | ||
dbContainsData = repoBrowserContext.Models.Any() && repoBrowserContext.Repositories.Any() && repoBrowserContext.Catalogs.Any(); | ||
} | ||
|
||
if (canConnectToDb && dbContainsData) | ||
{ | ||
return HealthCheckResult.Healthy(); | ||
} | ||
else | ||
{ | ||
return HealthCheckResult.Unhealthy(); | ||
} | ||
} | ||
} |