Project
This commit is contained in:
15
HealtRegistry/Containerfile
Normal file
15
HealtRegistry/Containerfile
Normal file
@@ -0,0 +1,15 @@
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
|
||||
WORKDIR /build
|
||||
|
||||
# Copy everything
|
||||
COPY . ./
|
||||
# Restore as distinct layers
|
||||
RUN dotnet restore
|
||||
# Build and publish a release
|
||||
RUN dotnet publish -c Release -o out
|
||||
|
||||
# Build runtime image
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0
|
||||
WORKDIR /app
|
||||
COPY --from=build-env /build/out .
|
||||
ENTRYPOINT ["dotnet", "HealtRegistry.dll"]
|
||||
15
HealtRegistry/HealtRegistry.csproj
Normal file
15
HealtRegistry/HealtRegistry.csproj
Normal file
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
18
HealtRegistry/HealtRegistry.http
Normal file
18
HealtRegistry/HealtRegistry.http
Normal file
@@ -0,0 +1,18 @@
|
||||
### Send health pdate for service 'test'
|
||||
POST http://127.0.0.1:5081/health/test HTTP/1.0
|
||||
|
||||
### Set health of service 'test' to unhealthy
|
||||
PUT http://127.0.0.1:5081/health/test HTTP/1.0
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"health": false
|
||||
}
|
||||
|
||||
### Set health of service 'test' to unhealthy
|
||||
PUT https://healthr.adix.link/health/doorbell HTTP/1.0
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"health": false
|
||||
}
|
||||
3
HealtRegistry/Models/HealthRequest.cs
Normal file
3
HealtRegistry/Models/HealthRequest.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace HealtRegistry.Models;
|
||||
|
||||
public record HealthRequest(bool? Health);
|
||||
3
HealtRegistry/Models/ServiceConfiguration.cs
Normal file
3
HealtRegistry/Models/ServiceConfiguration.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace HealtRegistry.Models;
|
||||
|
||||
public record ServiceConfiguration(bool DefaultValue, TimeSpan Timeout);
|
||||
7
HealtRegistry/Models/ServiceHealthData.cs
Normal file
7
HealtRegistry/Models/ServiceHealthData.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace HealtRegistry.Models;
|
||||
|
||||
public class ServiceHealthData
|
||||
{
|
||||
public required bool Healthy { get; set; }
|
||||
public required DateTimeOffset LastUpdated { get; set; }
|
||||
}
|
||||
4
HealtRegistry/Models/ServiceHealthes.cs
Normal file
4
HealtRegistry/Models/ServiceHealthes.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
namespace HealtRegistry.Models;
|
||||
|
||||
public record ServiceHealth(bool Health);
|
||||
public record ServiceHealthes(IReadOnlyDictionary<string, ServiceHealth> Healthes);
|
||||
8
HealtRegistry/Models/ServicesConfiguration.cs
Normal file
8
HealtRegistry/Models/ServicesConfiguration.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace HealtRegistry.Models;
|
||||
|
||||
public class ServicesConfiguration
|
||||
{
|
||||
[Required] public required Dictionary<string, ServiceConfiguration> Services { get; init; }
|
||||
}
|
||||
60
HealtRegistry/Program.cs
Normal file
60
HealtRegistry/Program.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using HealtRegistry.Models;
|
||||
using HealtRegistry.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddSingleton<IHealthService, HealthService>();
|
||||
|
||||
builder.Services.AddHostedService<HealthTimeoutService>();
|
||||
|
||||
builder.Services.AddOptions<ServicesConfiguration>()
|
||||
.Bind(builder.Configuration)
|
||||
.ValidateDataAnnotations();
|
||||
|
||||
if (builder.Environment.IsProduction())
|
||||
{
|
||||
builder.Configuration.AddJsonFile("/config/appsettings.json", optional: true, reloadOnChange: true);
|
||||
}
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.MapPost("/health/{service}", ([FromRoute] string service, IHealthService healthService) =>
|
||||
{
|
||||
healthService.SetServiceHealth(service, true);
|
||||
return Results.Ok();
|
||||
})
|
||||
.WithOpenApi();
|
||||
|
||||
app.MapPut("/health/{service}", ([FromRoute] string service, [FromBody] HealthRequest requestBody, IHealthService healthService, [FromServices] ILoggerFactory loggerFactory) =>
|
||||
{
|
||||
if (requestBody.Health.HasValue)
|
||||
{
|
||||
healthService.SetServiceHealth(service, requestBody.Health.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
var logger = loggerFactory.CreateLogger("Health");
|
||||
logger.LogDebug("Health request body does not contains 'Health' property");
|
||||
}
|
||||
return Results.Ok();
|
||||
})
|
||||
.WithOpenApi();
|
||||
|
||||
app.MapGet("/status", (IHealthService healthService) =>
|
||||
{
|
||||
return healthService.GetServiceHealthes();
|
||||
})
|
||||
.WithOpenApi();
|
||||
|
||||
app.Run();
|
||||
31
HealtRegistry/Properties/launchSettings.json
Normal file
31
HealtRegistry/Properties/launchSettings.json
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:48758",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5081",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": false,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
34
HealtRegistry/Services/HealthService.cs
Normal file
34
HealtRegistry/Services/HealthService.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using HealtRegistry.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace HealtRegistry.Services;
|
||||
|
||||
public class HealthService : IHealthService
|
||||
{
|
||||
|
||||
public Dictionary<string, ServiceHealthData> ServiceHealthes { get; } = new();
|
||||
|
||||
public HealthService(IOptions<ServicesConfiguration> servicesConfiguration)
|
||||
{
|
||||
foreach (var service in servicesConfiguration.Value.Services)
|
||||
{
|
||||
ServiceHealthes.Add(service.Key, new ServiceHealthData()
|
||||
{
|
||||
Healthy = service.Value.DefaultValue,
|
||||
LastUpdated = DateTimeOffset.Now
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public ServiceHealthes GetServiceHealthes()
|
||||
{
|
||||
var serviceHealthes = ServiceHealthes.ToDictionary(x => x.Key, x => new ServiceHealth(x.Value.Healthy));
|
||||
return new ServiceHealthes(serviceHealthes);
|
||||
}
|
||||
|
||||
public void SetServiceHealth(string serviceName, bool health)
|
||||
{
|
||||
ServiceHealthes[serviceName].Healthy = health;
|
||||
ServiceHealthes[serviceName].LastUpdated = DateTimeOffset.Now;
|
||||
}
|
||||
}
|
||||
51
HealtRegistry/Services/HealthTimeoutService.cs
Normal file
51
HealtRegistry/Services/HealthTimeoutService.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
using HealtRegistry.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace HealtRegistry.Services;
|
||||
|
||||
public class HealthTimeoutService : BackgroundService
|
||||
{
|
||||
private readonly IHealthService _healthService;
|
||||
private readonly IOptionsMonitor<ServicesConfiguration> _servicesConfiguration;
|
||||
private readonly ILogger<HealthTimeoutService> _logger;
|
||||
|
||||
public HealthTimeoutService(
|
||||
IHealthService healthService,
|
||||
IOptionsMonitor<ServicesConfiguration> servicesConfiguration,
|
||||
ILogger<HealthTimeoutService> logger
|
||||
)
|
||||
{
|
||||
_healthService = healthService;
|
||||
_servicesConfiguration = servicesConfiguration;
|
||||
_logger = logger;
|
||||
}
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var now = DateTimeOffset.Now;
|
||||
foreach (var service in _healthService.ServiceHealthes)
|
||||
{
|
||||
if (!service.Value.Healthy) continue;
|
||||
|
||||
var serviceConfiguration = _servicesConfiguration.CurrentValue.Services.TryGetValue(service.Key, out var configuration)
|
||||
? configuration
|
||||
: null;
|
||||
|
||||
if (serviceConfiguration is not null &&
|
||||
now - service.Value.LastUpdated > serviceConfiguration.Timeout)
|
||||
{
|
||||
_logger.LogInformation("Service {ServiceName} is timed out", service.Key);
|
||||
_healthService.SetServiceHealth(service.Key, false);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
10
HealtRegistry/Services/IHealthService.cs
Normal file
10
HealtRegistry/Services/IHealthService.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using HealtRegistry.Models;
|
||||
|
||||
namespace HealtRegistry.Services;
|
||||
|
||||
public interface IHealthService
|
||||
{
|
||||
Dictionary<string, ServiceHealthData> ServiceHealthes { get; }
|
||||
ServiceHealthes GetServiceHealthes();
|
||||
void SetServiceHealth(string serviceName, bool health);
|
||||
}
|
||||
14
HealtRegistry/appsettings.Development.json
Normal file
14
HealtRegistry/appsettings.Development.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"Services": {
|
||||
"test": {
|
||||
"DefaultValue": false,
|
||||
"Timeout": "00:00:30"
|
||||
}
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
HealtRegistry/appsettings.json
Normal file
9
HealtRegistry/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user