forked from OrchardCMS/OrchardCore
-
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 Redis Health Check (OrchardCMS#13589)
Co-authored-by: Zoltán Lehóczky <zoltan.lehoczky@lombiq.com>
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...chardCore.Modules/OrchardCore.Redis/HealthChecks/Extensions/RedisHealthCheckExtensions.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,9 @@ | ||
using OrchardCore.Redis.HealthChecks; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class RedisHealthCheckExtensions | ||
{ | ||
public static IHealthChecksBuilder AddRedisCheck(this IHealthChecksBuilder healthChecksBuilder) | ||
=> healthChecksBuilder.AddCheck<RedisHealthCheck>("Redis Health Check"); | ||
} |
54 changes: 54 additions & 0 deletions
54
src/OrchardCore.Modules/OrchardCore.Redis/HealthChecks/RedisHealthCheck.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,54 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace OrchardCore.Redis.HealthChecks; | ||
|
||
public class RedisHealthCheck : IHealthCheck | ||
{ | ||
private const int Timeout = 30; | ||
|
||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public RedisHealthCheck(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider; | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
var redisService = _serviceProvider.GetService<IRedisService>(); | ||
if (redisService == null) | ||
{ | ||
return HealthCheckResult.Unhealthy(description: $"The service '{nameof(IRedisService)}' isn't registered."); | ||
} | ||
|
||
if (redisService.Connection == null) | ||
{ | ||
await redisService.ConnectAsync(); | ||
} | ||
|
||
if (redisService.Connection.IsConnected) | ||
{ | ||
var time = await redisService.Database.PingAsync(); | ||
if (time > TimeSpan.FromSeconds(Timeout)) | ||
{ | ||
return HealthCheckResult.Unhealthy(description: $"The Redis server couldn't be reached within {Timeout} seconds and might be offline or have degraded performance."); | ||
} | ||
else | ||
{ | ||
return HealthCheckResult.Healthy(); | ||
} | ||
} | ||
else | ||
{ | ||
return HealthCheckResult.Unhealthy(description: "Couldn't connect to the Redis server."); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
return HealthCheckResult.Unhealthy("Retrieving the status of the Redis service failed.", ex); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/OrchardCore.Modules/OrchardCore.Redis/HealthChecks/Startup.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,19 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Modules; | ||
|
||
namespace OrchardCore.Redis.HealthChecks; | ||
|
||
[RequireFeatures("OrchardCore.HealthChecks")] | ||
public class Startup : StartupBase | ||
{ | ||
// The order of this startup configuration should be greater than zero to register the Redis check early, | ||
// so the health check can be reported alongside with other health checks in the system. | ||
public override int Order => 100; | ||
|
||
public override void ConfigureServices(IServiceCollection services) | ||
{ | ||
services | ||
.AddHealthChecks() | ||
.AddRedisCheck(); | ||
} | ||
} |
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