Skip to content

Instantly share code, notes, and snippets.

@ridiculous
Last active January 31, 2017 04:06
Show Gist options
  • Save ridiculous/68893183d0675fc52007b8eec54b0992 to your computer and use it in GitHub Desktop.
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 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
@ridiculous
Copy link
Author

See https://github.com/ridiculous/signals for an example of trapping signals to hot-reload some code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment