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

Execute arbitrary code once a resource is up and running (some kind of "after start" event) #2155

Closed
asimmon opened this issue Feb 9, 2024 · 6 comments · Fixed by #2543
Closed
Assignees
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication feature A single user-facing feature. Can be grouped under an epic.

Comments

@asimmon
Copy link

asimmon commented Feb 9, 2024

Currently, the IDistributedApplicationLifecycleHook hook only allows us to modify the app model before starting the resources. Is there a plan to add a hook that allows us to respond to an event indicating that a resource is up and running (or has failed to start, or any status change)? Does the orchestrator (DCP) support such a feature?

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Feb 9, 2024
@davidfowl
Copy link
Member

I think we should add a hook that runs after the resources were told to start, but it doesn't mean they are healthy. Your code would still need to be resilient to that.

@asimmon
Copy link
Author

asimmon commented Feb 10, 2024

It makes sense that my code should be resilient, as we cannot predict the behavior of the resources. However, I'm trying to understand the precise definition of "executing code after the resources were told to start."

Does this mean it would trigger immediately after the .NET Aspire part has finished its job of pushing the desired state to the orchestrator?

Or does the .NET Aspire part wait to receive at least some indication that the orchestrator attempted to start the resource, which could result in various states, such as: process not found, container not found, or resource has started (which doesn't mean it's healthy)? Thinking about it, it reminds me of CliWrap, which returns a CommandTask containing the process information if started (although the process could fail miserably) or throws an exception if the process wasn't found.

If we cannot rely on such a signal, I could implement today an IDistributedApplicationLifecycleHook.AfterEndpointsAllocatedAsync, start my resilient code in a "detached" task whose lifetime is secured by using the provided cancellation token, and wait for the resource to actually become available.

@davidfowl
Copy link
Member

Does this mean it would trigger immediately after the .NET Aspire part has finished its job of pushing the desired state to the orchestrator?

This. We tell the orchestrator to start, but we there's no concept of health in the system as yet

@davidfowl davidfowl added this to the preview 5 (Apr) milestone Feb 25, 2024
@danmoseley danmoseley added the feature A single user-facing feature. Can be grouped under an epic. label Feb 26, 2024
@BrennanConroy
Copy link
Member

So we're thinking somthing like

interface IDistributedApplicationLifecycleHook
{
     // Runs after resources have been orchestrated. There is no guarantee that the resources are healthy or ready for use at this point.
+    Task AfterStartAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken = default);
}

Or AfterResourcesCreatedAsync/AfterResourcesProvisionedAsync?

@davidfowl
Copy link
Member

Yea I think that's right.

@davidfowl
Copy link
Member

I prefer AfterResourcesCreatedAsync

@github-actions github-actions bot locked and limited conversation to collaborators Apr 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication feature A single user-facing feature. Can be grouped under an epic.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants