-
-
Notifications
You must be signed in to change notification settings - Fork 11
Introduced DI #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lsfera
wants to merge
28
commits into
event-driven-io:master
Choose a base branch
from
lsfera:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Introduced DI #25
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
ab10cc1
FS clean up
lsfera 977f203
suppress CS1591 (Missing XML comment for publicly visible type or me…
lsfera ad74d3c
made UseTable() optional
lsfera b59d386
enforce AOT
lsfera 71c1f14
rename IConsumes to IHandles
lsfera 86656ec
Use double quote
lsfera 438cf21
use ILKE. Replication slot name is forced to lcase
lsfera b24c01f
unused directive
lsfera b024c6f
remove static variables to enable multiple instances on same process
lsfera 8cc1fed
Added DependencyIbjection project
lsfera 380d06a
Added demo projrct for DI
lsfera acf5f4d
Enhanced logging
lsfera e918dfa
bumped version to 0.1.1
lsfera 2055fed
allow tableDescriptor access
lsfera eadba73
renamed vars
lsfera c097047
expose NpgsqlDataSource along with connection string - close #16
lsfera ef9ac95
rename extension method
lsfera 345cad6
move to files
lsfera 645ec1a
simplified di registration
lsfera ca28054
collapse project
lsfera f4cfe0c
rename folder
lsfera f9bf48f
mark as implicit usage
lsfera 7fff1b4
switch to prepared statement
lsfera f0c4160
dispose resources
lsfera 8db4187
explicit defaults
lsfera c446721
add PublisherOptions
lsfera e71999b
Expose shortcut for table validation when bootstrapping publisher
lsfera 2351af1
explain different available to publisher for validating table
lsfera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
54 changes: 54 additions & 0 deletions
54
src/Blumchen.DependencyInjection/Blumchen.DependencyInjection.csproj
This file contains hidden or 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,54 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<VersionPrefix>0.1.1</VersionPrefix> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<GenerateAssemblyTitleAttribute>true</GenerateAssemblyTitleAttribute> | ||
<GenerateAssemblyDescriptionAttribute>true</GenerateAssemblyDescriptionAttribute> | ||
<GenerateAssemblyProductAttribute>true</GenerateAssemblyProductAttribute> | ||
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute> | ||
<GenerateAssemblyVersionAttribute>true</GenerateAssemblyVersionAttribute> | ||
<GenerateAssemblyFileVersionAttribute>true</GenerateAssemblyFileVersionAttribute> | ||
<GenerateAssemblyInformationalVersionAttribute>true</GenerateAssemblyInformationalVersionAttribute> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<LangVersion>12.0</LangVersion> | ||
<Authors>Oskar Dudycz</Authors> | ||
<!-- <PackageIconUrl>https://github.com/event-driven-io/Blumchen/content/images/emblem.png</PackageIconUrl>--> | ||
<PackageProjectUrl>https://github.com/event-driven-io/Blumchen</PackageProjectUrl> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<RepositoryUrl>https://github.com/event-driven-io/Blumchen.git</RepositoryUrl> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<Product>Blumchen</Product> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
<RootNamespace>Blumchen</RootNamespace> | ||
<PublishAot>true</PublishAot> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<NoWarn>1591</NoWarn> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> | ||
<NoWarn>1591</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<ExcludeAssets>none</ExcludeAssets> | ||
<IncludeAssets>all</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" /> | ||
<PackageReference Include="Polly" Version="8.4.1" /> | ||
oskardudycz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Blumchen\Blumchen.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains hidden or 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 hidden or 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
23 changes: 23 additions & 0 deletions
23
src/Blumchen/DependencyInjection/ServiceCollectionExtensions.cs
This file contains hidden or 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 @@ | ||
using Blumchen.Subscriptions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
#pragma warning disable IL2091 | ||
|
||
namespace Blumchen.DependencyInjection; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
|
||
public static IServiceCollection AddBlumchen<T>( | ||
this IServiceCollection service, | ||
Func<IServiceProvider, IWorkerOptionsBuilder, IWorkerOptionsBuilder> workerOptions) | ||
where T : class, IHandler => | ||
service | ||
.AddKeyedSingleton(typeof(T), (provider, _) => workerOptions(provider, new WorkerOptionsBuilder()).Build()) | ||
.AddHostedService(provider => | ||
new Worker<T>(workerOptions(provider, new WorkerOptionsBuilder()).Build(), | ||
ServiceProviderServiceExtensions.GetRequiredService<ILogger<Worker<T>>>(provider))); | ||
|
||
|
||
} |
This file contains hidden or 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,41 @@ | ||
using System.Collections.Concurrent; | ||
using Blumchen.Subscriptions; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Blumchen.DependencyInjection; | ||
|
||
public class Worker<T>( | ||
WorkerOptions options, | ||
ILogger<Worker<T>> logger): BackgroundService where T : class, IHandler | ||
{ | ||
private string WorkerName { get; } = $"{nameof(Worker<T>)}<{typeof(T).Name}>"; | ||
private static readonly ConcurrentDictionary<string, Action<ILogger, string, object[]>> LoggingActions = new(StringComparer.OrdinalIgnoreCase); | ||
private static void Notify(ILogger logger, LogLevel level, string template, params object[] parameters) | ||
{ | ||
static Action<ILogger, string, object[]> LoggerAction(LogLevel ll, bool enabled) => | ||
(ll, enabled) switch | ||
{ | ||
(LogLevel.Information, true) => (logger, template, parameters) => logger.LogInformation(template, parameters), | ||
(LogLevel.Debug, true) => (logger, template, parameters) => logger.LogDebug(template, parameters), | ||
(_, _) => (_, _, _) => { } | ||
}; | ||
LoggingActions.GetOrAdd(template,_ => LoggerAction(level, logger.IsEnabled(level)))(logger, template, parameters); | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
await options.ResiliencePipeline.ExecuteAsync(async token => | ||
{ | ||
await using var subscription = new Subscription(); | ||
await using var cursor = subscription.Subscribe(options.SubscriptionOptions, ct: token) | ||
.GetAsyncEnumerator(token); | ||
Notify(logger, LogLevel.Information,"{WorkerName} started", WorkerName); | ||
while (await cursor.MoveNextAsync().ConfigureAwait(false) && !token.IsCancellationRequested) | ||
Notify(logger, LogLevel.Debug, "{cursor.Current} processed", cursor.Current); | ||
|
||
}, stoppingToken).ConfigureAwait(false); | ||
Notify(logger, LogLevel.Information, "{WorkerName} stopped", WorkerName); | ||
} | ||
|
||
} |
This file contains hidden or 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,37 @@ | ||
using Blumchen.Subscriptions; | ||
using Polly; | ||
|
||
namespace Blumchen.DependencyInjection; | ||
|
||
public record WorkerOptions(ResiliencePipeline ResiliencePipeline, ISubscriptionOptions SubscriptionOptions); | ||
|
||
public interface IWorkerOptionsBuilder | ||
{ | ||
IWorkerOptionsBuilder ResiliencyPipeline(ResiliencePipeline resiliencePipeline); | ||
IWorkerOptionsBuilder Subscription(Func<SubscriptionOptionsBuilder, SubscriptionOptionsBuilder>? builder); | ||
WorkerOptions Build(); | ||
} | ||
|
||
internal sealed class WorkerOptionsBuilder: IWorkerOptionsBuilder | ||
{ | ||
private ResiliencePipeline? _resiliencePipeline = default; | ||
private Func<SubscriptionOptionsBuilder, SubscriptionOptionsBuilder>? _builder; | ||
|
||
public IWorkerOptionsBuilder ResiliencyPipeline(ResiliencePipeline resiliencePipeline) | ||
{ | ||
_resiliencePipeline = resiliencePipeline; | ||
return this; | ||
}public IWorkerOptionsBuilder Subscription(Func<SubscriptionOptionsBuilder, SubscriptionOptionsBuilder>? builder) | ||
{ | ||
_builder = builder; | ||
return this; | ||
} | ||
|
||
public WorkerOptions Build() | ||
{ | ||
ArgumentNullException.ThrowIfNull(_resiliencePipeline); | ||
ArgumentNullException.ThrowIfNull(_builder); | ||
return new(_resiliencePipeline, _builder(new SubscriptionOptionsBuilder()).Build()); | ||
} | ||
} | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
using Blumchen.Database; | ||
using Blumchen.Serialization; | ||
using Npgsql; | ||
|
||
namespace Blumchen.Publications; | ||
|
||
public record PublisherOptions(TableDescriptorBuilder.MessageTable TableDescriptor, IJsonTypeResolver JsonTypeResolver); | ||
|
||
public static class PublisherOptionsExtensions | ||
{ | ||
public static async Task<PublisherOptions> EnsureTable(this PublisherOptions publisherOptions, NpgsqlDataSource dataSource, CancellationToken ct) | ||
{ | ||
await dataSource.EnsureTableExists(publisherOptions.TableDescriptor, ct); | ||
return publisherOptions; | ||
} | ||
|
||
public static Task<PublisherOptions> EnsureTable(this PublisherOptions publisherOptions, | ||
string connectionString, CancellationToken ct) | ||
=> EnsureTable(publisherOptions, new NpgsqlDataSourceBuilder(connectionString).Build(), ct); | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FIX: Let's merge it into the regular project. I'm fine with having hosting.abstractions and Polly dependency in the main project. It's much more accessible when user installs just a package and have all available (of course, unless that's enforcing too much from the peer dependency side).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've no strong opinion against it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lsfera, could you change that in the PR?