Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't throw from hosted services #5699

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private async Task WatchNotifications(CancellationToken cancellationToken)

await Task.WhenAll(logWatchTasks).ConfigureAwait(false);
}
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested)
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// this was expected as the token was canceled
}
Expand Down
21 changes: 14 additions & 7 deletions src/Aspire.Hosting/Health/ResourceHealthCheckScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,24 @@ public ResourceHealthCheckScheduler(ResourceNotificationService resourceNotifica

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var resourceEvents = _resourceNotificationService.WatchAsync(stoppingToken);

await foreach (var resourceEvent in resourceEvents.ConfigureAwait(false))
try
{
if (resourceEvent.Snapshot.State?.Text == KnownResourceStates.Running)
var resourceEvents = _resourceNotificationService.WatchAsync(stoppingToken);

await foreach (var resourceEvent in resourceEvents.ConfigureAwait(false))
{
// Each time we receive an event that tells us that the resource is
// running we need to enable the health check annotation.
UpdateCheckEnablement(resourceEvent.Resource, true);
if (resourceEvent.Snapshot.State?.Text == KnownResourceStates.Running)
{
// Each time we receive an event that tells us that the resource is
// running we need to enable the health check annotation.
UpdateCheckEnablement(resourceEvent.Resource, true);
}
}
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
// This was expected as the token was canceled
}
}

private void UpdateCheckEnablement(IResource resource, bool enabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task BackgroundServiceIsRegisteredInServiceProvider()
}

[Fact]
public async Task ExecuteThrowsOperationCanceledWhenAppStoppingTokenSignaled()
public async Task ExecuteDoesNotThrowOperationCanceledWhenAppStoppingTokenSignaled()
{
var hostApplicationLifetime = new TestHostApplicationLifetime();
var resourceNotificationService = new ResourceNotificationService(NullLogger<ResourceNotificationService>.Instance, hostApplicationLifetime);
Expand All @@ -44,10 +44,7 @@ public async Task ExecuteThrowsOperationCanceledWhenAppStoppingTokenSignaled()
// Signal the stopping token
hostApplicationLifetime.StopApplication();

await Assert.ThrowsAsync<OperationCanceledException>(async () =>
{
await resourceLogForwarder.ExecuteTask;
});
await resourceLogForwarder.ExecuteTask;
}

[Fact]
Expand Down
Loading