1+ using System . Diagnostics . CodeAnalysis ;
12using HotChocolate . AspNetCore . Warmup ;
23using HotChocolate . Execution . Configuration ;
3- using HotChocolate . Execution . Internal ;
44
55// ReSharper disable once CheckNamespace
66namespace Microsoft . Extensions . DependencyInjection ;
77
88public static partial class HotChocolateAspNetCoreServiceCollectionExtensions
99{
1010 /// <summary>
11- /// Adds the current GraphQL configuration to the warmup background service .
11+ /// Adds a warmup task that will be executed on each newly created request executor .
1212 /// </summary>
1313 /// <param name="builder">
1414 /// The <see cref="IRequestExecutorBuilder"/>.
1515 /// </param>
16- /// <param name="warmup">
17- /// The warmup task that shall be executed on a new executor.
18- /// </param>
19- /// <param name="keepWarm">
20- /// Apply warmup task after eviction and keep executor in-memory.
16+ /// <param name="warmupFunc">
17+ /// The warmup delegate to execute.
2118 /// </param>
2219 /// <param name="skipIf">
23- /// Skips the warmup task if set to true .
20+ /// If <c>true</c>, the warmup task will not be registered .
2421 /// </param>
2522 /// <returns>
2623 /// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
2724 /// </returns>
2825 /// <exception cref="ArgumentNullException">
29- /// The <see cref="IRequestExecutorBuilder"/> is <c>null</c>.
26+ /// The <paramref name="builder"/> is <c>null</c>.
27+ /// </exception>
28+ /// <exception cref="ArgumentNullException">
29+ /// The <paramref name="warmupFunc"/> is <c>null</c>.
3030 /// </exception>
31- public static IRequestExecutorBuilder InitializeOnStartup (
31+ public static IRequestExecutorBuilder AddWarmupTask (
3232 this IRequestExecutorBuilder builder ,
33- Func < IRequestExecutor , CancellationToken , Task > ? warmup = null ,
34- bool keepWarm = false ,
33+ Func < IRequestExecutor , CancellationToken , Task > warmupFunc ,
3534 bool skipIf = false )
3635 {
3736 ArgumentNullException . ThrowIfNull ( builder ) ;
37+ ArgumentNullException . ThrowIfNull ( warmupFunc ) ;
3838
39- if ( ! skipIf )
40- {
41- builder . Services . AddHostedService < RequestExecutorWarmupService > ( ) ;
42- builder . Services . AddSingleton ( new WarmupSchemaTask ( builder . Name , keepWarm , warmup ) ) ;
43- }
44-
45- return builder ;
39+ return builder . AddWarmupTask ( new DelegateRequestExecutorWarmupTask ( warmupFunc ) , skipIf ) ;
4640 }
4741
4842 /// <summary>
49- /// Adds the current GraphQL configuration to the warmup background service .
43+ /// Adds a warmup task that will be executed on each newly created request executor .
5044 /// </summary>
5145 /// <param name="builder">
5246 /// The <see cref="IRequestExecutorBuilder"/>.
5347 /// </param>
54- /// <param name="options ">
55- /// The <see cref="RequestExecutorInitializationOptions"/> .
48+ /// <param name="warmupTask ">
49+ /// The warmup task to execute .
5650 /// </param>
5751 /// <param name="skipIf">
58- /// Skips the warmup task if set to true .
52+ /// If <c>true</c>, the warmup task will not be registered .
5953 /// </param>
6054 /// <returns>
6155 /// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
6256 /// </returns>
6357 /// <exception cref="ArgumentNullException">
64- /// The <see cref="IRequestExecutorBuilder "/> is <c>null</c>.
58+ /// The <paramref name="builder "/> is <c>null</c>.
6559 /// </exception>
66- public static IRequestExecutorBuilder InitializeOnStartup (
60+ /// <exception cref="ArgumentNullException">
61+ /// The <paramref name="warmupTask"/> is <c>null</c>.
62+ /// </exception>
63+ public static IRequestExecutorBuilder AddWarmupTask (
6764 this IRequestExecutorBuilder builder ,
68- RequestExecutorInitializationOptions options ,
65+ IRequestExecutorWarmupTask warmupTask ,
6966 bool skipIf = false )
7067 {
7168 ArgumentNullException . ThrowIfNull ( builder ) ;
69+ ArgumentNullException . ThrowIfNull ( warmupTask ) ;
7270
7371 if ( skipIf )
7472 {
7573 return builder ;
7674 }
7775
78- Func < IRequestExecutor , CancellationToken , Task > ? warmup ;
76+ return builder . ConfigureSchemaServices ( ( _ , sc ) => sc . AddSingleton ( warmupTask ) ) ;
77+ }
7978
80- if ( options . WriteSchemaFile . Enable )
81- {
82- var schemaFileName =
83- options . WriteSchemaFile . FileName
84- ?? System . IO . Path . Combine ( Environment . CurrentDirectory , "schema.graphqls" ) ;
79+ /// <summary>
80+ /// Adds a warmup task for the request executor.
81+ /// </summary>
82+ /// <param name="builder">
83+ /// The <see cref="IRequestExecutorBuilder"/>.
84+ /// </param>
85+ /// <param name="skipIf">
86+ /// If <c>true</c>, the warmup task will not be registered.
87+ /// </param>
88+ /// <typeparam name="T">
89+ /// The warmup task to execute.
90+ /// </typeparam>
91+ /// <returns>
92+ /// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
93+ /// </returns>
94+ /// <exception cref="ArgumentNullException">
95+ /// The <paramref name="builder"/> is <c>null</c>.
96+ /// </exception>
97+ public static IRequestExecutorBuilder AddWarmupTask < [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicConstructors ) ] T > (
98+ this IRequestExecutorBuilder builder ,
99+ bool skipIf = false )
100+ where T : class , IRequestExecutorWarmupTask
101+ {
102+ ArgumentNullException . ThrowIfNull ( builder ) ;
85103
86- if ( options . Warmup is null )
87- {
88- warmup = async ( executor , cancellationToken )
89- => await SchemaFileExporter . Export ( schemaFileName , executor , cancellationToken ) ;
90- }
91- else
92- {
93- warmup = async ( executor , cancellationToken ) =>
94- {
95- await SchemaFileExporter . Export ( schemaFileName , executor , cancellationToken ) ;
96- await options . Warmup ( executor , cancellationToken ) ;
97- } ;
98- }
99- }
100- else
104+ if ( skipIf )
101105 {
102- warmup = options . Warmup ;
106+ return builder ;
103107 }
104108
105- return InitializeOnStartup ( builder , warmup , options . KeepWarm ) ;
109+ builder . ConfigureSchemaServices (
110+ static ( _ , sc ) => sc . AddSingleton < IRequestExecutorWarmupTask , T > ( ) ) ;
111+
112+ return builder ;
106113 }
107114
108115 /// <summary>
@@ -114,26 +121,24 @@ public static IRequestExecutorBuilder InitializeOnStartup(
114121 /// <param name="schemaFileName">
115122 /// The file name of the schema file.
116123 /// </param>
124+ /// <param name="skipIf">
125+ /// If <c>true</c>, the schema file will not be exported.
126+ /// </param>
117127 /// <returns>
118128 /// Returns the <see cref="IRequestExecutorBuilder"/> so that configuration can be chained.
119129 /// </returns>
120130 /// <exception cref="ArgumentNullException">
121- /// The <see cref="IRequestExecutorBuilder "/> is <c>null</c>.
131+ /// The <paramref name="builder "/> is <c>null</c>.
122132 /// </exception>
123133 public static IRequestExecutorBuilder ExportSchemaOnStartup (
124134 this IRequestExecutorBuilder builder ,
125- string ? schemaFileName = null )
135+ string ? schemaFileName = null ,
136+ bool skipIf = false )
126137 {
127138 ArgumentNullException . ThrowIfNull ( builder ) ;
128139
129- return InitializeOnStartup ( builder , new RequestExecutorInitializationOptions
130- {
131- KeepWarm = true ,
132- WriteSchemaFile = new SchemaFileInitializationOptions
133- {
134- Enable = true ,
135- FileName = schemaFileName
136- }
137- } ) ;
140+ schemaFileName ??= System . IO . Path . Combine ( Environment . CurrentDirectory , "schema.graphqls" ) ;
141+
142+ return builder . AddWarmupTask ( new SchemaFileExporterWarmupTask ( schemaFileName ) , skipIf ) ;
138143 }
139144}
0 commit comments