-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
260b07f
commit a7ec744
Showing
7 changed files
with
102 additions
and
90 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
84 changes: 84 additions & 0 deletions
84
src/Monolith/ClassifiedAds.Infrastructure/HostedServices/HealthChecksBackgroundService.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,84 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.Infrastructure.HostedServices; | ||
|
||
public class HealthChecksBackgroundService : BackgroundService | ||
{ | ||
private readonly IServiceProvider _services; | ||
private readonly ILogger<HealthChecksBackgroundService> _logger; | ||
private readonly HealthCheckService _healthCheckService; | ||
|
||
public HealthChecksBackgroundService(IServiceProvider services, | ||
ILogger<HealthChecksBackgroundService> logger, | ||
HealthCheckService healthCheckService) | ||
{ | ||
_services = services; | ||
_logger = logger; | ||
_healthCheckService = healthCheckService; | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
var jsonOptions = new JsonWriterOptions { Indented = true }; | ||
|
||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
var healthReport = await _healthCheckService.CheckHealthAsync(stoppingToken); | ||
|
||
using var memoryStream = new MemoryStream(); | ||
using (var jsonWriter = new Utf8JsonWriter(memoryStream, jsonOptions)) | ||
{ | ||
jsonWriter.WriteStartObject(); | ||
jsonWriter.WriteString("status", healthReport.Status.ToString()); | ||
jsonWriter.WriteStartObject("results"); | ||
|
||
foreach (var healthReportEntry in healthReport.Entries) | ||
{ | ||
jsonWriter.WriteStartObject(healthReportEntry.Key); | ||
jsonWriter.WriteString("status", healthReportEntry.Value.Status.ToString()); | ||
jsonWriter.WriteString("description", healthReportEntry.Value.Description ?? healthReportEntry.Value.Exception?.Message.ToString()); | ||
jsonWriter.WriteStartObject("data"); | ||
|
||
foreach (var item in healthReportEntry.Value.Data) | ||
{ | ||
jsonWriter.WritePropertyName(item.Key); | ||
|
||
JsonSerializer.Serialize(jsonWriter, item.Value, item.Value?.GetType() ?? typeof(object)); | ||
} | ||
|
||
jsonWriter.WriteEndObject(); | ||
|
||
jsonWriter.WriteEndObject(); | ||
} | ||
|
||
jsonWriter.WriteEndObject(); | ||
jsonWriter.WriteEndObject(); | ||
} | ||
|
||
var json = Encoding.UTF8.GetString(memoryStream.ToArray()); | ||
|
||
if (healthReport.Status == HealthStatus.Healthy) | ||
{ | ||
_logger.LogInformation(json); | ||
} | ||
else if (healthReport.Status == HealthStatus.Degraded) | ||
{ | ||
_logger.LogWarning(json); | ||
} | ||
else | ||
{ | ||
_logger.LogError(json); | ||
} | ||
|
||
await Task.Delay(60_000, stoppingToken); | ||
} | ||
} | ||
} |
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