Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #49 from rinkudesu/healthchecks
Browse files Browse the repository at this point in the history
Add healthchecks
  • Loading branch information
KowalskiPiotr98 authored Sep 6, 2021
2 parents aae2eca + 2629f6b commit df81e33
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
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();
}
}
}
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>
6 changes: 6 additions & 0 deletions Rinkudesu.Services.Links/Rinkudesu.Services.Links.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rinkudesu.Services.Links.Mo
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rinkudesu.Services.Links.Data", "Rinkudesu.Services.Links.Data\Rinkudesu.Services.Links.Data.csproj", "{26A791B3-4BAA-4F25-8AE7-AAF90BEC4594}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rinkudesu.Services.Links.HealthChecks", "Rinkudesu.Services.Links.HealthChecks\Rinkudesu.Services.Links.HealthChecks.csproj", "{E43C4BCB-2BB1-4602-A5BC-AB7216F679E2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -42,5 +44,9 @@ Global
{26A791B3-4BAA-4F25-8AE7-AAF90BEC4594}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26A791B3-4BAA-4F25-8AE7-AAF90BEC4594}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26A791B3-4BAA-4F25-8AE7-AAF90BEC4594}.Release|Any CPU.Build.0 = Release|Any CPU
{E43C4BCB-2BB1-4602-A5BC-AB7216F679E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E43C4BCB-2BB1-4602-A5BC-AB7216F679E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E43C4BCB-2BB1-4602-A5BC-AB7216F679E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E43C4BCB-2BB1-4602-A5BC-AB7216F679E2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS final
EXPOSE 80
WORKDIR /app
COPY --from=publish /app/publish .
HEALTHCHECK --interval=20s --start-period=5s --retries=3 CMD curl --fail http://localhost/health || exit 1
ENTRYPOINT ["dotnet", "Rinkudesu.Services.Links.dll"]
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
<PackageVersion>0.0.0</PackageVersion>
<AssemblyVersion>0.0.0</AssemblyVersion>
<PackageVersion>0.2.0</PackageVersion>
<AssemblyVersion>0.2.0</AssemblyVersion>
<FileVersion></FileVersion>
<Authors>Rinkudesu</Authors>
<Company>Rinkudesu</Company>
Expand Down Expand Up @@ -43,6 +43,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Rinkudesu.Services.Links.HealthChecks\Rinkudesu.Services.Links.HealthChecks.csproj" />
<ProjectReference Include="..\Rinkudesu.Services.Links.Models\Rinkudesu.Services.Links.Models.csproj" />
<ProjectReference Include="..\Rinkudesu.Services.Links.Repositories\Rinkudesu.Services.Links.Repositories.csproj" />
<ProjectReference Include="..\Rinkudesu.Services.Links.Utilities\Rinkudesu.Services.Links.Utilities.csproj" />
Expand Down
8 changes: 7 additions & 1 deletion Rinkudesu.Services.Links/Rinkudesu.Services.Links/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.OpenApi.Models;
using Rinkudesu.Services.Links.Data;
using Rinkudesu.Services.Links.DataTransferObjects;
using Rinkudesu.Services.Links.HealthChecks;
using Rinkudesu.Services.Links.Repositories;
using Rinkudesu.Services.Links.Utilities;
using Serilog;
Expand Down Expand Up @@ -68,6 +69,8 @@ public void ConfigureServices(IServiceCollection services)
var xmlPath = AppContext.BaseDirectory;
c.IncludeXmlComments(Path.Combine(xmlPath, xmlName));
});

services.AddHealthChecks().AddCheck<DatabaseHealthCheck>("Database health check");
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -91,7 +94,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseAuthorization();

app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
endpoints.MapHealthChecks("/health");
});

if (InputArguments.Current.ApplyMigrations)
{
Expand Down

0 comments on commit df81e33

Please sign in to comment.