-
Notifications
You must be signed in to change notification settings - Fork 0
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
42b8107
commit ea6cf0e
Showing
11 changed files
with
160 additions
and
4 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
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
39 changes: 39 additions & 0 deletions
39
src/ServiceControl.Connector.MassTransit.Host/Commands/HealthCheckCommand.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,39 @@ | ||
namespace ServiceControl.Connector.MassTransit.Host.Commands; | ||
|
||
using System.CommandLine; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
public class HealthCheckCommand : Command | ||
{ | ||
public HealthCheckCommand() : base("health-check", "Performs a validation that the connector is able to connect to the broker.") | ||
{ | ||
this.SetHandler(async context => | ||
{ | ||
context.ExitCode = await InternalHandler(context.GetCancellationToken()); | ||
}); | ||
} | ||
|
||
async Task<int> InternalHandler(CancellationToken cancellationToken) | ||
{ | ||
var builder = Host.CreateEmptyApplicationBuilder(null); | ||
builder.Configuration.AddEnvironmentVariables(); | ||
builder.UseMassTransitConnector(true); | ||
|
||
var host = builder.Build(); | ||
|
||
var queueInformationProvider = host.Services.GetRequiredService<IHealthCheckerProvider>(); | ||
var (success, errorMessage) = await queueInformationProvider.TryCheck(cancellationToken); | ||
|
||
if (!success) | ||
{ | ||
Console.WriteLine(errorMessage); | ||
return 1; | ||
} | ||
|
||
Console.WriteLine("Success"); | ||
|
||
return 0; | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
src/ServiceControl.Connector.MassTransit.RabbitMQ/RabbitMQHealthChecker.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,46 @@ | ||
using NServiceBus.Transport; | ||
using RabbitMQ.Client.Exceptions; | ||
|
||
class RabbitMQHealthChecker(RabbitMQHelper helper, Configuration configuration, TransportInfrastructureFactory transportInfrastructureFactory) : IHealthCheckerProvider | ||
{ | ||
public async Task<(bool, string)> TryCheck(CancellationToken cancellationToken) | ||
{ | ||
var result = await helper.TryCheck(cancellationToken); | ||
|
||
if (!result.Success) | ||
{ | ||
return (false, result.ErrorMessage); | ||
} | ||
|
||
var hostSettings = new HostSettings( | ||
configuration.ReturnQueue, | ||
$"Queue creator for {configuration.ReturnQueue}", | ||
new StartupDiagnosticEntries(), | ||
(_, __, ___) => | ||
{ | ||
}, | ||
false); | ||
|
||
var receiverSettings = new[]{ | ||
new ReceiveSettings( | ||
id: "Return", | ||
receiveAddress: new QueueAddress(configuration.ReturnQueue), | ||
usePublishSubscribe: false, | ||
purgeOnStartup: false, | ||
errorQueue: configuration.PoisonQueue)}; | ||
|
||
|
||
try | ||
{ | ||
var infrastructure = await transportInfrastructureFactory.CreateTransportInfrastructure(hostSettings, | ||
receiverSettings, [configuration.PoisonQueue, configuration.ServiceControlQueue], cancellationToken); | ||
await infrastructure.Shutdown(cancellationToken); | ||
} | ||
catch (BrokerUnreachableException e) | ||
{ | ||
return (false, e.Message); | ||
} | ||
|
||
return (true, string.Empty); | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
src/ServiceControl.Connector.MassTransit/IHealthCheckerProvider.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,4 @@ | ||
public interface IHealthCheckerProvider | ||
{ | ||
Task<(bool Success, string ErrorMessage)> TryCheck(CancellationToken cancellationToken); | ||
} |