From 5045ebe2e0b5fbc5732128a7e6df80dd8b662fbc Mon Sep 17 00:00:00 2001 From: 839 <8398a7@gmail.com> Date: Sun, 22 Nov 2015 04:32:20 +0900 Subject: [PATCH] add unicorn worker killer --- .env.default | 5 +++++ Gemfile | 1 + Gemfile.lock | 5 +++++ config.ru | 10 ++++++++++ docs/configuration.md | 3 +++ 5 files changed, 24 insertions(+), 0 deletions(-) diff --git a/.env.default b/.env.default index f55b14c..3a2d92a 100644 --- a/.env.default +++ b/.env.default @@ -23,3 +23,8 @@ GITHUB_API_URL=https://api.github.com GITHUB_ACCESS_SCOPE='[repo]' GITHUB_SITE_TITLE=GitHub DEVISE_MODULES='[database_authenticatable,recoverable,rememberable,trackable,validatable,omniauthable]' +USE_UNICORN_WORKER_KILLER=false +KILL_ON_REQUEST_COUNT_MIN=3072 +KILL_ON_REQUEST_COUNT_MAX=4096 +KILL_ON_RSS_MIN=250 +KILL_ON_RSS_MAX=300 diff --git a/Gemfile b/Gemfile index 50eb6dc..1705e60 100644 --- a/Gemfile +++ b/Gemfile @@ -101,6 +101,7 @@ end group :heroku, :production do gem 'rails_12factor', require: ENV.key?("HEROKU") gem 'unicorn', require: false, platform: 'ruby' + gem 'unicorn-worker-killer' end gem 'therubyracer', :platform => :ruby # C Ruby (MRI) or Rubinius, but NOT Windows diff --git a/Gemfile.lock b/Gemfile.lock index 53161c2..2f254c8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -134,6 +134,7 @@ GEM multi_json font-awesome-rails (4.2.0.0) railties (>= 3.2, < 5.0) + get_process_mem (0.2.0) globalid (0.3.6) activesupport (>= 4.1.0) haml (4.0.6) @@ -401,6 +402,9 @@ GEM kgio (~> 2.6) rack raindrops (~> 0.7) + unicorn-worker-killer (0.4.3) + get_process_mem (~> 0) + unicorn (~> 4) useragent (0.14.0) warden (1.2.3) rack (>= 1.0) @@ -480,6 +484,7 @@ DEPENDENCIES uglifier underscore-rails unicorn + unicorn-worker-killer useragent xmpp4r yajl-ruby diff --git a/config.ru b/config.ru index 76d9bb0..bde969b 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,13 @@ +if ENV['USE_UNICORN_WORKER_KILLER'] + require 'unicorn/worker_killer' + max_request_min = ENV['KILL_ON_REQUEST_COUNT_MIN'].to_i || 3072 + max_request_max = ENV['KILL_ON_REQUEST_COUNT_MAX'].to_i || 4096 + use Unicorn::WorkerKiller::MaxRequests, max_request_min, max_request_max + oom_min = ((ENV['KILL_ON_RSS_MIN'].to_i || 250) * (1024**2)) + oom_max = ((ENV['KILL_ON_RSS_MAX'].to_i || 300) * (1024**2)) + use Unicorn::WorkerKiller::Oom, oom_min, oom_max +end + # This file is used by Rack-based servers to start the application. require ::File.expand_path('../config/environment', __FILE__) diff --git a/docs/configuration.md b/docs/configuration.md index 18a888a..6f18f96 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -100,4 +100,7 @@ In order of precedence Errbit uses: