Add healthchecks to the api

This commit is contained in:
Zoe Roux 2023-04-01 23:24:43 +09:00
parent cbcc2f30b7
commit a554b7681f
6 changed files with 78 additions and 2 deletions

View File

@ -23,11 +23,12 @@ ARG VERSION
RUN dotnet publish --no-restore -c Release -o /app "-p:Version=${VERSION:-"0.0.0-dev"};SkipTranscoder=true" src/Kyoo.Host
FROM mcr.microsoft.com/dotnet/aspnet:6.0
RUN apt-get update && apt-get install -y libavutil-dev libavcodec-dev libavformat-dev
RUN apt-get update && apt-get install -y libavutil-dev libavcodec-dev libavformat-dev curl
COPY --from=builder /app /app
COPY --from=transcoder /transcoder/libtranscoder.so /app
WORKDIR /kyoo
EXPOSE 5000
HEALTHCHECK CMD curl --fail http://localhost:5000/health || exit
CMD /app/Kyoo.Host

View File

@ -5,7 +5,7 @@ COPY src/Kyoo.Transcoder .
RUN cmake . && make -j
FROM mcr.microsoft.com/dotnet/sdk:6.0
RUN apt-get update && apt-get install -y libavutil-dev libavcodec-dev libavformat-dev
RUN apt-get update && apt-get install -y libavutil-dev libavcodec-dev libavformat-dev curl
WORKDIR /app
COPY Kyoo.sln ./Kyoo.sln
@ -25,5 +25,6 @@ COPY --from=transcoder /transcoder/libtranscoder.so /app
WORKDIR /kyoo
EXPOSE 5000
ENV DOTNET_USE_POLLING_FILE_WATCHER 1
HEALTHCHECK CMD curl --fail http://localhost:5000/health || exit
CMD dotnet watch run --no-restore --project /app/src/Kyoo.Host

View File

@ -0,0 +1,70 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System.Threading.Tasks;
using Kyoo.Abstractions.Models.Attributes;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace Kyoo.Core.Api
{
/// <summary>
/// An API endpoint to check the health.
/// </summary>
[Route("health")]
[ApiController]
[ApiDefinition("Health")]
public class Health : BaseApi
{
private readonly HealthCheckService _healthCheckService;
/// <summary>
/// Create a new <see cref="Health"/>.
/// </summary>
/// <param name="healthCheckService">The service to check health.</param>
public Health(HealthCheckService healthCheckService)
{
_healthCheckService = healthCheckService;
}
/// <summary>
/// Check if the api is ready to accept requests.
/// </summary>
/// <returns>A status indicating the health of the api.</returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
public async Task<IActionResult> CheckHealth()
{
IHeaderDictionary headers = HttpContext.Response.Headers;
headers.CacheControl = "no-store, no-cache";
headers.Pragma = "no-cache";
headers.Expires = "Thu, 01 Jan 1970 00:00:00 GMT";
HealthReport result = await _healthCheckService.CheckHealthAsync();
return result.Status switch
{
HealthStatus.Healthy => NoContent(),
HealthStatus.Unhealthy => NoContent(),
HealthStatus.Degraded => StatusCode(StatusCodes.Status503ServiceUnavailable),
_ => StatusCode(StatusCodes.Status500InternalServerError),
};
}
}
}

View File

@ -24,6 +24,7 @@ using System.Threading.Tasks;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Kyoo.Postgresql;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

View File

@ -10,6 +10,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="5.0.7" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.8" />
</ItemGroup>

View File

@ -91,6 +91,8 @@ namespace Kyoo.Postgresql
if (_environment.IsDevelopment())
x.EnableDetailedErrors().EnableSensitiveDataLogging();
}, ServiceLifetime.Transient);
services.AddHealthChecks().AddDbContextCheck<DatabaseContext>();
}
}
}