This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from rinkudesu/healthchecks
Add healthchecks
- Loading branch information
Showing
6 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Rinkudesu.Services.Links/Rinkudesu.Services.Links.HealthChecks/DatabaseHealthCheck.cs
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,35 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Rinkudesu.Services.Links.Data; | ||
|
||
namespace Rinkudesu.Services.Links.HealthChecks | ||
{ | ||
[ExcludeFromCodeCoverage] | ||
public class DatabaseHealthCheck : IHealthCheck | ||
{ | ||
private readonly LinkDbContext _context; | ||
|
||
public DatabaseHealthCheck(LinkDbContext context) | ||
{ | ||
this._context = context; | ||
} | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
if (!await _context.Database.CanConnectAsync(cancellationToken)) | ||
{ | ||
return HealthCheckResult.Unhealthy("Unable to connect to the database"); | ||
} | ||
var migrations = await _context.Database.GetPendingMigrationsAsync(cancellationToken); | ||
if (migrations?.Any() ?? false) | ||
{ | ||
return HealthCheckResult.Degraded("Database migrations are pending, functionality may be limited"); | ||
} | ||
return HealthCheckResult.Healthy(); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
....Links/Rinkudesu.Services.Links.HealthChecks/Rinkudesu.Services.Links.HealthChecks.csproj
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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Rinkudesu.Services.Links.Data\Rinkudesu.Services.Links.Data.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.18" /> | ||
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="3.1.18" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
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