-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Richard Pringle <richardpringle@gmail.com>
- Loading branch information
Showing
63 changed files
with
1,982 additions
and
106 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
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
17 changes: 17 additions & 0 deletions
17
src/Samples/Sample.CircuitBreaker.HealthCheck/Consumers/AddConsumer.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,17 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.Consumers; | ||
|
||
public class AddConsumer : IConsumer<Add> | ||
{ | ||
private readonly ILogger<AddConsumer> _logger; | ||
|
||
public AddConsumer(ILogger<AddConsumer> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task OnHandle(Add message) | ||
{ | ||
_logger.LogInformation("{A} + {B} = {C}", message.a, message.b, message.a + message.b); | ||
return Task.CompletedTask; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Samples/Sample.CircuitBreaker.HealthCheck/Consumers/SubtractConsumer.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,17 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.Consumers; | ||
|
||
public class SubtractConsumer : IConsumer<Subtract> | ||
{ | ||
private readonly ILogger<SubtractConsumer> _logger; | ||
|
||
public SubtractConsumer(ILogger<SubtractConsumer> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task OnHandle(Subtract message) | ||
{ | ||
_logger.LogInformation("{A} - {B} = {C}", message.a, message.b, message.a - message.b); | ||
return Task.CompletedTask; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Samples/Sample.CircuitBreaker.HealthCheck/GlobalUsings.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,17 @@ | ||
global using System.Net.Mime; | ||
global using System.Reflection; | ||
|
||
global using Microsoft.Extensions.Configuration; | ||
global using Microsoft.Extensions.DependencyInjection; | ||
global using Microsoft.Extensions.Hosting; | ||
global using Microsoft.Extensions.Logging; | ||
|
||
global using Sample.CircuitBreaker.HealthCheck.Consumers; | ||
global using Sample.CircuitBreaker.HealthCheck.Models; | ||
|
||
global using SecretStore; | ||
|
||
global using SlimMessageBus; | ||
global using SlimMessageBus.Host; | ||
global using SlimMessageBus.Host.RabbitMQ; | ||
global using SlimMessageBus.Host.Serialization.SystemTextJson; |
11 changes: 11 additions & 0 deletions
11
src/Samples/Sample.CircuitBreaker.HealthCheck/HealthChecks/AddRandomHealthCheck.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,11 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.HealthChecks; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
public class AddRandomHealthCheck : RandomHealthCheck | ||
{ | ||
public AddRandomHealthCheck(ILogger<AddRandomHealthCheck> logger) | ||
: base(logger) | ||
{ | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Samples/Sample.CircuitBreaker.HealthCheck/HealthChecks/RandomHealthCheck.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,20 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.HealthChecks; | ||
|
||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
public abstract class RandomHealthCheck : IHealthCheck | ||
{ | ||
private readonly ILogger _logger; | ||
|
||
protected RandomHealthCheck(ILogger logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
var value = (HealthStatus)Random.Shared.Next(3); | ||
_logger.LogInformation("{HealthCheck} evaluated as {HealthStatus}", this.GetType(), value); | ||
return Task.FromResult(new HealthCheckResult(value, value.ToString())); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Samples/Sample.CircuitBreaker.HealthCheck/HealthChecks/SubtractRandomHealthCheck.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,11 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.HealthChecks; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
public class SubtractRandomHealthCheck : RandomHealthCheck | ||
{ | ||
public SubtractRandomHealthCheck(ILogger<SubtractRandomHealthCheck> logger) | ||
: base(logger) | ||
{ | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Samples/Sample.CircuitBreaker.HealthCheck/IntermittentMessagePublisher.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,28 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck; | ||
public class IntermittentMessagePublisher : BackgroundService | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly IMessageBus _messageBus; | ||
|
||
public IntermittentMessagePublisher(ILogger<IntermittentMessagePublisher> logger, IMessageBus messageBus) | ||
{ | ||
_logger = logger; | ||
_messageBus = messageBus; | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
var a = Random.Shared.Next(10); | ||
var b = Random.Shared.Next(10); | ||
|
||
//_logger.LogInformation("Emitting {A} +- {B} = ?", a, b); | ||
|
||
await Task.WhenAll( | ||
_messageBus.Publish(new Add(a, b)), | ||
_messageBus.Publish(new Subtract(a, b)), | ||
Task.Delay(1000, stoppingToken)); | ||
} | ||
} | ||
} |
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,3 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.Models; | ||
|
||
public record Add(int a, int b); |
3 changes: 3 additions & 0 deletions
3
src/Samples/Sample.CircuitBreaker.HealthCheck/Models/Subtract.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,3 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck.Models; | ||
|
||
public record Subtract(int a, int b); |
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,91 @@ | ||
namespace Sample.CircuitBreaker.HealthCheck; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
using Sample.CircuitBreaker.HealthCheck.HealthChecks; | ||
|
||
using SlimMessageBus.Host.CircuitBreaker.HealthCheck.Config; | ||
|
||
public static class Program | ||
{ | ||
private static async Task Main(string[] args) | ||
{ | ||
// Local file with secrets | ||
Secrets.Load(@"..\..\..\..\..\secrets.txt"); | ||
|
||
await Host.CreateDefaultBuilder(args) | ||
.ConfigureServices((builder, services) => | ||
{ | ||
const string AddTag = "add"; | ||
const string SubtractTag = "subtract"; | ||
|
||
services.AddSlimMessageBus(mbb => | ||
{ | ||
var ticks = DateTimeOffset.UtcNow.Ticks; | ||
var addTopic = $"Sample-CircuitBreaker-HealthCheck-add-{ticks}"; | ||
var subtractTopic = $"Sample-CircuitBreaker-HealthCheck-subtract-{ticks}"; | ||
|
||
mbb | ||
.WithProviderRabbitMQ( | ||
cfg => | ||
{ | ||
cfg.ConnectionString = Secrets.Service.PopulateSecrets(builder.Configuration.GetValue<string>("RabbitMQ:ConnectionString")); | ||
cfg.ConnectionFactory.ClientProvidedName = $"Sample_CircuitBreaker_HealthCheck_{Environment.MachineName}"; | ||
|
||
cfg.UseMessagePropertiesModifier((m, p) => p.ContentType = MediaTypeNames.Application.Json); | ||
cfg.UseExchangeDefaults(durable: false); | ||
cfg.UseQueueDefaults(durable: false); | ||
}); | ||
mbb | ||
.Produce<Add>(x => x | ||
.Exchange(addTopic, exchangeType: ExchangeType.Fanout, autoDelete: false) | ||
.RoutingKeyProvider((m, p) => Guid.NewGuid().ToString())) | ||
.Consume<Add>( | ||
cfg => | ||
{ | ||
cfg | ||
.Queue(nameof(Add), autoDelete: false) | ||
.Path(nameof(Add)) | ||
.ExchangeBinding(addTopic) | ||
.WithConsumer<AddConsumer>() | ||
.PauseOnDegradedHealthCheck(AddTag); | ||
}); | ||
|
||
mbb | ||
.Produce<Subtract>(x => x | ||
.Exchange(subtractTopic, exchangeType: ExchangeType.Fanout, autoDelete: false) | ||
.RoutingKeyProvider((m, p) => Guid.NewGuid().ToString())) | ||
.Consume<Subtract>( | ||
cfg => | ||
{ | ||
cfg | ||
.Queue(nameof(Subtract), autoDelete: false) | ||
.Path(nameof(Subtract)) | ||
.ExchangeBinding(subtractTopic) | ||
.WithConsumer<SubtractConsumer>() | ||
.PauseOnUnhealthyCheck(SubtractTag); | ||
}); | ||
|
||
mbb.AddServicesFromAssembly(Assembly.GetExecutingAssembly()); | ||
mbb.AddJsonSerializer(); | ||
}); | ||
|
||
services.AddHostedService<IntermittentMessagePublisher>(); | ||
services.AddSingleton<AddRandomHealthCheck>(); | ||
services.AddSingleton<SubtractRandomHealthCheck>(); | ||
|
||
services.Configure<HealthCheckPublisherOptions>(cfg => | ||
{ | ||
// aggressive to toggle health status often (sample only) | ||
cfg.Delay = TimeSpan.FromSeconds(3); | ||
cfg.Period = TimeSpan.FromSeconds(5); | ||
}); | ||
|
||
services | ||
.AddHealthChecks() | ||
.AddCheck<AddRandomHealthCheck>("Add", tags: [AddTag]) | ||
.AddCheck<SubtractRandomHealthCheck>("Subtract", tags: [SubtractTag]); | ||
}) | ||
.Build() | ||
.RunAsync(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Samples/Sample.CircuitBreaker.HealthCheck/Sample.CircuitBreaker.HealthCheck.csproj
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,33 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.7" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\SlimMessageBus.Host.CircuitBreaker.HealthCheck\SlimMessageBus.Host.CircuitBreaker.HealthCheck.csproj" /> | ||
<ProjectReference Include="..\..\SlimMessageBus.Host.RabbitMQ\SlimMessageBus.Host.RabbitMQ.csproj" /> | ||
<ProjectReference Include="..\..\SlimMessageBus.Host\SlimMessageBus.Host.csproj" /> | ||
<ProjectReference Include="..\..\SlimMessageBus.Host.Serialization.SystemTextJson\SlimMessageBus.Host.Serialization.SystemTextJson.csproj" /> | ||
<ProjectReference Include="..\..\SlimMessageBus\SlimMessageBus.csproj" /> | ||
<ProjectReference Include="..\..\Tools\SecretStore\SecretStore.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="appsettings.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile> | ||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
19 changes: 19 additions & 0 deletions
19
src/Samples/Sample.CircuitBreaker.HealthCheck/appsettings.json
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,19 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
}, | ||
"Console": { | ||
"FormatterName": "simple", | ||
"FormatterOptions": { | ||
"SingleLine": true, | ||
"TimestampFormat": "HH:mm:ss.fff " | ||
} | ||
} | ||
}, | ||
"RabbitMQ": { | ||
"ConnectionString": "{{rabbitmq_connectionstring}}" | ||
} | ||
} |
Oops, something went wrong.