Skip to content

Commit

Permalink
Merge pull request #18 from DEFRA/Update-Service-Bus-Details
Browse files Browse the repository at this point in the history
updated asb to use WebProxy which was part of TDM
  • Loading branch information
t11omas authored Dec 13, 2024
2 parents f214701 + 48ee0c6 commit 0782d4b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
39 changes: 39 additions & 0 deletions Btms.Backend/Asb/AzureServiceBusHealthCheckBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Btms.Backend.Config;
using HealthChecks.AzureServiceBus;
using HealthChecks.AzureServiceBus.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
using System.Net;

namespace Btms.Backend.Asb;

/// <summary>
/// Extension methods to configure
/// <see cref="AzureServiceBusHealthCheck{TOptions}"/>, <see cref="AzureServiceBusQueueHealthCheck"/>,
/// <see cref="AzureServiceBusSubscriptionHealthCheck"/>, <see cref="AzureServiceBusTopicHealthCheck"/>,
/// <see cref="AzureServiceBusQueueMessageCountThresholdHealthCheck"/>.
/// </summary>
public static class AzureServiceBusHealthCheckBuilderExtensions
{
public static IHealthChecksBuilder AddBtmsAzureServiceBusSubscription(
this IHealthChecksBuilder builder,
TimeSpan? timeout = default)
{

return builder.Add(new HealthCheckRegistration(
"azuresubscription",
sp =>
{
var sbOptions = sp.GetRequiredService<IOptions<ServiceBusOptions>>();
var options = new AzureServiceBusSubscriptionHealthCheckHealthCheckOptions(sbOptions.Value.Topic, sbOptions.Value.Subscription)
{
ConnectionString = sbOptions.Value.ConnectionString,
UsePeekMode = true
};
return new AzureServiceBusSubscriptionHealthCheck(options, new BtmsServiceBusClientProvider(sp.GetRequiredService<IWebProxy>()));
},
default,
default,
timeout));
}
}
22 changes: 22 additions & 0 deletions Btms.Backend/Asb/BtmsServiceBusClientProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Net;
using Azure.Messaging.ServiceBus.Administration;
using Azure.Messaging.ServiceBus;
using HealthChecks.AzureServiceBus;
using System;

namespace Btms.Backend.Asb;

public class BtmsServiceBusClientProvider(IWebProxy proxy) : ServiceBusClientProvider
{
public override ServiceBusClient CreateClient(string? connectionString)
{
var clientOptions = new ServiceBusClientOptions()
{
WebProxy = proxy,
TransportType = ServiceBusTransportType.AmqpWebSockets,
};
return new ServiceBusClient(connectionString, clientOptions);
}


}
6 changes: 4 additions & 2 deletions Btms.Backend/Endpoints/DiagnosticEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

using System.Net;
using Btms.Backend.Asb;
using Btms.Backend.Config;
using Btms.BlobService;
using Btms.Common.Extensions;
Expand Down Expand Up @@ -32,14 +34,14 @@ private static async Task<IResult> GetBlobDiagnosticAsync(IBlobService service)
return Results.Conflict(result);
}

private static async Task<IResult> GetAzureServiceBusDiagnosticAsync(IOptions<ServiceBusOptions> serviceBusOptions)
private static async Task<IResult> GetAzureServiceBusDiagnosticAsync(IOptions<ServiceBusOptions> serviceBusOptions, IWebProxy proxy)
{
var options = new AzureServiceBusSubscriptionHealthCheckHealthCheckOptions(serviceBusOptions.Value.Topic, serviceBusOptions.Value.Subscription)
{
ConnectionString = serviceBusOptions.Value.ConnectionString
};

var healthCheck = new AzureServiceBusSubscriptionHealthCheck(options);
var healthCheck = new AzureServiceBusSubscriptionHealthCheck(options, new BtmsServiceBusClientProvider(proxy));
var result = await healthCheck.CheckHealthAsync(new HealthCheckContext());

if (result.Status == HealthStatus.Healthy)
Expand Down
2 changes: 1 addition & 1 deletion Btms.Backend/Endpoints/SyncEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal static async Task<IResult> InitialiseEnvironment(IHost app, SyncPeriod
private static IResult DownloadNotifications([FromServices] IWebHostEnvironment env, string id)
{
var stream = File.OpenRead($"{Path.Combine(env.ContentRootPath, id)}.zip");
return Results.File(stream, "application/zip", $"{id}.zip");
return Results.File(stream, "application/zip", $"{id}.zip", enableRangeProcessing: true);
}

private static async Task<IResult> GenerateDownload([FromServices] IBtmsMediator mediator, [FromQuery] SyncPeriod period)
Expand Down
8 changes: 2 additions & 6 deletions Btms.Backend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using System.Text.Json.Serialization;
using Btms.Azure.Extensions;
using Environment = System.Environment;
using Btms.Backend.Asb;

//-------- Configure the WebApplication builder------------------//

Expand Down Expand Up @@ -219,12 +220,7 @@ static void ConfigureEndpoints(WebApplicationBuilder builder)
builder.Services.AddHealthChecks()
.AddAzureBlobStorage(sp => sp.GetService<IBlobServiceClientFactory>()!.CreateBlobServiceClient(5, 1), timeout: TimeSpan.FromSeconds(15))
.AddMongoDb(timeout: TimeSpan.FromSeconds(15))
.AddAzureServiceBusSubscription(
sp => sp.GetRequiredService<IOptions<ServiceBusOptions>>().Value.ConnectionString,
sp => sp.GetRequiredService<IOptions<ServiceBusOptions>>().Value.Topic,
sp => sp.GetRequiredService<IOptions<ServiceBusOptions>>().Value.Subscription,
configure: options => options.UsePeekMode = true,
timeout: TimeSpan.FromSeconds(15));
.AddBtmsAzureServiceBusSubscription(TimeSpan.FromSeconds(15));
}

[ExcludeFromCodeCoverage]
Expand Down

0 comments on commit 0782d4b

Please sign in to comment.