forked from event-driven-io/Blumchen
-
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
Showing
7 changed files
with
165 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Text.Json.Serialization; | ||
using Blumchen.Serialization; | ||
|
||
namespace SubscriberWorker | ||
{ | ||
[MessageUrn("user-created:v1")] | ||
public record UserCreatedContract( | ||
Guid Id, | ||
string Name | ||
); | ||
|
||
[MessageUrn("user-deleted:v1")] | ||
public record UserDeletedContract( | ||
Guid Id, | ||
string Name | ||
); | ||
|
||
[JsonSourceGenerationOptions(WriteIndented = true)] | ||
[JsonSerializable(typeof(UserCreatedContract))] | ||
[JsonSerializable(typeof(UserDeletedContract))] | ||
internal partial class SourceGenerationContext: JsonSerializerContext; | ||
} |
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,25 @@ | ||
using Blumchen.Subscriptions; | ||
using Microsoft.Extensions.Logging; | ||
#pragma warning disable CS9113 // Parameter is unread. | ||
|
||
namespace SubscriberWorker; | ||
|
||
|
||
public class Handler<T>(ILoggerFactory loggerFactory): IHandler<T> where T : class | ||
{ | ||
private readonly ILogger _logger = loggerFactory.CreateLogger<Handler<T>>(); | ||
private Task ReportSuccess(int count) | ||
{ | ||
if(_logger.IsEnabled(LogLevel.Debug)) | ||
_logger.LogDebug($"Read #{count} messages {typeof(T).FullName}"); | ||
return Task.CompletedTask; | ||
} | ||
|
||
private int _counter; | ||
private int _completed; | ||
public Task Handle(T value) | ||
=> Interlocked.Increment(ref _counter) % 10 == 0 | ||
//Simulating some exception on out of process dependencies | ||
? Task.FromException(new Exception($"Error on publishing {nameof(T)}")) | ||
: ReportSuccess(Interlocked.Increment(ref _completed)); | ||
} |
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,59 @@ | ||
using System.Text.Json.Serialization; | ||
using Blumchen.Configuration; | ||
using Blumchen.Serialization; | ||
using Blumchen.Subscriptions; | ||
using Blumchen.Workers; | ||
using Commons; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Polly.Retry; | ||
using Polly; | ||
using SubscriberWorker; | ||
|
||
|
||
#pragma warning disable CS8601 // Possible null reference assignment. | ||
Console.Title = typeof(Program).Assembly.GetName().Name; | ||
#pragma warning restore CS8601 // Possible null reference assignment. | ||
|
||
|
||
|
||
AppDomain.CurrentDomain.UnhandledException += (_, e) => Console.Out.WriteLine(e.ExceptionObject.ToString()); | ||
TaskScheduler.UnobservedTaskException += (_, e) => Console.Out.WriteLine(e.Exception.ToString()); | ||
|
||
var cancellationTokenSource = new CancellationTokenSource(); | ||
var builder = Host.CreateApplicationBuilder(args); | ||
|
||
builder.Services | ||
.AddBlumchen<SubscriberWorker<UserCreatedContract>, UserCreatedContract>() | ||
.AddSingleton<IHandler<UserCreatedContract>, Handler<UserCreatedContract>>() | ||
.AddBlumchen<SubscriberWorker<UserDeletedContract>, UserDeletedContract>() | ||
.AddSingleton<IHandler<UserDeletedContract>, Handler<UserDeletedContract>>() | ||
|
||
.AddSingleton<INamingPolicy, AttributeNamingPolicy>() | ||
.AddSingleton<IErrorProcessor, ConsoleOutErrorProcessor>() | ||
.AddSingleton<JsonSerializerContext, SourceGenerationContext>() | ||
.AddSingleton(new DatabaseOptions(Settings.ConnectionString)) | ||
.AddResiliencePipeline("default",(pipelineBuilder,context) => | ||
pipelineBuilder | ||
.AddRetry(new RetryStrategyOptions | ||
{ | ||
BackoffType = DelayBackoffType.Constant, | ||
Delay = TimeSpan.FromSeconds(5), | ||
MaxRetryAttempts = int.MaxValue | ||
}).Build()) | ||
.AddLogging(loggingBuilder => | ||
{ | ||
loggingBuilder | ||
.AddFilter("Microsoft", LogLevel.Warning) | ||
.AddFilter("System", LogLevel.Warning) | ||
.AddFilter("Npgsql", LogLevel.Information) | ||
.AddFilter("Blumchen", LogLevel.Debug) | ||
.AddFilter("SubscriberWorker", LogLevel.Debug) | ||
.AddSimpleConsole(); | ||
}); | ||
|
||
await builder | ||
.Build() | ||
.RunAsync(cancellationTokenSource.Token) | ||
.ConfigureAwait(false); |
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 System.Text.Json.Serialization; | ||
using Blumchen.Configuration; | ||
using Blumchen.Serialization; | ||
using Blumchen.Subscriptions; | ||
using Blumchen.Subscriptions.Management; | ||
using Blumchen.Workers; | ||
using Microsoft.Extensions.Logging; | ||
using Polly.Registry; | ||
// ReSharper disable ClassNeverInstantiated.Global | ||
|
||
namespace SubscriberWorker; | ||
public class SubscriberWorker<T>( | ||
DatabaseOptions databaseOptions, | ||
IHandler<T> handler, | ||
JsonSerializerContext jsonSerializerContext, | ||
ResiliencePipelineProvider<string> pipelineProvider, | ||
INamingPolicy namingPolicy, | ||
IErrorProcessor errorProcessor, | ||
ILoggerFactory loggerFactory | ||
): Worker<T>(databaseOptions | ||
, handler | ||
, jsonSerializerContext | ||
, errorProcessor | ||
, pipelineProvider.GetPipeline("default") | ||
, namingPolicy | ||
, new PublicationManagement.PublicationSetupOptions($"{typeof(T).Name}_pub") | ||
, new ReplicationSlotManagement.ReplicationSlotSetupOptions($"{typeof(T).Name}_slot") | ||
, loggerFactory) where T : class; |
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,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<PublishAot>true</PublishAot> | ||
<InvariantGlobalization>true</InvariantGlobalization> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
<PackageReference Include="Polly.Extensions" Version="8.4.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Blumchen.DependencyInjection\Blumchen.DependencyInjection.csproj" /> | ||
<ProjectReference Include="..\Commons\Commons.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |