-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
150 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,13 @@ | ||
using Blumchen.Subscriptions; | ||
|
||
namespace SubscriberWorker; | ||
|
||
|
||
public class Handler<T>: IHandler<T> where T : class | ||
{ | ||
private volatile int _counter; | ||
public Task Handle(T value) | ||
=> Interlocked.Increment(ref _counter) % 3 == 0 | ||
? Task.FromException(new Exception($"Error on publishing {nameof(T)}")) | ||
: Task.CompletedTask; | ||
} |
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,57 @@ | ||
using System.Text.Json.Serialization; | ||
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 DbOptions(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) | ||
.AddConsole(); | ||
}); | ||
|
||
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,27 @@ | ||
using System.Text.Json.Serialization; | ||
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>( | ||
DbOptions dbOptions, | ||
IHandler<T> handler, | ||
JsonSerializerContext jsonSerializerContext, | ||
ResiliencePipelineProvider<string> pipelineProvider, | ||
INamingPolicy namingPolicy, | ||
IErrorProcessor errorProcessor, | ||
ILoggerFactory loggerFactory | ||
): Worker<T>(dbOptions | ||
, 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="..\src\Commons\Commons.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |