@@ -50,7 +50,7 @@ public ResourceNotificationService(ILogger<ResourceNotificationService> logger,
5050 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
5151 _serviceProvider = new NullServiceProvider ( ) ;
5252 _resourceLoggerService = new ResourceLoggerService ( ) ;
53- DefaultWaitBehavior = WaitBehavior . StopOnDependencyFailure ;
53+ DefaultWaitBehavior = WaitBehavior . StopOnResourceUnavailable ;
5454 }
5555
5656 /// <summary>
@@ -69,7 +69,7 @@ public ResourceNotificationService(
6969 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
7070 _serviceProvider = serviceProvider ;
7171 _resourceLoggerService = resourceLoggerService ?? throw new ArgumentNullException ( nameof ( resourceLoggerService ) ) ;
72- DefaultWaitBehavior = serviceProvider . GetService < IOptions < ResourceNotificationServiceOptions > > ( ) ? . Value . DefaultWaitBehavior ?? WaitBehavior . StopOnDependencyFailure ;
72+ DefaultWaitBehavior = serviceProvider . GetService < IOptions < ResourceNotificationServiceOptions > > ( ) ? . Value . DefaultWaitBehavior ?? WaitBehavior . StopOnResourceUnavailable ;
7373
7474 // The IHostApplicationLifetime parameter is not used anymore, but we keep it for backwards compatibility.
7575 // Notification updates will be cancelled when the service is disposed.
@@ -161,7 +161,7 @@ async Task Core(string displayName, string resourceId)
161161 var resourceEvent = await WaitForResourceCoreAsync ( dependency . Name , re => re . ResourceId == resourceId && IsContinuableState ( waitBehavior , re . Snapshot ) , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
162162 var snapshot = resourceEvent . Snapshot ;
163163
164- if ( waitBehavior == WaitBehavior . StopOnDependencyFailure )
164+ if ( waitBehavior == WaitBehavior . StopOnResourceUnavailable )
165165 {
166166 if ( snapshot . State ? . Text == KnownResourceStates . FailedToStart )
167167 {
@@ -208,8 +208,8 @@ async Task Core(string displayName, string resourceId)
208208 static bool IsContinuableState ( WaitBehavior waitBehavior , CustomResourceSnapshot snapshot ) =>
209209 waitBehavior switch
210210 {
211- WaitBehavior . WaitOnDependencyFailure => snapshot . State ? . Text == KnownResourceStates . Running ,
212- WaitBehavior . StopOnDependencyFailure => snapshot . State ? . Text == KnownResourceStates . Running ||
211+ WaitBehavior . WaitOnResourceUnavailable => snapshot . State ? . Text == KnownResourceStates . Running ,
212+ WaitBehavior . StopOnResourceUnavailable => snapshot . State ? . Text == KnownResourceStates . Running ||
213213 snapshot . State ? . Text == KnownResourceStates . Finished ||
214214 snapshot . State ? . Text == KnownResourceStates . Exited ||
215215 snapshot . State ? . Text == KnownResourceStates . FailedToStart ||
@@ -233,33 +233,25 @@ public async Task<ResourceEvent> WaitForResourceHealthyAsync(string resourceName
233233 {
234234 return await WaitForResourceHealthyAsync (
235235 resourceName ,
236- WaitBehavior . WaitOnDependencyFailure , // Retain default behavior.
236+ WaitBehavior . WaitOnResourceUnavailable , // Retain default behavior.
237237 cancellationToken ) . ConfigureAwait ( false ) ;
238238 }
239239
240240 /// <summary>
241241 /// Waits for a resource to become healthy.
242242 /// </summary>
243243 /// <param name="resourceName">The name of the resource.</param>
244- /// <param name="waitBehavior">The behavior to use when waiting for the resource to become healthy.</param>
245244 /// <param name="cancellationToken">The cancellation token.</param>
245+ /// <param name="waitBehavior">The wait behavior.</param>
246246 /// <returns>A task.</returns>
247247 /// <remarks>
248248 /// This method returns a task that will complete with the resource is healthy. A resource
249- /// without <see cref="HealthCheckAnnotation"/> annotations will be considered healthy. This overload
250- /// will throw a <see cref="Aspire.Hosting.DistributedApplicationException"/> if the resource fails to start.
249+ /// without <see cref="HealthCheckAnnotation"/> annotations will be considered healthy.
251250 /// </remarks>
252251 public async Task < ResourceEvent > WaitForResourceHealthyAsync ( string resourceName , WaitBehavior waitBehavior , CancellationToken cancellationToken = default )
253252 {
254- var waitCondition = waitBehavior switch
255- {
256- WaitBehavior . WaitOnDependencyFailure => ( Func < ResourceEvent , bool > ) ( re => re . Snapshot . HealthStatus == HealthStatus . Healthy ) ,
257- WaitBehavior . StopOnDependencyFailure => ( Func < ResourceEvent , bool > ) ( re => re . Snapshot . HealthStatus == HealthStatus . Healthy || re . Snapshot . State ? . Text == KnownResourceStates . FailedToStart ) ,
258- _ => throw new DistributedApplicationException ( $ "Unexpected wait behavior: { waitBehavior } ")
259- } ;
260-
261253 _logger . LogDebug ( "Waiting for resource '{Name}' to enter the '{State}' state." , resourceName , HealthStatus . Healthy ) ;
262- var resourceEvent = await WaitForResourceCoreAsync ( resourceName , waitCondition , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
254+ var resourceEvent = await WaitForResourceCoreAsync ( resourceName , re => ShouldYield ( waitBehavior , re . Snapshot ) , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
263255
264256 if ( resourceEvent . Snapshot . HealthStatus != HealthStatus . Healthy )
265257 {
@@ -270,6 +262,19 @@ public async Task<ResourceEvent> WaitForResourceHealthyAsync(string resourceName
270262 _logger . LogDebug ( "Finished waiting for resource '{Name}'." , resourceName ) ;
271263
272264 return resourceEvent ;
265+
266+ // Determine if we should yield based on the wait behavior and the snapshot of the resource.
267+ static bool ShouldYield ( WaitBehavior waitBehavior , CustomResourceSnapshot snapshot ) =>
268+ waitBehavior switch
269+ {
270+ WaitBehavior . WaitOnResourceUnavailable => snapshot . HealthStatus == HealthStatus . Healthy ,
271+ WaitBehavior . StopOnResourceUnavailable => snapshot . HealthStatus == HealthStatus . Healthy ||
272+ snapshot . State ? . Text == KnownResourceStates . Finished ||
273+ snapshot . State ? . Text == KnownResourceStates . Exited ||
274+ snapshot . State ? . Text == KnownResourceStates . FailedToStart ||
275+ snapshot . State ? . Text == KnownResourceStates . RuntimeUnhealthy ,
276+ _ => throw new DistributedApplicationException ( $ "Unexpected wait behavior: { waitBehavior } ")
277+ } ;
273278 }
274279
275280 private async Task WaitUntilCompletionAsync ( IResource resource , IResource dependency , int exitCode , CancellationToken cancellationToken )
0 commit comments