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
16 changed files
with
186 additions
and
121 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
21 changes: 16 additions & 5 deletions
21
src/Blumchen.DependencyInjection/Workers/ServiceCollectionExtensions.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 |
---|---|---|
@@ -1,13 +1,24 @@ | ||
using Blumchen.Subscriptions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Polly; | ||
|
||
#pragma warning disable IL2091 | ||
|
||
namespace Blumchen.Workers; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddBlumchen<T,TU>(this IServiceCollection service, T? instance = default) | ||
where T : Worker<TU> where TU : class => | ||
instance is null | ||
? service.AddHostedService<T>() | ||
: service.AddHostedService(_=>instance); | ||
|
||
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(), | ||
provider.GetRequiredService<ILogger<Worker<T>>>())); | ||
|
||
|
||
} |
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
37 changes: 37 additions & 0 deletions
37
src/Blumchen.DependencyInjection/Workers/WorkerOptionsBuilder.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,37 @@ | ||
using Blumchen.Subscriptions; | ||
using Polly; | ||
|
||
namespace Blumchen.Workers; | ||
|
||
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 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
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
Oops, something went wrong.