From 56c7af940dbf76aee98375b2128e0cb5712312ed Mon Sep 17 00:00:00 2001 From: Lukas Matt Date: Wed, 13 Apr 2016 10:51:05 +0200 Subject: [PATCH] Move unicorn_killer to Gemfile --- Gemfile | 1 + Gemfile.lock | 5 ++++ config.ru | 7 ++++-- lib/unicorn_killer.rb | 53 ------------------------------------------- 4 files changed, 11 insertions(+), 55 deletions(-) delete mode 100644 lib/unicorn_killer.rb diff --git a/Gemfile b/Gemfile index 3472389b0..f8b9bfdda 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ gem "responders", "2.1.1" # Appserver gem "unicorn", "5.0.1", require: false +gem "unicorn-worker-killer", "0.4.4" # Federation diff --git a/Gemfile.lock b/Gemfile.lock index f30b592d6..cba0ca07f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -353,6 +353,7 @@ GEM fuubar (2.0.0) rspec (~> 3.0) ruby-progressbar (~> 1.4) + get_process_mem (0.2.0) gherkin (3.2.0) gitlab (3.4.0) httparty @@ -860,6 +861,9 @@ GEM kgio (~> 2.6) rack raindrops (~> 0.7) + unicorn-worker-killer (0.4.4) + get_process_mem (~> 0) + unicorn (>= 4, < 6) url_safe_base64 (0.2.2) uuid (2.3.8) macaddr (~> 1.0) @@ -1032,6 +1036,7 @@ DEPENDENCIES typhoeus (= 1.0.1) uglifier (= 2.7.2) unicorn (= 5.0.1) + unicorn-worker-killer (= 0.4.4) uuid (= 2.3.8) versionist (= 1.4.1) webmock (= 1.22.6) diff --git a/config.ru b/config.ru index e25248600..2f5ac99d8 100644 --- a/config.ru +++ b/config.ru @@ -5,12 +5,15 @@ # This file is used by Rack-based servers to start the application. require ::File.expand_path("../config/environment", __FILE__) -require ::File.expand_path("../lib/unicorn_killer", __FILE__) require ::File.expand_path("../lib/rack/internet_explorer_version", __FILE__) # Kill unicorn workers really aggressively (at 300mb) if defined?(Unicorn) - use UnicornKiller::Oom, 300 * 1024 + require "unicorn/worker_killer" + oom_min = (280) * (1024**2) + oom_max = (300) * (1024**2) + # Max memory size (RSS) per worker + use Unicorn::WorkerKiller::Oom, oom_min, oom_max end use Rack::Deflater use Rack::InternetExplorerVersion, minimum: 9 diff --git a/lib/unicorn_killer.rb b/lib/unicorn_killer.rb deleted file mode 100644 index 7e908538d..000000000 --- a/lib/unicorn_killer.rb +++ /dev/null @@ -1,53 +0,0 @@ -# # your config.ru -# require 'unicorn_killer' -# use UnicornKiller::MaxRequests, 1000 -# use UnicornKiller::Oom, 400 * 1024 - -module UnicornKiller - module Kill - def quit - sec = (Time.now - @process_start).to_i - warn "#{self.class} send SIGQUIT (pid: #{Process.pid})\talive: #{sec} sec" - Process.kill :QUIT, Process.pid - end - end - - class Oom - include Kill - - def initialize(app, memory_size= 512 * 1024, check_cycle = 10) - @app = app - @memory_size = memory_size - @check_cycle = check_cycle - @check_count = 0 - end - - def rss - `ps -o rss= -p #{Process.pid}`.to_i - end - - def call(env) - @process_start ||= Time.now - if (@check_count += 1) % @check_cycle == 0 - @check_count = 0 - quit if rss > @memory_size - end - @app.call env - end - end - - class MaxRequests - include Kill - - def initialize(app, max_requests = 1000) - @app = app - @max_requests = max_requests - end - - def call(env) - @process_start ||= Time.now - quit if (@max_requests -= 1) == 0 - @app.call env - end - end -end