@@ -231,8 +231,42 @@ static bool IsContinuableState(WaitBehavior waitBehavior, CustomResourceSnapshot
231231 /// </remarks>
232232 public async Task < ResourceEvent > WaitForResourceHealthyAsync ( string resourceName , CancellationToken cancellationToken = default )
233233 {
234+ return await WaitForResourceHealthyAsync (
235+ resourceName ,
236+ WaitBehavior . WaitOnDependencyFailure , // Retain default behavior.
237+ cancellationToken ) . ConfigureAwait ( false ) ;
238+ }
239+
240+ /// <summary>
241+ /// Waits for a resource to become healthy.
242+ /// </summary>
243+ /// <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>
245+ /// <param name="cancellationToken">The cancellation token.</param>
246+ /// <returns>A task.</returns>
247+ /// <remarks>
248+ /// 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.
251+ /// </remarks>
252+ public async Task < ResourceEvent > WaitForResourceHealthyAsync ( string resourceName , WaitBehavior waitBehavior , CancellationToken cancellationToken = default )
253+ {
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+
234261 _logger . LogDebug ( "Waiting for resource '{Name}' to enter the '{State}' state." , resourceName , HealthStatus . Healthy ) ;
235- var resourceEvent = await WaitForResourceCoreAsync ( resourceName , re => re . Snapshot . HealthStatus == HealthStatus . Healthy , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
262+ var resourceEvent = await WaitForResourceCoreAsync ( resourceName , waitCondition , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
263+
264+ if ( resourceEvent . Snapshot . HealthStatus != HealthStatus . Healthy )
265+ {
266+ _logger . LogError ( "Stopped waiting for resource '{ResourceName}' to become healthy because it failed to start." , resourceName ) ;
267+ throw new DistributedApplicationException ( $ "Stopped waiting for resource '{ resourceName } ' to become healthy because it failed to start.") ;
268+ }
269+
236270 _logger . LogDebug ( "Finished waiting for resource '{Name}'." , resourceName ) ;
237271
238272 return resourceEvent ;
0 commit comments