Skip to content

Instantly share code, notes, and snippets.

@defunkt
Created October 12, 2009 18:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save defunkt/208588 to your computer and use it in GitHub Desktop.
Save defunkt/208588 to your computer and use it in GitHub Desktop.
# This will ride alongside god and kill any rogue memory-greedy
# processes. Their sacrifice is for the greater good.
unicorn_worker_memory_limit = 300_000
Thread.new do
loop do
begin
# unicorn workers
#
# ps output line format:
# 31580 275444 unicorn_rails worker[15] -c /data/github/current/config/unicorn.rb -E production -D
# pid ram command
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 30
end
end
@chengguangnan
Copy link

I don't understand why you used a Thread.new here.

@chetan
Copy link

chetan commented Dec 12, 2012

I think the idea is this code is basically concatenated to the normal unicorn.god config file, so it's running inside the god process.

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