Commit 2131e342 authored by Dennis Schubert's avatar Dennis Schubert
Browse files

Merge pull request #6792 from Zauberstuhl/unicorn_killer

Move unicorn_killer to Gemfile
parents fc97b654 56c7af94
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -107,6 +107,7 @@ before.
* Redesign back to top button [#6782](https://github.com/diaspora/diaspora/pull/6782)
* Adjusted Facebook integration for a successful review [#6778](https://github.com/diaspora/diaspora/pull/6778)
* Redirect to the sign-in page instead of the stream on account deletion [#6784](https://github.com/diaspora/diaspora/pull/6784)
* Removed own unicorn killer by a maintained third-party gem [#6792](https://github.com/diaspora/diaspora/pull/6792)

## Bug fixes
* Destroy Participation when removing interactions with a post [#5852](https://github.com/diaspora/diaspora/pull/5852)
+1 −0
Original line number Diff line number Diff line
@@ -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

+5 −0
Original line number Diff line number Diff line
@@ -343,6 +343,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
@@ -849,6 +850,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)
@@ -1021,6 +1025,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)
+5 −2
Original line number Diff line number Diff line
@@ -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

lib/unicorn_killer.rb

deleted100644 → 0
+0 −53
Original line number Diff line number Diff line
# # 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