Skip to content

Instantly share code, notes, and snippets.

@t27duck
Created July 3, 2017 18:06
Show Gist options
  • Select an option

  • Save t27duck/2420569d5be8af7f9845f80deea3e2bc to your computer and use it in GitHub Desktop.

Select an option

Save t27duck/2420569d5be8af7f9845f80deea3e2bc to your computer and use it in GitHub Desktop.

Revisions

  1. t27duck created this gist Jul 3, 2017.
    27 changes: 27 additions & 0 deletions github_unicorn_killer.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #! /usr/bin/env ruby

    unicorn_worker_memory_limit = 460_000

    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 60
    end