-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IHeathCheck implementation to Asp.Net Core hosting
The healthcheck just checks if dotvvm pages can be compiled.
- Loading branch information
Showing
4 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Framework/Hosting.AspNetCore/Hosting/DotvvmHealthCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using DotVVM.Framework.Compilation; | ||
using DotVVM.Framework.Utils; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace DotVVM.Framework.Hosting | ||
{ | ||
public class DotvvmHealthCheck : IHealthCheck | ||
{ | ||
private readonly IDotvvmViewCompilationService compilationService; | ||
|
||
public DotvvmHealthCheck(IDotvvmViewCompilationService compilationService) | ||
{ | ||
this.compilationService = compilationService; | ||
} | ||
|
||
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
var c = compilationService.GetRoutes().AddRange(compilationService.GetControls().AddRange(compilationService.GetMasterPages())); | ||
|
||
if (!c.Any(p => p.Status == CompilationState.CompilationFailed)) | ||
{ | ||
return Task.FromResult(new HealthCheckResult(HealthStatus.Healthy)); | ||
} | ||
|
||
// if nothing compiles, we say unhealthy. If at least something works, we say only degraded | ||
var state = | ||
c.Any(p => p.Status is CompilationState.CompletedSuccessfully or CompilationState.CompilationWarning) | ||
? HealthStatus.Degraded | ||
: HealthStatus.Unhealthy; | ||
|
||
var views = c.Where(p => p.Status == CompilationState.CompilationFailed) | ||
.Select(c => c.VirtualPath); | ||
return Task.FromResult(new HealthCheckResult(state, $"Dothtml pages can not be compiled: {views.Take(10).StringJoin(", ")}. See /_dotvvm/diagnostics/compilation for more information.")); | ||
} | ||
|
||
public static void RegisterHealthCheck(IServiceCollection services) | ||
{ | ||
services.ConfigureWithServices<HealthCheckServiceOptions>((options, s) => { | ||
if (options.Registrations.Any(c => c.Name == "DotVVM")) | ||
return; | ||
options.Registrations.Add( | ||
new HealthCheckRegistration( | ||
"DotVVM", | ||
ActivatorUtilities.CreateInstance<DotvvmHealthCheck>(s), | ||
null, | ||
new [] { "dotvvm" } | ||
) | ||
); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters