44using System ;
55using System . Threading . Tasks ;
66using Autofac ;
7+ using Microsoft . Extensions . Configuration ;
78using Microsoft . Extensions . DependencyInjection ;
89using Microsoft . Extensions . Logging ;
10+ using Microsoft . Extensions . Options ;
911using NuGet . Services . ServiceBus ;
1012
1113namespace NuGet . Jobs . Validation
1214{
1315 public abstract class SubcriptionProcessorJob < T > : JsonConfigurationJob
1416 {
17+ private const string SubscriptionProcessorConfigurationSectionName = "ServiceBus" ;
18+
1519 /// <summary>
1620 /// The maximum amount of time that graceful shutdown can take before the job will
1721 /// forcefully end itself.
@@ -25,10 +29,18 @@ public override async Task Run()
2529 if ( processor == null )
2630 {
2731 throw new Exception ( $ "DI container was not set up to produce instances of ISubscriptionProcessor<{ typeof ( T ) . Name } >. " +
28- $ "Call SubcriptionProcessorJob<T>.ConfigureDefaultSubscriptionProcessor() or set it up your way.") ;
32+ $ "Call SubcriptionProcessorJob<T>.{ nameof ( ConfigureDefaultSubscriptionProcessor ) } () or set it up your way.") ;
33+ }
34+
35+ var configuration = _serviceProvider . GetService < IOptionsSnapshot < SubscriptionProcessorConfiguration > > ( ) ;
36+
37+ if ( configuration == null || configuration . Value == null )
38+ {
39+ throw new Exception ( $ "Failed to get the SubscriptionProcessorJob configuration. Call " +
40+ $ "SubcriptionProcessorJob<T>.{ nameof ( SetupDefaultSubscriptionProcessorConfiguration ) } () or set it up your way.") ;
2941 }
3042
31- processor . Start ( ) ;
43+ processor . Start ( configuration . Value . MaxConcurrentCalls ) ;
3244
3345 // Wait a day, and then shutdown this process so that it is restarted.
3446 await Task . Delay ( TimeSpan . FromDays ( 1 ) ) ;
@@ -56,5 +68,10 @@ protected static void ConfigureDefaultSubscriptionProcessor(ContainerBuilder con
5668 ( parameter , context ) => context . ResolveKeyed ( bindingKey , typeof ( IMessageHandler < T > ) ) )
5769 . As < ISubscriptionProcessor < T > > ( ) ;
5870 }
71+
72+ protected static void SetupDefaultSubscriptionProcessorConfiguration ( IServiceCollection services , IConfiguration configuration )
73+ {
74+ services . Configure < SubscriptionProcessorConfiguration > ( configuration . GetSection ( SubscriptionProcessorConfigurationSectionName ) ) ;
75+ }
5976 }
6077}
0 commit comments