diff --git a/back/Dockerfile b/back/Dockerfile index f45ae157..1a3818d2 100644 --- a/back/Dockerfile +++ b/back/Dockerfile @@ -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 diff --git a/back/Dockerfile.dev b/back/Dockerfile.dev index 6e5b2ffa..778f9018 100644 --- a/back/Dockerfile.dev +++ b/back/Dockerfile.dev @@ -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 diff --git a/back/src/Kyoo.Core/Views/Health.cs b/back/src/Kyoo.Core/Views/Health.cs new file mode 100644 index 00000000..1ee3aa26 --- /dev/null +++ b/back/src/Kyoo.Core/Views/Health.cs @@ -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 . + +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 +{ + /// + /// An API endpoint to check the health. + /// + [Route("health")] + [ApiController] + [ApiDefinition("Health")] + public class Health : BaseApi + { + private readonly HealthCheckService _healthCheckService; + + /// + /// Create a new . + /// + /// The service to check health. + public Health(HealthCheckService healthCheckService) + { + _healthCheckService = healthCheckService; + } + + /// + /// Check if the api is ready to accept requests. + /// + /// A status indicating the health of the api. + [HttpGet] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)] + public async Task 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), + }; + } + } +} diff --git a/back/src/Kyoo.Host/Application.cs b/back/src/Kyoo.Host/Application.cs index e180aed6..284ee9ec 100644 --- a/back/src/Kyoo.Host/Application.cs +++ b/back/src/Kyoo.Host/Application.cs @@ -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; diff --git a/back/src/Kyoo.Postgresql/Kyoo.Postgresql.csproj b/back/src/Kyoo.Postgresql/Kyoo.Postgresql.csproj index 54e34601..4c785d6b 100644 --- a/back/src/Kyoo.Postgresql/Kyoo.Postgresql.csproj +++ b/back/src/Kyoo.Postgresql/Kyoo.Postgresql.csproj @@ -10,6 +10,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/back/src/Kyoo.Postgresql/PostgresModule.cs b/back/src/Kyoo.Postgresql/PostgresModule.cs index 7129cc73..34289c10 100644 --- a/back/src/Kyoo.Postgresql/PostgresModule.cs +++ b/back/src/Kyoo.Postgresql/PostgresModule.cs @@ -91,6 +91,8 @@ namespace Kyoo.Postgresql if (_environment.IsDevelopment()) x.EnableDetailedErrors().EnableSensitiveDataLogging(); }, ServiceLifetime.Transient); + + services.AddHealthChecks().AddDbContextCheck(); } } }