-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding healthcheck command to docker #173
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} |
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)}; | ||||
|
||||
|
||||
Comment on lines
+31
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
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); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so if it fails for any reason other than brokerunreachable it will still be marked as healthy? This is the description of the healthchecker... but seems to me like it could cause some confusion There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
no, it will be an unhandled exception and therefore it will be exit code 1 |
||||
} | ||||
} |
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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without looking at the signature... should this be a different description?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this matters