diff --git a/src/Messaging/NBB.Messaging.Abstractions/NBB.Messaging.Abstractions.csproj b/src/Messaging/NBB.Messaging.Abstractions/NBB.Messaging.Abstractions.csproj index 7f84b030..87b6612d 100644 --- a/src/Messaging/NBB.Messaging.Abstractions/NBB.Messaging.Abstractions.csproj +++ b/src/Messaging/NBB.Messaging.Abstractions/NBB.Messaging.Abstractions.csproj @@ -1,33 +1,33 @@  - - SAK - SAK - SAK - SAK - + + SAK + SAK + SAK + SAK + - - net7.0 - A few lightweight messaging abstractions - + + net7.0 + A few lightweight messaging abstractions + - - - + + + - - - - - - + + + + + + - - - - - - + + + + + + diff --git a/src/Messaging/NBB.Messaging.Host/IMessagingHost.cs b/src/Messaging/NBB.Messaging.Host/IMessagingHost.cs index 810a5cb2..649f1ce7 100644 --- a/src/Messaging/NBB.Messaging.Host/IMessagingHost.cs +++ b/src/Messaging/NBB.Messaging.Host/IMessagingHost.cs @@ -12,6 +12,7 @@ public interface IMessagingHost public Task StartAsync(CancellationToken cancellationToken = default); public Task StopAsync(CancellationToken cancellationToken = default); public void ScheduleRestart(TimeSpan delay = default); + public bool IsRunning(); } diff --git a/src/Messaging/NBB.Messaging.Host/Internal/MessagingHost.cs b/src/Messaging/NBB.Messaging.Host/Internal/MessagingHost.cs index 0f325597..18194fc6 100644 --- a/src/Messaging/NBB.Messaging.Host/Internal/MessagingHost.cs +++ b/src/Messaging/NBB.Messaging.Host/Internal/MessagingHost.cs @@ -218,6 +218,8 @@ public async Task StopAsync(CancellationToken cancellationToken = default) } + public bool IsRunning() => _isStarted == 1 && _isStarting == 0 && _isStopping == 0 && _isScheduledRestart == 0; + private void ExecuteCancellation(CancellationTokenSource cancel) { // Noop if this is already cancelled @@ -234,5 +236,6 @@ public void Dispose() { _transportMonitor.OnError -= OnTransportError; } + } } diff --git a/src/Messaging/NBB.Messaging.Host/MessagingHostHealthCheck.cs b/src/Messaging/NBB.Messaging.Host/MessagingHostHealthCheck.cs new file mode 100644 index 00000000..3ac98056 --- /dev/null +++ b/src/Messaging/NBB.Messaging.Host/MessagingHostHealthCheck.cs @@ -0,0 +1,29 @@ +// Copyright (c) TotalSoft. +// This source code is licensed under the MIT license. + +using Microsoft.Extensions.Diagnostics.HealthChecks; +using System.Threading; +using System.Threading.Tasks; + +namespace NBB.Messaging.Host +{ + public class MessagingHostHealthCheck : IHealthCheck + { + private readonly IMessagingHost _messagingHost; + + public MessagingHostHealthCheck(IMessagingHost messagingHost) + { + _messagingHost = messagingHost; + } + + public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) + { + if (_messagingHost.IsRunning()) + { + return Task.FromResult(HealthCheckResult.Healthy("MessagingHost is running.")); + } + + return Task.FromResult(HealthCheckResult.Unhealthy("MessagingHost is not running.")); + } + } +} diff --git a/src/Messaging/NBB.Messaging.Host/NBB.Messaging.Host.csproj b/src/Messaging/NBB.Messaging.Host/NBB.Messaging.Host.csproj index 9ccba1f4..2e63f555 100644 --- a/src/Messaging/NBB.Messaging.Host/NBB.Messaging.Host.csproj +++ b/src/Messaging/NBB.Messaging.Host/NBB.Messaging.Host.csproj @@ -1,34 +1,35 @@  - - net7.0 - Messaging subscriber host - + + net7.0 + Messaging subscriber host + - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - + + + + + + + - - - <_Parameter1>$(AssemblyName).Tests - - + + + <_Parameter1>$(AssemblyName).Tests + +