diff --git a/Btms.Backend/Btms.Backend.csproj b/Btms.Backend/Btms.Backend.csproj index a311c996..35eff8dc 100644 --- a/Btms.Backend/Btms.Backend.csproj +++ b/Btms.Backend/Btms.Backend.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -16,6 +16,7 @@ + diff --git a/Btms.Backend/Config/ServiceBusOptions.cs b/Btms.Backend/Config/ServiceBusOptions.cs new file mode 100644 index 00000000..700d3cd8 --- /dev/null +++ b/Btms.Backend/Config/ServiceBusOptions.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace Btms.Backend.Config; + +public class ServiceBusOptions +{ + public static readonly string SectionName = nameof(ServiceBusOptions); + + [Required] + public required string ConnectionString { get; set; } + + [Required] + public required string Topic { get; set; } + + [Required] + public required string Subscription { get; set; } +} \ No newline at end of file diff --git a/Btms.Backend/Endpoints/DiagnosticEndpoints.cs b/Btms.Backend/Endpoints/DiagnosticEndpoints.cs index 16e90fbb..209f3548 100644 --- a/Btms.Backend/Endpoints/DiagnosticEndpoints.cs +++ b/Btms.Backend/Endpoints/DiagnosticEndpoints.cs @@ -2,6 +2,9 @@ using Btms.Backend.Config; using Btms.BlobService; using Btms.Common.Extensions; +using HealthChecks.AzureServiceBus; +using HealthChecks.AzureServiceBus.Configuration; +using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Options; namespace Btms.Backend.Endpoints; @@ -15,18 +18,34 @@ public static void UseDiagnosticEndpoints(this IEndpointRouteBuilder app, IOptio if (options.Value.EnableManagement) { app.MapGet(BaseRoute + "/blob", GetBlobDiagnosticAsync).AllowAnonymous(); + app.MapGet(BaseRoute + "/asb", GetAzureServiceBusDiagnosticAsync).AllowAnonymous(); } } - private static async Task GetBlobDiagnosticAsync(IBlobService service - ) + private static async Task GetBlobDiagnosticAsync(IBlobService service) { var result = await service.CheckBlobAsync(5, 1); - Console.WriteLine(result.ToJson()); if (result.Success) { return Results.Ok(result); } return Results.Conflict(result); } + + private static async Task GetAzureServiceBusDiagnosticAsync(IOptions serviceBusOptions) + { + var options = new AzureServiceBusSubscriptionHealthCheckHealthCheckOptions(serviceBusOptions.Value.Topic, serviceBusOptions.Value.Subscription) + { + ConnectionString = serviceBusOptions.Value.ConnectionString + }; + + var healthCheck = new AzureServiceBusSubscriptionHealthCheck(options); + var result = await healthCheck.CheckHealthAsync(new HealthCheckContext()); + + if (result.Status == HealthStatus.Healthy) + { + return Results.Ok(result); + } + return Results.Conflict(result); + } } \ No newline at end of file diff --git a/Btms.Backend/Program.cs b/Btms.Backend/Program.cs index 9026e40c..fab64e53 100644 --- a/Btms.Backend/Program.cs +++ b/Btms.Backend/Program.cs @@ -78,7 +78,11 @@ static void ConfigureWebApplication(WebApplicationBuilder builder) .AddIniFile($"Properties/local.{builder.Environment.EnvironmentName}.env", true); } - builder.Services.BtmsAddOptions(builder.Configuration, ApiOptions.SectionName) + builder.Services + .AddOptions() + .Bind(builder.Configuration.GetSection(ServiceBusOptions.SectionName)); + +builder.Services.BtmsAddOptions(builder.Configuration, ApiOptions.SectionName) .PostConfigure(options => { builder.Configuration.Bind(options); @@ -214,7 +218,13 @@ static void ConfigureEndpoints(WebApplicationBuilder builder) { builder.Services.AddHealthChecks() .AddAzureBlobStorage(sp => sp.GetService()!.CreateBlobServiceClient(5, 1), timeout: TimeSpan.FromSeconds(15)) - .AddMongoDb(timeout: TimeSpan.FromSeconds(15)); + .AddMongoDb(timeout: TimeSpan.FromSeconds(15)) + .AddAzureServiceBusSubscription( + sp => sp.GetRequiredService>().Value.ConnectionString, + sp => sp.GetRequiredService>().Value.Topic, + sp => sp.GetRequiredService>().Value.Subscription, + configure: options => options.UsePeekMode = true, + timeout: TimeSpan.FromSeconds(15)); } [ExcludeFromCodeCoverage]