-
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.
Merge pull request #15 from lenndewolten/feature/refactor-DI-and-opti…
…ons-setup Feature/refactor di and options setup
- Loading branch information
Showing
47 changed files
with
1,284 additions
and
1,206 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
lib/Dequeueable.AzureQueueStorage/Configurations/HostBuilder.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,100 @@ | ||
using Dequeueable.AzureQueueStorage.Factories; | ||
using Dequeueable.AzureQueueStorage.Services.Hosts; | ||
using Dequeueable.AzureQueueStorage.Services.Queues; | ||
using Dequeueable.AzureQueueStorage.Services.Singleton; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Dequeueable.AzureQueueStorage.Configurations | ||
{ | ||
internal class HostBuilder : IDequeueableHostBuilder | ||
{ | ||
private readonly IServiceCollection _services; | ||
|
||
public HostBuilder(IServiceCollection services) | ||
{ | ||
_services = services; | ||
} | ||
|
||
public IDequeueableHostBuilder RunAsJob(Action<HostOptions>? options = null) | ||
{ | ||
_services.AddOptions<HostOptions>().BindConfiguration(HostOptions.Dequeueable) | ||
.ValidateDataAnnotations() | ||
.ValidateOnStart(); | ||
|
||
if (options is not null) | ||
{ | ||
_services.Configure(options); | ||
} | ||
|
||
_services.AddHostedService<JobHost>(); | ||
_services.AddSingleton<IHostExecutor, JobExecutor>(); | ||
|
||
_services.TryAddSingleton<IHostOptions>(provider => | ||
{ | ||
var opt = provider.GetRequiredService<IOptions<HostOptions>>(); | ||
return opt.Value; | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
public IDequeueableHostBuilder RunAsListener(Action<ListenerHostOptions>? options = null) | ||
{ | ||
_services.AddOptions<ListenerHostOptions>().BindConfiguration(HostOptions.Dequeueable) | ||
.Validate(ListenerHostOptions.ValidatePollingInterval, $"The '{nameof(ListenerHostOptions.MinimumPollingIntervalInMilliseconds)}' must not be greater than the '{nameof(ListenerHostOptions.MaximumPollingIntervalInMilliseconds)}'.") | ||
.Validate(ListenerHostOptions.ValidateNewBatchThreshold, $"The '{nameof(ListenerHostOptions.NewBatchThreshold)}' must not be greater than the '{nameof(ListenerHostOptions.BatchSize)}'.") | ||
.ValidateDataAnnotations() | ||
.ValidateOnStart(); | ||
|
||
if (options is not null) | ||
{ | ||
_services.Configure(options); | ||
} | ||
|
||
_services.AddHostedService<QueueListenerHost>(); | ||
_services.AddSingleton<IHostExecutor, QueueListenerExecutor>(); | ||
|
||
_services.TryAddSingleton<IHostOptions>(provider => | ||
{ | ||
var opt = provider.GetRequiredService<IOptions<ListenerHostOptions>>(); | ||
return opt.Value; | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
public IDequeueableHostBuilder AsSingleton(Action<SingletonHostOptions>? options = null) | ||
{ | ||
_services.AddOptions<SingletonHostOptions>().BindConfiguration(SingletonHostOptions.Name) | ||
.Validate(SingletonHostOptions.ValidatePollingInterval, $"The '{nameof(SingletonHostOptions.MinimumPollingIntervalInSeconds)}' must not be greater than the '{nameof(SingletonHostOptions.MaximumPollingIntervalInSeconds)}'.") | ||
.ValidateDataAnnotations() | ||
.ValidateOnStart(); | ||
|
||
if (options is not null) | ||
{ | ||
_services.Configure(options); | ||
} | ||
|
||
_services.AddTransient<IDistributedLockManager, DistributedLockManager>(); | ||
_services.AddTransient<IDistributedLockManagerFactory, DistributedLockManagerFactory>(); | ||
_services.AddTransient<IBlobClientProvider, BlobClientProvider>(); | ||
_services.AddTransient<ISingletonLockManager, SingletonLockManager>(); | ||
_services.AddTransient<IBlobClientFactory, BlobClientFactory>(); | ||
_services.AddTransient<QueueMessageExecutor>(); | ||
_services.AddTransient<IQueueMessageExecutor>(provider => | ||
{ | ||
var singletonManager = provider.GetRequiredService<ISingletonLockManager>(); | ||
var executor = provider.GetRequiredService<QueueMessageExecutor>(); | ||
var attribute = provider.GetRequiredService<IOptions<SingletonHostOptions>>(); | ||
return new SingletonQueueMessageExecutor(singletonManager, executor, attribute); | ||
}); | ||
|
||
_services.PostConfigure<ListenerHostOptions>(storageAccountOptions => storageAccountOptions.NewBatchThreshold = 0); | ||
|
||
return this; | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
lib/Dequeueable.AzureQueueStorage/Configurations/IDequeueableHostBuilder.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 Dequeueable.AzureQueueStorage.Configurations | ||
{ | ||
/// <summary> | ||
/// Interface to builds and setup the dequeueable host | ||
/// </summary> | ||
public interface IDequeueableHostBuilder | ||
{ | ||
/// <summary> | ||
/// This makes sure only a single instance of the function is executed at any given time (even across host instances). | ||
/// A blob lease is used behind the scenes to implement the lock./> | ||
/// </summary> | ||
/// <param name="options">Action to configure the <see cref="SingletonHostOptions"/></param> | ||
/// <returns><see cref="IDequeueableHostBuilder"/></returns> | ||
IDequeueableHostBuilder AsSingleton(Action<SingletonHostOptions>? options = null); | ||
/// <summary> | ||
/// The application will run as a job, from start to finish, and will automatically shutdown when the messages are executed. | ||
/// </summary> | ||
/// <param name="options">Action to configure the <see cref="HostOptions"/></param> | ||
/// <returns><see cref="IDequeueableHostBuilder"/></returns> | ||
IDequeueableHostBuilder RunAsJob(Action<HostOptions>? options = null); | ||
/// <summary> | ||
/// The application will run as a listener, the queue will periodically be polled for new message. | ||
/// </summary> | ||
/// <param name="options">Action to configure the <see cref="ListenerHostOptions"/></param> | ||
/// <returns><see cref="IDequeueableHostBuilder"/></returns> | ||
IDequeueableHostBuilder RunAsListener(Action<ListenerHostOptions>? options = null); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
lib/Dequeueable.AzureQueueStorage/Configurations/ListenerHostOptions.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,51 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Dequeueable.AzureQueueStorage.Configurations | ||
{ | ||
/// <summary> | ||
/// Host options to configure the settings of the host and it's queue listeners | ||
/// </summary> | ||
public class ListenerHostOptions : HostOptions | ||
{ | ||
private int? _newBatchThreshold; | ||
|
||
/// <summary> | ||
/// The threshold at which a new batch of messages will be fetched. | ||
/// </summary> | ||
public int NewBatchThreshold | ||
{ | ||
get => _newBatchThreshold ?? Convert.ToInt32(Math.Ceiling(BatchSize / (double)2)); | ||
set | ||
{ | ||
_newBatchThreshold = value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The minimum polling interval to check the queue for new messages. | ||
/// </summary> | ||
[Range(1, long.MaxValue, ErrorMessage = "Value for {0} must not be negative.")] | ||
public long MinimumPollingIntervalInMilliseconds { get; set; } = 5; | ||
|
||
/// <summary> | ||
/// The maximum polling interval to check the queue for new messages. | ||
/// </summary> | ||
[Range(1, long.MaxValue, ErrorMessage = "Value for {0} must not be negative or zero.")] | ||
public long MaximumPollingIntervalInMilliseconds { get; set; } = 10000; | ||
|
||
/// <summary> | ||
/// The delta used to randomize the polling interval. | ||
/// </summary> | ||
public TimeSpan? DeltaBackOff { get; set; } | ||
|
||
internal static bool ValidatePollingInterval(ListenerHostOptions options) | ||
{ | ||
return options.MinimumPollingIntervalInMilliseconds < options.MaximumPollingIntervalInMilliseconds; | ||
} | ||
|
||
internal static bool ValidateNewBatchThreshold(ListenerHostOptions options) | ||
{ | ||
return options._newBatchThreshold is null || options.NewBatchThreshold <= options.BatchSize; | ||
} | ||
} | ||
} |
68 changes: 0 additions & 68 deletions
68
lib/Dequeueable.AzureQueueStorage/Configurations/ListenerOptions.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.