Browse Source

- Add redis health check

- Add health checks on startup
pull/2106/head
David Fowler 1 year ago
parent
commit
4653a771f6
6 changed files with 57 additions and 14 deletions
  1. +1
    -0
      src/Services/Basket/Basket.API/Basket.API.csproj
  2. +7
    -0
      src/Services/Basket/Basket.API/CustomExtensionMethods.cs
  3. +30
    -10
      src/Services/Basket/Basket.API/Program.cs
  4. +5
    -0
      src/Services/Basket/Basket.API/Properties/serviceDependencies.json
  5. +10
    -0
      src/Services/Basket/Basket.API/Properties/serviceDependencies.local.json
  6. +4
    -4
      src/Services/Services.Common/CommonExtensions.cs

+ 1
- 0
src/Services/Basket/Basket.API/Basket.API.csproj View File

@ -7,6 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Grpc.AspNetCore" /> <PackageReference Include="Grpc.AspNetCore" />
<PackageReference Include="AspNetCore.HealthChecks.Redis" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
</ItemGroup> </ItemGroup>


+ 7
- 0
src/Services/Basket/Basket.API/CustomExtensionMethods.cs View File

@ -4,6 +4,13 @@ public static class CustomExtensionMethods
{ {
public static IServiceCollection AddRedis(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddRedis(this IServiceCollection services, IConfiguration configuration)
{ {
services.AddHealthChecks()
.AddRedis(_ =>
{
return configuration.GetConnectionString("redis");
},
"redis", tags: new[] { "ready", "liveness" });
return services.AddSingleton(sp => return services.AddSingleton(sp =>
{ {
var redisConfig = ConfigurationOptions.Parse(configuration.GetConnectionString("redis"), true); var redisConfig = ConfigurationOptions.Parse(configuration.GetConnectionString("redis"), true);


+ 30
- 10
src/Services/Basket/Basket.API/Program.cs View File

@ -1,4 +1,6 @@
var builder = WebApplication.CreateBuilder(args);
using Microsoft.Extensions.Diagnostics.HealthChecks;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults(); builder.AddServiceDefaults();
@ -23,20 +25,38 @@ builder.Services.AddTransient<IIdentityService, IdentityService>();
var app = builder.Build(); var app = builder.Build();
app.UseServiceDefaults();
try
{
app.Logger.LogInformation("Running health checks...");
app.MapGet("/", () => Results.Redirect("/swagger"));
// Do a health check on startup, this will throw an exception if any of the checks fail
var report = await app.Services.GetRequiredService<HealthCheckService>().CheckHealthAsync();
app.MapGrpcService<BasketService>();
app.MapControllers();
if (report.Status == HealthStatus.Unhealthy)
{
app.Logger.LogCritical("Health checks failed!");
foreach (var entry in report.Entries)
{
if (entry.Value.Status == HealthStatus.Unhealthy)
{
app.Logger.LogCritical("{Check}: {Status}", entry.Key, entry.Value.Status);
}
}
return 1;
}
var eventBus = app.Services.GetRequiredService<IEventBus>();
app.UseServiceDefaults();
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>();
eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>();
app.MapGet("/", () => Results.Redirect("/swagger"));
app.MapGrpcService<BasketService>();
app.MapControllers();
var eventBus = app.Services.GetRequiredService<IEventBus>();
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>();
eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>();
try
{
await app.RunAsync(); await app.RunAsync();
return 0; return 0;


+ 5
- 0
src/Services/Basket/Basket.API/Properties/serviceDependencies.json View File

@ -7,6 +7,11 @@
"type": "rabbitmq", "type": "rabbitmq",
"connectionId": "eventbus", "connectionId": "eventbus",
"dynamicId": null "dynamicId": null
},
"redis1": {
"type": "redis",
"connectionId": "ConnectionStrings:Redis",
"dynamicId": null
} }
} }
} }

+ 10
- 0
src/Services/Basket/Basket.API/Properties/serviceDependencies.local.json View File

@ -11,6 +11,16 @@
"type": "rabbitmq.container", "type": "rabbitmq.container",
"connectionId": "eventbus", "connectionId": "eventbus",
"dynamicId": null "dynamicId": null
},
"redis1": {
"serviceConnectorResourceId": "",
"containerPorts": "6379:6379",
"secretStore": "LocalSecretsFile",
"containerName": "basket-redis",
"containerImage": "redis:alpine",
"type": "redis.container",
"connectionId": "ConnectionStrings:Redis",
"dynamicId": null
} }
} }
} }

+ 4
- 4
src/Services/Services.Common/CommonExtensions.cs View File

@ -283,13 +283,13 @@ public static class CommonExtensions
"servicebus" => hcBuilder.AddAzureServiceBusTopic( "servicebus" => hcBuilder.AddAzureServiceBusTopic(
eventBusConnectionString, eventBusConnectionString,
topicName: "eshop_event_bus", topicName: "eshop_event_bus",
name: "servicebus-check",
tags: new string[] { "servicebus" }),
name: "servicebus",
tags: new string[] { "ready" }),
_ => hcBuilder.AddRabbitMQ( _ => hcBuilder.AddRabbitMQ(
$"amqp://{eventBusConnectionString}", $"amqp://{eventBusConnectionString}",
name: "rabbitmqbus-check",
tags: new string[] { "rabbitmqbus" })
name: "rabbitmq",
tags: new string[] { "ready" })
}; };
} }


Loading…
Cancel
Save