I want to automatically stop Spring that has been idle for a certain period of time.

Asked 2 years ago, Updated 2 years ago, 60 views

Recently, when I thought that my PC was running low on memory, I noticed that several Rails apps left Spring running (checked on the Mac Activity Monitor).

spring stop stops the process and frees up memory, but it's not realistic to stop every time you go back and forth between various Rails apps.

For example, if you have been idle for a certain period of time like Pow, you can automatically stop Spring, which reduces unnecessary memory consumption.

I'd like to stop the spring that has been started in some way, but is there any good way?

ruby-on-rails rails-spring

2022-09-30 12:00

1 Answers

For resident processes started by Spring, how about installing a timer to stop the main thread after a certain period of time (==make the resident process exit).

I tried the code below and it worked.Write this down in ~/.spring.rb or config/spring.rb. Try changing it to make it easier to use around sleep.

Thread.new do
  sleepENV.fetch('SPRING_AUTOSTOP', 1.day).to_i
  Thread.main.exit
end

(Rails 4.1.7 Spring 1.1.3)

Note: https://github.com/rails/spring#running-code-before-forking


2022-09-30 12:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.