diff --git a/Jellyfin.Server/HealthChecks/JellyfinDbHealthCheck.cs b/Jellyfin.Server/HealthChecks/JellyfinDbHealthCheck.cs
new file mode 100644
index 000000000..aea684479
--- /dev/null
+++ b/Jellyfin.Server/HealthChecks/JellyfinDbHealthCheck.cs
@@ -0,0 +1,36 @@
+using System.Threading;
+using System.Threading.Tasks;
+using Jellyfin.Server.Implementations;
+using Microsoft.Extensions.Diagnostics.HealthChecks;
+
+namespace Jellyfin.Server.HealthChecks
+{
+ ///
+ /// Checks connectivity to the database.
+ ///
+ public class JellyfinDbHealthCheck : IHealthCheck
+ {
+ private readonly JellyfinDbProvider _dbProvider;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The jellyfin db provider.
+ public JellyfinDbHealthCheck(JellyfinDbProvider dbProvider)
+ {
+ _dbProvider = dbProvider;
+ }
+
+ ///
+ public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
+ {
+ await using var jellyfinDb = _dbProvider.CreateContext();
+ if (await jellyfinDb.Database.CanConnectAsync(cancellationToken).ConfigureAwait(false))
+ {
+ return HealthCheckResult.Healthy("Database connection successful.");
+ }
+
+ return HealthCheckResult.Unhealthy("Unable to connect to the database.");
+ }
+ }
+}
diff --git a/Jellyfin.Server/Jellyfin.Server.csproj b/Jellyfin.Server/Jellyfin.Server.csproj
index 5e85ff4f1..6ca370d04 100644
--- a/Jellyfin.Server/Jellyfin.Server.csproj
+++ b/Jellyfin.Server/Jellyfin.Server.csproj
@@ -43,6 +43,7 @@
+
diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs
index 995271aa3..9e456de12 100644
--- a/Jellyfin.Server/Startup.cs
+++ b/Jellyfin.Server/Startup.cs
@@ -3,9 +3,9 @@ using System.ComponentModel;
using System.Net.Http.Headers;
using Jellyfin.Api.TypeConverters;
using Jellyfin.Server.Extensions;
+using Jellyfin.Server.HealthChecks;
using Jellyfin.Server.Middleware;
using Jellyfin.Server.Models;
-using MediaBrowser.Common;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
@@ -77,6 +77,9 @@ namespace Jellyfin.Server
c.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue($"({_serverApplicationHost.ApplicationUserAgentAddress})"));
})
.ConfigurePrimaryHttpMessageHandler(x => new DefaultHttpClientHandler());
+
+ services.AddHealthChecks()
+ .AddCheck("JellyfinDb");
}
///
@@ -132,6 +135,8 @@ namespace Jellyfin.Server
{
endpoints.MapMetrics(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/metrics");
}
+
+ endpoints.MapHealthChecks(_serverConfigurationManager.Configuration.BaseUrl.TrimStart('/') + "/health");
});
// Add type descriptor for legacy datetime parsing.