Skip to content

Commit 1bf6579

Browse files
committed
Health check for CHES
1 parent d14ca2e commit 1bf6579

11 files changed

Lines changed: 101 additions & 4 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Diagnostics.HealthChecks;
5+
using Pims.Api.Repositories.Ches;
6+
7+
namespace Pims.Api.Helpers.Healthchecks
8+
{
9+
/// <summary>
10+
/// Health check for CHES service connectivity.
11+
/// </summary>
12+
public class ChesHealthCheck : IHealthCheck
13+
{
14+
private readonly IEmailRepository _repository;
15+
16+
public ChesHealthCheck(IEmailRepository repository)
17+
{
18+
_repository = repository;
19+
}
20+
21+
public async Task<HealthCheckResult> CheckHealthAsync(
22+
HealthCheckContext context, CancellationToken cancellationToken = default)
23+
{
24+
try
25+
{
26+
const int maxJitterMilliseconds = 10000;
27+
var jitter = Random.Shared.Next(0, maxJitterMilliseconds + 1);
28+
if (jitter > 0)
29+
{
30+
await Task.Delay(TimeSpan.FromMilliseconds(jitter), cancellationToken);
31+
}
32+
33+
var response = await _repository.TryGetHealthAsync();
34+
if (response.StatusCode != System.Net.HttpStatusCode.OK)
35+
{
36+
return new HealthCheckResult(HealthStatus.Degraded, $"CHES health check returned status code: {response.StatusCode}");
37+
}
38+
}
39+
catch (Exception ex)
40+
{
41+
return new HealthCheckResult(context.Registration.FailureStatus, $"CHES health check failed with exception: {ex.Message}");
42+
}
43+
return HealthCheckResult.Healthy();
44+
}
45+
}
46+
}

source/backend/api/Repositories/Ches/ChesAuthRepository.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ private async Task<ExternalResponse<JwtResponse>> TryRequestToken()
8080
{
8181
_logger.LogDebug("Getting authentication token...");
8282

83-
using HttpClient client = _httpClientFactory.CreateClient("Pims.Api.Logging");
84-
client.DefaultRequestHeaders.Accept.Clear();
85-
8683
var requestForm = new Dictionary<string, string>
8784
{
8885
{ "grant_type", "client_credentials" },

source/backend/api/Repositories/Ches/ChesRepository.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,18 @@ public async Task<ExternalResponse<EmailResponse>> SendEmailAsync(EmailRequest r
107107
_logger.LogDebug($"Finished sending email");
108108
return result;
109109
}
110+
111+
public async Task<HttpResponseMessage> TryGetHealthAsync()
112+
{
113+
_logger.LogDebug("Checking health of CHES service");
114+
string authenticationToken = await _authRepository.GetTokenAsync();
115+
116+
Uri endpoint = new(this._config.ChesHost, "/api/v1/health");
117+
118+
Task<HttpResponseMessage> result = GetRawAsync(endpoint, authenticationToken);
119+
120+
_logger.LogDebug($"Finished checking health of CHES service");
121+
return await result;
122+
}
110123
}
111124
}

source/backend/api/Repositories/Ches/IEmailRepository.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Net.Http;
12
using System.Threading.Tasks;
23
using Pims.Api.Models.Ches;
34
using Pims.Api.Models.Requests.Http;
@@ -7,5 +8,7 @@ namespace Pims.Api.Repositories.Ches
78
public interface IEmailRepository
89
{
910
Task<ExternalResponse<EmailResponse>> SendEmailAsync(EmailRequest request);
11+
12+
Task<HttpResponseMessage> TryGetHealthAsync();
1013
}
1114
}

source/backend/api/Startup.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,16 @@ public void ConfigureServices(IServiceCollection services)
373373
{ Period = TimeSpan.FromMinutes(allHealthCheckOptions.Cdogs.Period) });
374374
}
375375

376+
if (allHealthCheckOptions.Ches.Enabled)
377+
{
378+
services.AddHealthChecks().Add(new HealthCheckRegistration(
379+
"Ches",
380+
sp => new ChesHealthCheck(sp.GetService<IEmailRepository>()),
381+
null,
382+
new string[] { SERVICES, EXTERNAL, SYSTEMCHECK })
383+
{ Period = TimeSpan.FromMinutes(allHealthCheckOptions.Ches.Period) });
384+
}
385+
376386
services.AddApiVersioning(options =>
377387
{
378388
options.ReportApiVersions = true;
@@ -574,7 +584,7 @@ private static void AddPimsApiServices(IServiceCollection services)
574584
services.AddScoped<IManagementActivityService, ManagementActivityService>();
575585
services.AddScoped<IManagementFileStatusSolver, ManagementFileStatusSolver>();
576586
services.AddScoped<IFilePropertyLocationUpdateSolver, FilePropertyLocationUpdateSolver>();
577-
services.AddScoped<IChesService, ChesService>();
587+
services.AddScoped<IEmailService, ChesService>();
578588
}
579589

580590
/// <summary>

source/backend/api/appsettings.Development.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
"Ltsa": {
1717
"Enabled": false,
1818
"Period": 60
19+
},
20+
"Ches": {
21+
"Enabled": false,
22+
"Period": 60
1923
}
2024
},
2125
"Serilog": {

source/backend/api/appsettings.Local.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
"Mayan": {
2727
"Period": 60,
2828
"Enabled": false
29+
},
30+
"Ches": {
31+
"Period": 60,
32+
"Enabled": false
2933
}
3034
},
3135
"Logging": {

source/backend/api/appsettings.Test.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
"Ltsa": {
1717
"Enabled": false,
1818
"Period": 15
19+
},
20+
"Ches": {
21+
"Enabled": false,
22+
"Period": 15
1923
}
2024
},
2125
"Serilog": {

source/backend/api/appsettings.Uat.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
"Ltsa": {
1717
"Enabled": false,
1818
"Period": 5
19+
},
20+
"Ches": {
21+
"Enabled": false,
22+
"Period": 5
1923
}
2024
},
2125
"Serilog": {

source/backend/api/appsettings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
"Cdogs": {
4242
"Enabled": true,
4343
"Period": 1
44+
},
45+
"Ches": {
46+
"Enabled": true,
47+
"Period": 1
4448
}
4549
},
4650
"Swagger": {
@@ -153,6 +157,12 @@
153157
"ServiceClientId": "[CLIENT_ID]",
154158
"ServiceClientSecret": "[CLIENT_SECRET]"
155159
},
160+
"Ches": {
161+
"AuthEndpoint": "[AUTH_ENDPOINT]",
162+
"ChesHost": "[CDOGS_HOST]",
163+
"ServiceClientId": "[CLIENT_ID]",
164+
"ServiceClientSecret": "[CLIENT_SECRET]"
165+
},
156166
"Polly": {
157167
"MaxRetries": 3,
158168
"DelayInSeconds": 1

0 commit comments

Comments
 (0)