-
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.
Merge pull request #1209 from riganti/health-checks
Add IHeathCheck implementation to Asp.Net Core hosting
- Loading branch information
Showing
7 changed files
with
121 additions
and
5 deletions.
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
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
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
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using DotVVM.Framework.Compilation; | ||
using DotVVM.Framework.Configuration; | ||
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; | ||
private readonly DotvvmConfiguration configuration; | ||
|
||
public DotvvmHealthCheck( | ||
IDotvvmViewCompilationService compilationService, | ||
DotvvmConfiguration configuration | ||
) | ||
{ | ||
this.compilationService = compilationService; | ||
this.configuration = configuration; | ||
} | ||
|
||
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); | ||
var statusPageUrl = configuration.Diagnostics.CompilationPage.Url; | ||
return Task.FromResult(new HealthCheckResult(state, $"Dothtml pages can not be compiled: {views.Take(10).StringJoin(", ")}. See {statusPageUrl} 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