aspire icon indicating copy to clipboard operation
aspire copied to clipboard

Start, Stop Resource Programmatically

Open KFKMan opened this issue 9 months ago • 15 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I want to Start, Stop Resources Programmatically. Can you add this feature?

KFKMan avatar Apr 19 '25 19:04 KFKMan

No, this api will never be public. Your time is better spent asking for specific features and the team will decide how best those should be expose.

Exposing APIs ties our hands and stops us from doing refactoring (this type didn’t exist before 9.1, and it’ll probably get rewritten again at some point)

davidfowl avatar Apr 19 '25 22:04 davidfowl

No, this api will never be public. Your time is better spent asking for specific features and the team will decide how best those should be expose.

Exposing APIs ties our hands and stops us from doing refactoring (this type didn’t exist before 9.1, and it’ll probably get rewritten again at some point)

Thanks for response. I updated the issue.

KFKMan avatar Apr 20 '25 07:04 KFKMan

I was thinking about this recently in the context of looking at dev tunnels too (as I'd likely want to model the invocation of the devtunnels CLI as invisible resources that are started via resource commands). We have APIs like ResourceNotificationService already so perhaps we introduce a new service like ResourceLifetimeService that has StartResourceAsync, StopResourceAsync, RestartResourceAsync, etc. on it.

DamianEdwards avatar Apr 21 '25 22:04 DamianEdwards

As a workaround that works right now, you can get the start and stop command from the resources annotations. For example

var startCommand = resourceBuilder
	.Resource
	.Annotations
	.OfType<ResourceCommandAnnotation>()
	.First(c => c.Name == "resource-start");
var stopCommand = resourceBuilder
	.Resource
	.Annotations
	.OfType<ResourceCommandAnnotation>()
	.First(c => c.Name == "resource-stop");

await stopCommand.ExecuteCommand(context);
await startCommand.ExecuteCommand(context);

jack775544 avatar Apr 23 '25 22:04 jack775544

Top tier hacking @jack775544

davidfowl avatar Apr 23 '25 23:04 davidfowl

I made a custom command for myself to stop a project, run dotnet build on the command line and then start it again after it was done. So I had to dig around to find out how to start and stop a project for that.

jack775544 avatar Apr 24 '25 00:04 jack775544

It would be nice if there was appropriate BeforeResourceStartedEvent, AfterResourceStartedEvent, BeforeResourceStoppedEvent, AfterResourceStoppedEvent events linked to this.

CZEMacLeod avatar Apr 24 '25 17:04 CZEMacLeod

Not sure what you mean. The built in resources fire events appropriately (or they are suppose me to).

davidfowl avatar Apr 24 '25 22:04 davidfowl

Not sure what you mean. The built in resources fire events appropriately (or they are suppose me to).

There are state change notifications, but not resource events (Only BeforeResourceStartedEvent) that match up with lifecycle events. There are apphost lifecycle events and IDistributedApplicationLifecycleHook but not similar for a resource. If you stop and start a resource, there are no stop events (that I can see), and the BeforeResourceStartedEvent does not trigger (again) if you stop and start or restart.

CZEMacLeod avatar Apr 24 '25 22:04 CZEMacLeod

I think BeforeResourceStartedEvent does trigger on each start of the resource (pretty sure I hit this myself). But I hear what you're saying RE more resource events rather than having to consume notifications and process state updates.

DamianEdwards avatar Apr 24 '25 23:04 DamianEdwards

Right afaik BeforeResourceStartedEvent does fire. That’s the only event we have right now that makes sense to re-trigger. The global ones fire once and the per resource ones should figure when each resource changes.

The global ones won’t fire again nor should they (it would cause too many inconsistencies!

davidfowl avatar Apr 25 '25 01:04 davidfowl

My testing shows that it didn't fire in 9.0 but does re-fire in 9.2. I wouldn't expect the global events to fire, but we are missing easy access to stop/start events per resource. I don't want to take this issue too far off topic though.

CZEMacLeod avatar Apr 25 '25 01:04 CZEMacLeod

I think there are 4 issues asking for an API to invoke commands/start/stop programmatically. The need is well understood.

davidfowl avatar Apr 25 '25 02:04 davidfowl

Is there an option to not run a project on Aspire start?

akovanev avatar Apr 29 '25 17:04 akovanev

The only option you have today is to use WithExplicitStart. Maybe follow this issue https://github.com/dotnet/aspire/issues/303?

davidfowl avatar Apr 29 '25 17:04 davidfowl

Closing in favor of https://github.com/dotnet/aspire/issues/9804

davidfowl avatar Jun 11 '25 09:06 davidfowl