forked from JudahGabriel/Chavah
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add raveb db health check issue JudahGabriel#89
- Loading branch information
Showing
7 changed files
with
154 additions
and
7 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
67 changes: 67 additions & 0 deletions
67
Chavah.NetCore/Common/HealthChecks/HealthCheckBuilderExtensions.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,67 @@ | ||
using BitShuva.Chavah.Models; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.HealthChecks; | ||
using Microsoft.Extensions.Options; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public static class HealthCheckBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Add a healthcheck for ravendb. | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <param name="services"></param> | ||
/// <param name="name"></param> | ||
/// <param name="failureStatus"></param> | ||
/// <param name="tags"></param> | ||
/// <returns></returns> | ||
public static IHealthChecksBuilder AddRavenDbCheck( | ||
this IHealthChecksBuilder builder, | ||
IServiceCollection services, | ||
string name = "ravendb", | ||
HealthStatus? failureStatus = default, | ||
IEnumerable<string> tags = default) | ||
{ | ||
var provider = services.BuildServiceProvider(); | ||
|
||
var options = provider.GetRequiredService<IOptions<AppSettings>>().Value.DbConnection; | ||
|
||
var hostingEnviroment = provider.GetRequiredService<IHostingEnvironment>(); | ||
|
||
builder.Add(new HealthCheckRegistration( | ||
name, | ||
sp => new RavenDdHealthCheck(options, hostingEnviroment), | ||
failureStatus, | ||
tags)); | ||
|
||
return builder; | ||
} | ||
|
||
internal static Task WriteResponse( | ||
HttpContext httpContext, | ||
HealthReport result) | ||
{ | ||
httpContext.Response.ContentType = "application/json"; | ||
|
||
var json = new JObject( | ||
new JProperty("status", result.Status.ToString()), | ||
new JProperty("results", new JObject(result.Entries.Select(pair => | ||
new JProperty(pair.Key, new JObject( | ||
new JProperty("status", pair.Value.Status.ToString()), | ||
new JProperty("description", pair.Value.Description), | ||
new JProperty("data", new JObject(pair.Value.Data.Select( | ||
p => new JProperty(p.Key, p.Value)))))))))); | ||
|
||
return httpContext.Response.WriteAsync( | ||
json.ToString(Formatting.Indented)); | ||
} | ||
} | ||
} |
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,68 @@ | ||
using BitShuva.Chavah.Models; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Raven.Client.Documents; | ||
using Raven.Client.ServerWide.Operations; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Extensions.HealthChecks | ||
{ | ||
public class RavenDdHealthCheck : IHealthCheck | ||
{ | ||
private readonly DbConnection _options; | ||
private readonly IHostingEnvironment _hostingEnvironment; | ||
|
||
public RavenDdHealthCheck(DbConnection options, IHostingEnvironment hostingEnvironment) | ||
{ | ||
_options = options ?? throw new ArgumentNullException(nameof(options)); | ||
_hostingEnvironment = hostingEnvironment ?? throw new ArgumentException(nameof(hostingEnvironment)); | ||
} | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync( | ||
HealthCheckContext context, | ||
CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
try | ||
{ | ||
using (var store = new DocumentStore | ||
{ | ||
Urls = new[] { _options.Url }, | ||
Database = _options.DatabaseName | ||
}) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(_options.CertFileName)) | ||
{ | ||
var certFilePath = Path.Combine(_hostingEnvironment.ContentRootPath, _options.CertFileName); | ||
store.Certificate = new X509Certificate2(certFilePath, _options.CertPassword); | ||
} | ||
|
||
store.Initialize(); | ||
|
||
var operation = new GetDatabaseNamesOperation(start: 0, pageSize: 100); | ||
var databaseNames = await store.Maintenance.Server.SendAsync(operation); | ||
|
||
if (!string.IsNullOrWhiteSpace(_options.DatabaseName) | ||
&& !databaseNames.Contains(_options.DatabaseName, StringComparer.OrdinalIgnoreCase)) | ||
{ | ||
return new HealthCheckResult( | ||
context.Registration.FailureStatus, | ||
$"RavenDB doesn't contains '{_options.DatabaseName}' database."); | ||
} | ||
else | ||
{ | ||
return HealthCheckResult.Healthy(); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex); | ||
} | ||
} | ||
} | ||
} |
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