Skip to content

Commit

Permalink
Suppress dashboard resource client error on shutdown (#6315)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Oct 16, 2024
1 parent 7d23b1d commit 4f7dbd4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/Aspire.Hosting/Dashboard/DashboardLifecycleHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Aspire.Hosting.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

Expand All @@ -27,10 +28,11 @@ internal sealed class DashboardLifecycleHook(IConfiguration configuration,
ResourceNotificationService resourceNotificationService,
ResourceLoggerService resourceLoggerService,
ILoggerFactory loggerFactory,
DcpNameGenerator nameGenerator) : IDistributedApplicationLifecycleHook, IAsyncDisposable
DcpNameGenerator nameGenerator,
IHostApplicationLifetime hostApplicationLifetime) : IDistributedApplicationLifecycleHook, IAsyncDisposable
{
private readonly CancellationTokenSource _shutdownCts = new();
private Task? _dashboardLogsTask;
private CancellationTokenSource? _dashboardLogsCts;

public Task BeforeStartAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken)
{
Expand All @@ -49,14 +51,20 @@ public Task BeforeStartAsync(DistributedApplicationModel appModel, CancellationT
AddDashboardResource(appModel);
}

_dashboardLogsTask = WatchDashboardLogsAsync(_shutdownCts.Token);
// Stop watching logs from the dashboard when the app host is stopping. Part of the app host shutdown is tearing down the dashboard service.
// Dashboard services are killed while the dashboard is using them and will cause the dashboard to report an error accessing data.
// By stopping here we prevent the app host from printing errors from the dashboard caused by shutdown.
_dashboardLogsCts = CancellationTokenSource.CreateLinkedTokenSource(hostApplicationLifetime.ApplicationStopping);

_dashboardLogsTask = WatchDashboardLogsAsync(_dashboardLogsCts.Token);

return Task.CompletedTask;
}

public async ValueTask DisposeAsync()
{
_shutdownCts.Cancel();
// Stop listening to logs if the lifecycle hook is disposed without the app being shutdown.
_dashboardLogsCts?.Cancel();

if (_dashboardLogsTask is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ private static DashboardLifecycleHook CreateHook(
resourceNotificationService,
resourceLoggerService,
loggerFactory ?? NullLoggerFactory.Instance,
new DcpNameGenerator(configuration, Options.Create(new DcpOptions())));
new DcpNameGenerator(configuration, Options.Create(new DcpOptions())),
new TestHostApplicationLifetime());
}

public static IEnumerable<object?[]> Data()
Expand Down

0 comments on commit 4f7dbd4

Please sign in to comment.