Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Umbraco__CMS__Global__Smtp__Host=
Umbraco__CMS__Global__Smtp__Port=
Umbraco__CMS__Global__Smtp__Username=
Umbraco__CMS__Global__Smtp__Password=
Umbraco__CMS__Global__Smtp__SecureSocketOptions=StartTls
Sentry__Dsn=
CADDY_ENVIRONMENT=Development
HTTP_PORT=80
Expand Down
30 changes: 30 additions & 0 deletions SgfDevs/HealthChecks/ReadinessHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.Persistence;

namespace SgfDevs.HealthChecks;

internal sealed class ReadinessHealthCheck(
IRuntimeState runtimeState,
IUmbracoDatabaseFactory databaseFactory) : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default)
{
if (runtimeState.Level != RuntimeLevel.Run)
{
return Task.FromResult(HealthCheckResult.Unhealthy());
}

using var database = databaseFactory.CreateDatabase();
var result = database.ExecuteScalar<int>("SELECT 1");

return Task.FromResult(result == 1
? HealthCheckResult.Healthy()
: HealthCheckResult.Unhealthy());
}
}
19 changes: 19 additions & 0 deletions SgfDevs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Data.Common;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -10,6 +12,7 @@
using SgfDevs.Dev.EventSync;
using SgfDevs.Dev.EventSync.Meetup;
using SgfDevs.Dev.EventSync.Sessionize;
using SgfDevs.HealthChecks;
using SGFDevs.Dev;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Persistence.Sqlite;
Expand Down Expand Up @@ -44,6 +47,8 @@

umbracoBuilder.Build();

builder.Services.AddHealthChecks()
.AddCheck<ReadinessHealthCheck>("ready", tags: ["ready"]);
builder.Services.AddHttpClient();
builder.Services.Configure<EventSyncOptions>(builder.Configuration.GetSection("SGFDevs"));
builder.Services.AddScoped<MemberConverter>();
Expand Down Expand Up @@ -71,6 +76,20 @@

await app.BootUmbracoAsync();

var healthCheckOptions = new HealthCheckOptions
{
ResponseWriter = static (_, _) => Task.CompletedTask
};
app.MapHealthChecks("/health/live", new HealthCheckOptions
{
Predicate = static _ => false,
ResponseWriter = healthCheckOptions.ResponseWriter
}).AllowAnonymous();
app.MapHealthChecks("/health/ready", new HealthCheckOptions
{
Predicate = static check => check.Tags.Contains("ready"),
ResponseWriter = healthCheckOptions.ResponseWriter
}).AllowAnonymous();

app.UseUmbraco()
.WithMiddleware(u =>
Expand Down