-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add health check to code and docker (#7)
Co-authored-by: Piotr Rojek <[email protected]>
- Loading branch information
1 parent
7c17f94
commit 59d3173
Showing
9 changed files
with
128 additions
and
45 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
9 changes: 9 additions & 0 deletions
9
src/ServiceBusEmulator.RabbitMq/Links/IRabbitMqChannelFactory.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,9 @@ | ||
using RabbitMQ.Client; | ||
|
||
namespace ServiceBusEmulator.RabbitMq.Links | ||
{ | ||
public interface IRabbitMqChannelFactory | ||
{ | ||
IModel CreateChannel(); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/ServiceBusEmulator.RabbitMq/Links/RabbitMqChannelFactory.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,68 @@ | ||
using Microsoft.Extensions.Options; | ||
using RabbitMQ.Client; | ||
using ServiceBusEmulator.RabbitMq.Options; | ||
|
||
namespace ServiceBusEmulator.RabbitMq.Links | ||
{ | ||
public class RabbitMqChannelFactory : IRabbitMqChannelFactory, IDisposable | ||
{ | ||
private readonly RabbitMqBackendOptions _options; | ||
|
||
private IConnection? _connection; | ||
private ConnectionFactory _connectionFactory; | ||
|
||
private bool _disposed; | ||
|
||
public RabbitMqChannelFactory(IOptions<RabbitMqBackendOptions> options) | ||
{ | ||
_options = options.Value; | ||
|
||
_connectionFactory = new() | ||
{ | ||
Password = _options.Password, | ||
UserName = _options.Username, | ||
HostName = _options.Host, | ||
Port = _options.Port | ||
}; | ||
} | ||
|
||
protected IConnection Connection | ||
{ | ||
get | ||
{ | ||
if (_disposed) | ||
{ | ||
throw new ObjectDisposedException(nameof(RabbitMqChannelFactory)); | ||
} | ||
|
||
if (!(_connection?.IsOpen ?? false)) | ||
{ | ||
lock (_connectionFactory) | ||
{ | ||
if (!(_connection?.IsOpen ?? false)) | ||
{ | ||
_connection = _connectionFactory.CreateConnection(); | ||
} | ||
} | ||
} | ||
|
||
return _connection; | ||
} | ||
} | ||
|
||
public IModel CreateChannel() | ||
{ | ||
return Connection.CreateModel(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_disposed && _connection != null) | ||
{ | ||
_connection.Dispose(); | ||
_connection = null; | ||
} | ||
_disposed = true; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using ServiceBusEmulator.RabbitMq.Links; | ||
|
||
namespace ServiceBusEmulator.RabbitMq | ||
{ | ||
public class RabbitMqHealthCheck : IHealthCheck | ||
{ | ||
private readonly IRabbitMqChannelFactory _channelFactory; | ||
|
||
public RabbitMqHealthCheck(IRabbitMqChannelFactory channelFactory) | ||
{ | ||
_channelFactory = channelFactory; | ||
} | ||
|
||
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using var model = _channelFactory.CreateChannel(); | ||
return Task.FromResult(HealthCheckResult.Healthy()); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return Task.FromResult(new HealthCheckResult(context.Registration.FailureStatus, exception: ex)); | ||
} | ||
} | ||
} | ||
} |
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