Last active
January 31, 2017 04:06
-
-
Save ridiculous/68893183d0675fc52007b8eec54b0992 to your computer and use it in GitHub Desktop.
Ruby thread to monitor ruby processes and stop them should they reach a certain memory size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file can be required from "config/unicorn.rb" so the thread will run alongside the master process | |
# This will kill any rogue memory-greedy unicorn processes | |
unicorn_worker_memory_limit = 220_000 | |
Thread.new do | |
loop do | |
begin | |
lines = `ps -e -www -o pid,rss,command | grep '[u]nicorn_rails worker'`.split("\n") | |
lines.each do |line| | |
parts = line.split(' ') | |
if parts[1].to_i > unicorn_worker_memory_limit | |
# tell the worker to die after it finishes serving its request | |
::Process.kill('QUIT', parts[0].to_i) | |
end | |
end | |
rescue Object | |
# don't die ever once we've tested this | |
nil | |
end | |
sleep 60 * 50 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://github.com/ridiculous/signals for an example of trapping signals to hot-reload some code