spring icon indicating copy to clipboard operation
spring copied to clipboard

Kill a process when spring stop

Open GCorbel opened this issue 10 years ago • 16 comments

This question is related to this issue and this question.

I'm looking for a way to kill Ember, or any process, when spring is stopped. Is it possible. Do you have an idea?

GCorbel avatar Aug 26 '15 23:08 GCorbel

allowing to overwrite pre-boot steps like start & stop might solve that ... maybe some config/spring-preboot.rb could be supported where you can go crazy and make stop do all kinds of things ...

grosser avatar Aug 26 '15 23:08 grosser

Monkey patching? No cleaner way?

GCorbel avatar Aug 27 '15 00:08 GCorbel

Use Signal.trap("TERM") { exec('touch /tmp/test123') } seems to works when spring is stopped. I don't know if their can be conflicts.

GCorbel avatar Aug 27 '15 00:08 GCorbel

I think I can you only one Signal.trap in the whole app so it's not a good way to go. Monkey Patching does not work because Spring is loaded after the initializers (I think) and methods I'm trying to create are overwritten. Any clue?

GCorbel avatar Aug 27 '15 11:08 GCorbel

FYI You can trap a signal and then call the previous trap:

https://github.com/grosser/parallel/blob/master/lib/parallel.rb#L158-L171

but not something I'd recommend ...

some pre-boot configuration file would be nice to be able to control start/stop etc behavior ...

On Thu, Aug 27, 2015 at 4:20 AM, Guirec Corbel [email protected] wrote:

I think I can you only one Signal.trap in the whole app so it's not a good way to go. Monkey Patching does not work because Spring is loaded after the initializers (I think) and methods I'm trying to create are overwritten. Any clue?

— Reply to this email directly or view it on GitHub https://github.com/rails/spring/issues/424#issuecomment-135392199.

grosser avatar Aug 27 '15 15:08 grosser

I agree but I don't know how to do. I have to do something when spring stops so I have to know when it stop and interact with EmberCLI. I don't see a solution to stop a process when spring is stopped. Do you have a clue?

GCorbel avatar Aug 27 '15 15:08 GCorbel

@grosser, the only think I see is to do a callback function Spring.after_stop or Spring.after_kill. If you agree, I will do a pull request.

GCorbel avatar Sep 03 '15 11:09 GCorbel

https://github.com/rails/spring/pull/426 would allow you to do:

Spring::Client::Stop.prepend(Module.new do
  def call(args)
    super
    # do something else
  end
end)

does that look good ?

grosser avatar Sep 03 '15 15:09 grosser

It does not work. Spring::Client is undefined so I required it and it's never triggered.

GCorbel avatar Sep 03 '15 23:09 GCorbel

did you put it in config/spring_preboot.rb and use gem installed from the branch ? (rake install)

grosser avatar Sep 03 '15 23:09 grosser

I did not. Now, it installed it properly. The code is called for each run even if I did not spring stop.

GCorbel avatar Sep 04 '15 00:09 GCorbel

Sorry, I was wrong. It seems to work. I have some verifications to do before to close this issue.

GCorbel avatar Sep 04 '15 00:09 GCorbel

Once you verified I'll merge the PR and release a new version :)

On Thu, Sep 3, 2015 at 5:10 PM, Guirec Corbel [email protected] wrote:

Sorry, I was wrong. It seems to work. I have some verifications to do before to close this issue.

— Reply to this email directly or view it on GitHub https://github.com/rails/spring/issues/424#issuecomment-137605826.

grosser avatar Sep 04 '15 00:09 grosser

I only have two problems but about EmberCLI-Rails. First, EmberCLI[:frontend] is undefined on spring_preboot.rb. I suppose all gems are not loaded.

The second problem, too linked to EmberCLI-Rails I think, is than there is 3 processes launched. The PID stored in the app is the first one and I need to kill the second one. I have to work to resolve this issue.

GCorbel avatar Sep 04 '15 00:09 GCorbel

can you ps -ef | grep foobar and then get the pids to kill them ? but the preboot.rb looks like it helps / should I merge it ?

On Thu, Sep 3, 2015 at 5:47 PM, Guirec Corbel [email protected] wrote:

I only have two problems but about EmberCLI-Rails. First, EmberCLI[:frontend] is undefined on spring_preboot.rb. I suppose all gems are not loaded.

The second problem, too linked to EmberCLI-Rails I think, is than there is 3 processes launched. The PID stored in the app is the first one and I need to kill the second one. I have to work to resolve this issue.

— Reply to this email directly or view it on GitHub https://github.com/rails/spring/issues/424#issuecomment-137612523.

grosser avatar Sep 04 '15 15:09 grosser

With ps -ef | grep ember, I can have other processes not related.

preboot.rb will help for sure. You can merge it.

GCorbel avatar Sep 04 '15 15:09 GCorbel