Skip to content

Instantly share code, notes, and snippets.

@defunkt
Created October 12, 2009 18:03

Revisions

  1. defunkt revised this gist Oct 12, 2009. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,11 @@
    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(' ')
  2. defunkt created this gist Oct 12, 2009.
    25 changes: 25 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # 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
    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