From 276dcf4a3b918786eabccf2451a212553cb0385e Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sat, 4 Apr 2020 18:50:01 +0200 Subject: [PATCH 1/2] Add optional unicorn-worker-killer configs --- Gemfile | 1 + Gemfile.lock | 6 ++++++ config.ru | 9 +++++++++ 3 files changed, 16 insertions(+) diff --git a/Gemfile b/Gemfile index 72dc87335c..c8bed19ead 100644 --- a/Gemfile +++ b/Gemfile @@ -122,6 +122,7 @@ gem 'ofn-qz', github: 'openfoodfoundation/ofn-qz', ref: '60da2ae4c44cbb4c8d602f5 group :production, :staging do gem 'ddtrace' + gem 'unicorn-worker-killer' end group :test, :development do diff --git a/Gemfile.lock b/Gemfile.lock index a8d86824fe..efb201586b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -411,6 +411,8 @@ GEM rspec-core (~> 3.0) ruby-progressbar (~> 1.4) geocoder (1.1.8) + get_process_mem (0.2.5) + ffi (~> 1.0) gmaps4rails (1.5.6) haml (4.0.7) tilt @@ -668,6 +670,9 @@ GEM unicorn-rails (2.2.1) rack unicorn + unicorn-worker-killer (0.4.4) + get_process_mem (~> 0) + unicorn (>= 4, < 6) uuidtools (2.1.5) warden (1.2.7) rack (>= 1.0) @@ -792,6 +797,7 @@ DEPENDENCIES uglifier (>= 1.0.3) unicorn unicorn-rails + unicorn-worker-killer web! webdrivers webmock diff --git a/config.ru b/config.ru index 45a4379990..c4c4031568 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,13 @@ # This file is used by Rack-based servers to start the application. +if ENV.fetch('KILL_UNICORNS', false) && ['production', 'staging'].include?(ENV['RAILS_ENV']) + # Gracefully restart individual unicorn workers if they have: + # - performed between 25000 and 30000 requests + # - grown in memory usage to between 700 and 850 MB + require 'unicorn/worker_killer' + use Unicorn::WorkerKiller::MaxRequests, 25_000, 30_000 + use Unicorn::WorkerKiller::Oom, (700 * (1024**2)), (850 * (1024**2)) +end + require ::File.expand_path('../config/environment', __FILE__) run Openfoodnetwork::Application From ba5a56db1464f380eec1a8e01bbccf82d17bb6ce Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sun, 5 Apr 2020 09:31:27 +0200 Subject: [PATCH 2/2] Make upper and lower bounds configurable --- config.ru | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/config.ru b/config.ru index c4c4031568..e2d03deb47 100644 --- a/config.ru +++ b/config.ru @@ -5,8 +5,12 @@ if ENV.fetch('KILL_UNICORNS', false) && ['production', 'staging'].include?(ENV[' # - performed between 25000 and 30000 requests # - grown in memory usage to between 700 and 850 MB require 'unicorn/worker_killer' - use Unicorn::WorkerKiller::MaxRequests, 25_000, 30_000 - use Unicorn::WorkerKiller::Oom, (700 * (1024**2)), (850 * (1024**2)) + use Unicorn::WorkerKiller::MaxRequests, + ENV.fetch('UWK_REQS_MIN', 25_000).to_i, + ENV.fetch('UWK_REQS_MAX', 30_000).to_i + use Unicorn::WorkerKiller::Oom, + ( ENV.fetch('UWK_MEM_MIN', 700).to_i * (1024**2) ), + ( ENV.fetch('UWK_MEM_MAX', 850).to_i * (1024**2) ) end require ::File.expand_path('../config/environment', __FILE__)