Skip to content

Commit

Permalink
Add health-check to filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
ajami1331 committed Feb 3, 2024
1 parent b0ef1c3 commit 41cd7bd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 8 additions & 1 deletion sojj/HealthChecks/HealthCheckPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ public class HealthCheckPublisher : IHealthCheckPublisher
{
private readonly ILogger<HealthCheckPublisher> _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly string _healthCheckFilePath;

public HealthCheckPublisher(ILogger<HealthCheckPublisher> logger, IHttpClientFactory httpClientFactory)
public HealthCheckPublisher(ILogger<HealthCheckPublisher> logger, IHttpClientFactory httpClientFactory, IConfiguration configuration)
{
_logger = logger;
_httpClientFactory = httpClientFactory;
var healthCheckFilePathString = configuration.GetValue<string>("HealthCheckFilePath") ?? throw new ArgumentNullException("HealthCheckFilePath");
Directory.CreateDirectory(healthCheckFilePathString);
_healthCheckFilePath = Path.Join(healthCheckFilePathString, Environment.MachineName);
}

public async Task PublishAsync(HealthReport report, CancellationToken cancellationToken)
Expand All @@ -29,9 +33,12 @@ public async Task PublishAsync(HealthReport report, CancellationToken cancellati

if (report.Status != HealthStatus.Healthy)
{
File.Delete(_healthCheckFilePath);
return;
}

File.Create(_healthCheckFilePath).Dispose();

var response = await httpClient.GetAsync(string.Empty, cancellationToken);

if (response.IsSuccessStatusCode)
Expand Down
3 changes: 2 additions & 1 deletion sojj/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
{ "Name": "Console" }
],
"Enrich": [ "FromLogContext", "WithMachineName", "FromGlobalLogContext" ]
}
},
"HealthCheckFilePath": "/healthchecks/",
}

0 comments on commit 41cd7bd

Please sign in to comment.