Skip to content

Commit

Permalink
[release/8.0-preview3] Add CTRL-C and working directory messages. (#2003
Browse files Browse the repository at this point in the history
)

* Add CTRL-C and working directory messages.

* Added a lifecycle service.

* Change log category.

---------

Co-authored-by: Mitch Denny <midenn@microsoft.com>
  • Loading branch information
github-actions[bot] and mitchdenny committed Jan 31, 2024
1 parent c7bb15f commit 7ffc9ba
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
4 changes: 1 addition & 3 deletions src/Aspire.Hosting/Dcp/ApplicationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ServiceAppResource(IResource modelResource, Service service, EndpointAnno
}

internal sealed class ApplicationExecutor(ILogger<ApplicationExecutor> logger,
ILoggerFactory loggerFactory,
ILogger<DistributedApplication> distributedApplicationLogger,
DistributedApplicationModel model,
DistributedApplicationOptions distributedApplicationOptions,
KubernetesService kubernetesService,
Expand Down Expand Up @@ -261,8 +261,6 @@ private async Task CheckDashboardAvailabilityAsync(string delimitedUrlList, Canc
if (StringUtils.TryGetUriFromDelimitedString(delimitedUrlList, ";", out var firstDashboardUrl))
{
await WaitForHttpSuccessOrThrow(firstDashboardUrl, DashboardAvailabilityTimeoutDuration, cancellationToken).ConfigureAwait(false);

var distributedApplicationLogger = loggerFactory.CreateLogger<DistributedApplication>();
distributedApplicationLogger.LogInformation("Now listening on: {DashboardUrl}", firstDashboardUrl.ToString().TrimEnd('/'));
}
else
Expand Down
17 changes: 0 additions & 17 deletions src/Aspire.Hosting/DistributedApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
using System.Diagnostics;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Lifecycle;
using Aspire.Hosting.Publishing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Aspire.Hosting;

Expand Down Expand Up @@ -113,7 +111,6 @@ public ValueTask DisposeAsync()
/// <inheritdoc cref="IHost.StartAsync" />
public async Task StartAsync(CancellationToken cancellationToken = default)
{
WriteStartingLog();
await _host.StartAsync(cancellationToken).ConfigureAwait(false);
await ExecuteBeforeStartHooksAsync(cancellationToken).ConfigureAwait(false);
}
Expand All @@ -127,24 +124,10 @@ public async Task StopAsync(CancellationToken cancellationToken = default)
/// <inheritdoc cref="HostingAbstractionsHostExtensions.RunAsync" />
public async Task RunAsync(CancellationToken cancellationToken = default)
{
WriteStartingLog();
await ExecuteBeforeStartHooksAsync(cancellationToken).ConfigureAwait(false);
await _host.RunAsync(cancellationToken).ConfigureAwait(false);
}

private void WriteStartingLog()
{
var options = _host.Services.GetRequiredService<IOptions<PublishingOptions>>();

if (options.Value?.Publisher == "manifest")
{
// If we are producing the manifest, don't write startup messages.
return;
}

_logger.LogInformation("Distributed application starting.");
}

/// <summary>
/// Runs the distributed application and only completes when the token is triggered or shutdown is triggered.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Aspire.Hosting/DistributedApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public DistributedApplicationBuilder(DistributedApplicationOptions options)

// Core things
_innerBuilder.Services.AddSingleton(sp => new DistributedApplicationModel(Resources));
_innerBuilder.Services.AddHostedService<DistributedApplicationLifecycle>();
_innerBuilder.Services.AddHostedService<DistributedApplicationRunner>();
_innerBuilder.Services.AddSingleton(options);
_innerBuilder.Services.AddSingleton<IEnvironmentVariables, EnvironmentVariables>();
Expand Down
56 changes: 56 additions & 0 deletions src/Aspire.Hosting/DistributedApplicationLifecycle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.Publishing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Aspire.Hosting;

internal sealed class DistributedApplicationLifecycle(ILogger<DistributedApplication> logger, IConfiguration configuration, IOptions<PublishingOptions> publishingOptions) : IHostedLifecycleService
{
public Task StartAsync(CancellationToken cancellationToken)
{
_ = logger;
_ = publishingOptions;
return Task.CompletedTask;
}

public Task StartedAsync(CancellationToken cancellationToken)
{
if (publishingOptions.Value.Publisher != "manifest")
{
logger.LogInformation("Distributed application started. Press CTRL-C to stop.");
}

return Task.CompletedTask;
}

public Task StartingAsync(CancellationToken cancellationToken)
{
if (publishingOptions.Value.Publisher != "manifest")
{
logger.LogInformation("Distributed application starting.");
logger.LogInformation("Application host directory is: {AppHostDirectory}", configuration["AppHost:Directory"]);
}

return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}

public Task StoppedAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}

public Task StoppingAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}

0 comments on commit 7ffc9ba

Please sign in to comment.