mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-23 15:30:34 -04:00
Add healthchecks to the api
This commit is contained in:
parent
cbcc2f30b7
commit
a554b7681f
@ -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
|
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
|
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=builder /app /app
|
||||||
COPY --from=transcoder /transcoder/libtranscoder.so /app
|
COPY --from=transcoder /transcoder/libtranscoder.so /app
|
||||||
|
|
||||||
WORKDIR /kyoo
|
WORKDIR /kyoo
|
||||||
EXPOSE 5000
|
EXPOSE 5000
|
||||||
|
HEALTHCHECK CMD curl --fail http://localhost:5000/health || exit
|
||||||
CMD /app/Kyoo.Host
|
CMD /app/Kyoo.Host
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ COPY src/Kyoo.Transcoder .
|
|||||||
RUN cmake . && make -j
|
RUN cmake . && make -j
|
||||||
|
|
||||||
FROM mcr.microsoft.com/dotnet/sdk:6.0
|
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
|
WORKDIR /app
|
||||||
|
|
||||||
COPY Kyoo.sln ./Kyoo.sln
|
COPY Kyoo.sln ./Kyoo.sln
|
||||||
@ -25,5 +25,6 @@ COPY --from=transcoder /transcoder/libtranscoder.so /app
|
|||||||
WORKDIR /kyoo
|
WORKDIR /kyoo
|
||||||
EXPOSE 5000
|
EXPOSE 5000
|
||||||
ENV DOTNET_USE_POLLING_FILE_WATCHER 1
|
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
|
CMD dotnet watch run --no-restore --project /app/src/Kyoo.Host
|
||||||
|
|
||||||
|
70
back/src/Kyoo.Core/Views/Health.cs
Normal file
70
back/src/Kyoo.Core/Views/Health.cs
Normal 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),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -24,6 +24,7 @@ using System.Threading.Tasks;
|
|||||||
using Autofac;
|
using Autofac;
|
||||||
using Autofac.Extensions.DependencyInjection;
|
using Autofac.Extensions.DependencyInjection;
|
||||||
using Kyoo.Postgresql;
|
using Kyoo.Postgresql;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="5.0.7" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.7" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.7" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.8" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.8" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -91,6 +91,8 @@ namespace Kyoo.Postgresql
|
|||||||
if (_environment.IsDevelopment())
|
if (_environment.IsDevelopment())
|
||||||
x.EnableDetailedErrors().EnableSensitiveDataLogging();
|
x.EnableDetailedErrors().EnableSensitiveDataLogging();
|
||||||
}, ServiceLifetime.Transient);
|
}, ServiceLifetime.Transient);
|
||||||
|
|
||||||
|
services.AddHealthChecks().AddDbContextCheck<DatabaseContext>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user