Skip to content

Use orchestrator container delay start feature for better WithExplictStart + ContainerLifetime.Persistent support #9471

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

Merged
merged 5 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions src/Aspire.Hosting/Dcp/DcpExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,12 @@ private static ResourceStatus GetResourceStatus(CustomResource resource)
{
if (resource is Container container)
{
if (container.Spec.Start == false && (container.Status?.State == null || container.Status?.State == ContainerState.Pending))
{
// If the resource is set for delay start, treat pending states as NotStarted.
return new(KnownResourceStates.NotStarted, null, null);
}

return new(container.Status?.State, container.Status?.StartupTimestamp?.ToUniversalTime(), container.Status?.FinishTimestamp?.ToUniversalTime());
}
if (resource is Executable executable)
Expand Down Expand Up @@ -1223,8 +1229,10 @@ async Task CreateContainerAsyncCore(AppResource cr, CancellationToken cancellati

if (cr.ModelResource.TryGetLastAnnotation<ExplicitStartupAnnotation>(out _))
{
await _executorEvents.PublishAsync(new OnResourceChangedContext(cancellationToken, KnownResourceTypes.Container, cr.ModelResource, cr.DcpResourceName, new ResourceStatus(KnownResourceStates.NotStarted, null, null), s => s with { State = new ResourceStateSnapshot(KnownResourceStates.NotStarted, null) })).ConfigureAwait(false);
Copy link
Member

@davidfowl davidfowl May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels scary, what else does firing this event do that is being missed now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This event was a virtual status update; the app model was skipping creating the DCP resource and just reporting it as "NotStarted". Now that DCP has support for a resource that should be explicitly started, the resource still ends up with a "NotStarted" status event, but it's in response to the actual DCP resource status (State=pending + Start=false).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the user sets environment variables in before start. Does that fire still?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProcessEnvironmentVariableValuesAsync gets called and the environment gets generated.

continue;
if (cr.DcpResource is Container container)
{
container.Spec.Start = false;
}
}

tasks.Add(CreateContainerAsyncCore(cr, cancellationToken));
Expand Down Expand Up @@ -1553,6 +1561,9 @@ public async Task StartResourceAsync(IResourceReference resourceReference, Cance
case Container c:
await EnsureResourceDeletedAsync<Container>(appResource.DcpResourceName).ConfigureAwait(false);

// Ensure we explicitly start the container
c.Spec.Start = true;

await _executorEvents.PublishAsync(new OnResourceStartingContext(cancellationToken, resourceType, appResource.ModelResource, appResource.DcpResourceName)).ConfigureAwait(false);
await CreateContainerAsync(appResource, resourceLogger, cancellationToken).ConfigureAwait(false);
break;
Expand Down
7 changes: 7 additions & 0 deletions src/Aspire.Hosting/Dcp/Model/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ internal sealed class ContainerSpec
[JsonPropertyName("networks")]
public List<ContainerNetworkConnection>? Networks { get; set; }

/// <summary>
/// Should this resource be started? If set to false, we will not attempt
/// to start the resource until Start is set to true (or null).
/// </summary>
[JsonPropertyName("start")]
public bool? Start { get; set; }

// Should this resource be stopped?
[JsonPropertyName("stop")]
public bool? Stop { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions src/Aspire.Hosting/Dcp/ResourceSnapshotBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public CustomResourceSnapshot ToSnapshot(Container container, CustomResourceSnap
var environment = GetEnvironmentVariables(container.Status?.EffectiveEnv ?? container.Spec.Env, container.Spec.Env);
var state = container.Status?.State;

if (container.Spec.Start == false && (state == null || state == ContainerState.Pending))
{
state = KnownResourceStates.NotStarted;
}

var relationships = ImmutableArray<RelationshipSnapshot>.Empty;

(ImmutableArray<string> Args, ImmutableArray<int>? ArgsAreSensitive, bool IsSensitive)? launchArguments = null;
Expand Down
Loading