unicorn-worker-killer-fork-for-newer-unicorn 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 62e7ae660fb9a5c3cc623ceee3c4bbb72fac9d848378d3ef0f95cc891e4038ca
4
+ data.tar.gz: cc239bfa14ed1fecde5cf0a489e7d190e95a36b5572557191ef8dcdcb79bc8e7
5
+ SHA512:
6
+ metadata.gz: a64ea2d0eb718fcf54d0a433271bed669280e7ca317814f570a2450937a53d1a951f38ad3023f05612d506804df7ab0e91b00e1832b6a4c109c1afb5807b97c8
7
+ data.tar.gz: db9f5bfa157c5fd9d40538819f04f4a63d74c90cc16bb5463026624cd2e4ede353e0871b3cfce975deeb5e929a06d2db3ac2a0ba4e339f88ffa33bba62d01595
data/AUTHORS ADDED
@@ -0,0 +1,3 @@
1
+ Kazuki Ohta <kazuki.ohta _at_ gmail.com>
2
+ Sadayuki FURUHASHI <frsyuki _at_ gmail.com>
3
+ Jonathan Clem <jonathan _at_ jclem.net>
data/ChangeLog ADDED
@@ -0,0 +1,56 @@
1
+ Release 0.4.5 - 2021/04/13
2
+ * update for Unicorn 6
3
+
4
+ Release 0.4.4 - 2015/11/13
5
+ * update for Unicorn 5
6
+
7
+ Release 0.4.3 - 2013/12/23
8
+ * fix for occasional 'invalid argument' error
9
+
10
+ Release 0.4.2 - 2013/09/23
11
+ * `verbose` options provides verbose logging
12
+
13
+ Release 0.4.1 - 2013/03/12
14
+ * Kill attempts happen once per request, not in a loop
15
+
16
+ Release 0.4.0 - 2013/03/12
17
+ * Send signals from inside a Thread (required for QUIT signal)
18
+ * Fix broken if/else in kill_self
19
+
20
+ Release 0.3.6 - 2013/03/09
21
+ * do not require configuration
22
+ * fix missing randomize() in MaxRequests killer
23
+
24
+ Release 0.3.5 - 2013/03/08
25
+ * configurable max QUIT, TERM, and sleep interval
26
+ * fix warning message of signal handling
27
+
28
+ Release 0.3.4 - 2013/03/08
29
+ * try SIGQUIT first, rather than SIGTERM
30
+
31
+ Release 0.3.3 - 2013/02/28
32
+ * fix for ruby 1.8
33
+
34
+ Release 0.3.2 - 2013/02/03
35
+ * add an option to disable the check for both MaxRequests and MaxMemory
36
+
37
+ Release 0.3.1 - 2013/01/13
38
+ * fix for ruby 1.9.2
39
+
40
+ Release 0.3.0 - 2012/12/31
41
+ * fix for 'preload_app true'
42
+
43
+ Release 0.2.1 - 2012/12/10
44
+ * fix critical namespace error, introduced at the previous version
45
+
46
+ Release 0.2.0 - 2012/11/22
47
+ * change namespace to Unicorn::WorkerKiller from UnicornWorkerKiller
48
+
49
+ Release 0.1.2 - 2012/11/17
50
+ * change unicorn version dependency to '~> 4'
51
+
52
+ Release 0.1.1 - 2012/11/17
53
+ * fix homepage
54
+
55
+ Release 0.1 - 2012/11/17
56
+ * first release
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,64 @@
1
+ Unicorn is copyrighted free software by all contributors, see logs in
2
+ revision control for names and email addresses of all of them.
3
+
4
+ You can redistribute it and/or modify it under either the terms of the
5
+ GNU General Public License (GPL) as published by the Free Software
6
+ Foundation (FSF), version {3.0}[http://www.gnu.org/licenses/gpl-3.0.txt]
7
+ or version {2.0}[http://www.gnu.org/licenses/gpl-2.0.txt]
8
+ or the Ruby-specific license terms (see below).
9
+
10
+ The unicorn project leader (Eric Wong) reserves the right to add future
11
+ versions of the GPL (and no other licenses) as published by the FSF to
12
+ the licensing terms.
13
+
14
+ === Ruby-specific terms (if you're not using the GPLv2 or GPLv3)
15
+
16
+ 1. You may make and give away verbatim copies of the source form of the
17
+ software without restriction, provided that you duplicate all of the
18
+ original copyright notices and associated disclaimers.
19
+
20
+ 2. You may modify your copy of the software in any way, provided that
21
+ you do at least ONE of the following:
22
+
23
+ a) place your modifications in the Public Domain or otherwise make them
24
+ Freely Available, such as by posting said modifications to Usenet or an
25
+ equivalent medium, or by allowing the author to include your
26
+ modifications in the software.
27
+
28
+ b) use the modified software only within your corporation or
29
+ organization.
30
+
31
+ c) rename any non-standard executables so the names do not conflict with
32
+ standard executables, which must also be provided.
33
+
34
+ d) make other distribution arrangements with the author.
35
+
36
+ 3. You may distribute the software in object code or executable
37
+ form, provided that you do at least ONE of the following:
38
+
39
+ a) distribute the executables and library files of the software,
40
+ together with instructions (in the manual page or equivalent) on where
41
+ to get the original distribution.
42
+
43
+ b) accompany the distribution with the machine-readable source of the
44
+ software.
45
+
46
+ c) give non-standard executables non-standard names, with
47
+ instructions on where to get the original software distribution.
48
+
49
+ d) make other distribution arrangements with the author.
50
+
51
+ 4. You may modify and include the part of the software into any other
52
+ software (possibly commercial). But some files in the distribution
53
+ are not written by the author, so that they are not under this terms.
54
+
55
+ 5. The scripts and library files supplied as input to or produced as
56
+ output from the software do not automatically fall under the
57
+ copyright of the software, but belong to whomever generated them,
58
+ and may be sold commercially, and may be aggregated with this
59
+ software.
60
+
61
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
62
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
63
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
64
+ PURPOSE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # unicorn-worker-killer
2
+
3
+ [Unicorn](http://unicorn.bogomips.org/) is widely used HTTP-server for Rack applications. One thing we thought Unicorn missed, is killing the Unicorn workers based on the number of requests and consumed memories.
4
+
5
+ `unicorn-worker-killer` gem provides automatic restart of Unicorn workers based on 1) max number of requests, and 2) process memory size (RSS), without affecting any requests. This will greatly improves site's stability by avoiding unexpected memory exhaustion at the application nodes.
6
+
7
+ # Install
8
+
9
+ No external process like `god` is required. Just install one gem: `unicorn-worker-killer`.
10
+
11
+ gem 'unicorn-worker-killer'
12
+
13
+ # Usage
14
+
15
+ Add these lines to your `config.ru`. (These lines should be added above the `require ::File.expand_path('../config/environment', __FILE__)` line.
16
+
17
+ # Unicorn self-process killer
18
+ require 'unicorn/worker_killer'
19
+
20
+ # Max requests per worker
21
+ use Unicorn::WorkerKiller::MaxRequests, 3072, 4096
22
+
23
+ # Max memory size (RSS) per worker
24
+ use Unicorn::WorkerKiller::Oom, (192*(1024**2)), (256*(1024**2))
25
+
26
+ This gem provides two modules.
27
+
28
+ ### `Unicorn::WorkerKiller::MaxRequests(max_requests_min=3072, max_requests_max=4096, verbose=false)`
29
+
30
+ This module automatically restarts the Unicorn workers, based on the number of requests which worker processed.
31
+
32
+ `max_requests_min` and `max_requests_max` specify the min and max of maximum requests per worker. The actual limit is decided by rand() between `max_requests_min` and `max_requests_max` per worker, to prevent all workers to be dead at the same time. Once the number exceeds the limit, that worker is automatically restarted.
33
+
34
+ If `verbose` is set to true, then after every request, your log will show the requests left before restart. This logging is done at the `info` level.
35
+
36
+ ### `Unicorn::WorkerKiller::Oom(memory_limit_min=(1024\*\*3), memory_limit_max=(2\*(1024\*\*3)), check_cycle = 16, verbose = false)`
37
+
38
+ This module automatically restarts the Unicorn workers, based on its memory size.
39
+
40
+ `memory_limit_min` and `memory_limit_max` specify the min and max of maximum memory in bytes per worker. The actual limit is decided by rand() between `memory_limit_min` and `memory_limit_max` per worker, to prevent all workers to be dead at the same time. Once the memory size exceeds `memory_size`, that worker is automatically restarted.
41
+
42
+ The memory size check is done in every `check_cycle` requests.
43
+
44
+ If `verbose` is set to true, then every memory size check will be shown in your logs. This logging is done at the `info` level.
45
+
46
+ # Special Thanks
47
+
48
+ - [@hotchpotch](http://github.com/hotchpotch/) for the [original idea](https://gist.github.com/hotchpotch/1258681)
49
+
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ task :default => [:build]
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.5
@@ -0,0 +1,11 @@
1
+ module Unicorn::WorkerKiller
2
+ class Configuration
3
+ attr_accessor :max_quit, :max_term, :sleep_interval
4
+
5
+ def initialize
6
+ self.max_quit = 10
7
+ self.max_term = 15
8
+ self.sleep_interval = 1
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,114 @@
1
+ require 'unicorn'
2
+ require 'unicorn/worker_killer/configuration'
3
+ require 'get_process_mem'
4
+
5
+ module Unicorn::WorkerKiller
6
+ class << self
7
+ attr_accessor :configuration
8
+ end
9
+
10
+ # Kill the current process by telling it to send signals to itself. If
11
+ # the process isn't killed after `configuration.max_quit` QUIT signals,
12
+ # send TERM signals until `configuration.max_term`. Finally, send a KILL
13
+ # signal. A single signal is sent per request.
14
+ # @see http://unicorn.bogomips.org/SIGNALS.html
15
+ def self.kill_self(logger, start_time)
16
+ alive_sec = (Time.now - start_time).round
17
+ worker_pid = Process.pid
18
+
19
+ @@kill_attempts ||= 0
20
+ @@kill_attempts += 1
21
+
22
+ sig = :QUIT
23
+ sig = :TERM if @@kill_attempts > configuration.max_quit
24
+ sig = :KILL if @@kill_attempts > configuration.max_term
25
+
26
+ logger.warn "#{self} send SIG#{sig} (pid: #{worker_pid}) alive: #{alive_sec} sec (trial #{@@kill_attempts})"
27
+ Process.kill sig, worker_pid
28
+ end
29
+
30
+ module Oom
31
+ # Killing the process must be occurred at the outside of the request. We're
32
+ # using similar techniques used by OobGC, to ensure actual killing doesn't
33
+ # affect the request.
34
+ #
35
+ # @see https://github.com/defunkt/unicorn/blob/master/lib/unicorn/oob_gc.rb#L40
36
+ def self.new(app, memory_limit_min = (1024**3), memory_limit_max = (2*(1024**3)), check_cycle = 16, verbose = false)
37
+ ObjectSpace.each_object(Unicorn::HttpServer) do |s|
38
+ s.extend(self)
39
+ s.instance_variable_set(:@_worker_memory_limit_min, memory_limit_min)
40
+ s.instance_variable_set(:@_worker_memory_limit_max, memory_limit_max)
41
+ s.instance_variable_set(:@_worker_check_cycle, check_cycle)
42
+ s.instance_variable_set(:@_worker_check_count, 0)
43
+ s.instance_variable_set(:@_verbose, verbose)
44
+ end
45
+ app # pretend to be Rack middleware since it was in the past
46
+ end
47
+
48
+ def randomize(integer)
49
+ RUBY_VERSION > "1.9" ? Random.rand(integer.abs) : rand(integer)
50
+ end
51
+
52
+ def process_client(*client)
53
+ super(*client) # Unicorn::HttpServer#process_client
54
+ return if @_worker_memory_limit_min == 0 && @_worker_memory_limit_max == 0
55
+
56
+ @_worker_process_start ||= Time.now
57
+ @_worker_memory_limit ||= @_worker_memory_limit_min + randomize(@_worker_memory_limit_max - @_worker_memory_limit_min + 1)
58
+ @_worker_check_count += 1
59
+ if @_worker_check_count % @_worker_check_cycle == 0
60
+ rss = GetProcessMem.new.bytes
61
+ logger.info "#{self}: worker (pid: #{Process.pid}) using #{rss} bytes." if @_verbose
62
+ if rss > @_worker_memory_limit
63
+ logger.warn "#{self}: worker (pid: #{Process.pid}) exceeds memory limit (#{rss} bytes > #{@_worker_memory_limit} bytes)"
64
+ Unicorn::WorkerKiller.kill_self(logger, @_worker_process_start)
65
+ end
66
+ @_worker_check_count = 0
67
+ end
68
+ end
69
+ end
70
+
71
+ module MaxRequests
72
+ # Killing the process must be occurred at the outside of the request. We're
73
+ # using similar techniques used by OobGC, to ensure actual killing doesn't
74
+ # affect the request.
75
+ #
76
+ # @see https://github.com/defunkt/unicorn/blob/master/lib/unicorn/oob_gc.rb#L40
77
+ def self.new(app, max_requests_min = 3072, max_requests_max = 4096, verbose = false)
78
+ ObjectSpace.each_object(Unicorn::HttpServer) do |s|
79
+ s.extend(self)
80
+ s.instance_variable_set(:@_worker_max_requests_min, max_requests_min)
81
+ s.instance_variable_set(:@_worker_max_requests_max, max_requests_max)
82
+ s.instance_variable_set(:@_verbose, verbose)
83
+ end
84
+
85
+ app # pretend to be Rack middleware since it was in the past
86
+ end
87
+
88
+ def randomize(integer)
89
+ RUBY_VERSION > "1.9" ? Random.rand(integer.abs) : rand(integer)
90
+ end
91
+
92
+ def process_client(*client)
93
+ super(*client) # Unicorn::HttpServer#process_client
94
+ return if @_worker_max_requests_min == 0 && @_worker_max_requests_max == 0
95
+
96
+ @_worker_process_start ||= Time.now
97
+ @_worker_cur_requests ||= @_worker_max_requests_min + randomize(@_worker_max_requests_max - @_worker_max_requests_min + 1)
98
+ @_worker_max_requests ||= @_worker_cur_requests
99
+ logger.info "#{self}: worker (pid: #{Process.pid}) has #{@_worker_cur_requests} left before being killed" if @_verbose
100
+
101
+ if (@_worker_cur_requests -= 1) <= 0
102
+ logger.warn "#{self}: worker (pid: #{Process.pid}) exceeds max number of requests (limit: #{@_worker_max_requests})"
103
+ Unicorn::WorkerKiller.kill_self(logger, @_worker_process_start)
104
+ end
105
+ end
106
+ end
107
+
108
+ def self.configure
109
+ self.configuration ||= Configuration.new
110
+ yield(configuration) if block_given?
111
+ end
112
+
113
+ self.configure
114
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "unicorn-worker-killer-fork-for-newer-unicorn"
6
+ gem.description = "Kill unicorn workers by memory and request counts"
7
+ gem.homepage = "https://github.com/jrupinski/unicorn-worker-killer"
8
+ gem.summary = gem.description
9
+ gem.version = File.read("VERSION").strip
10
+ gem.authors = ["Kazuki Ohta", "Sadayuki Furuhashi", "Jonathan Clem"]
11
+ gem.email = ["kazuki.ohta@gmail.com", "frsyuki@gmail.com", "jonathan@jclem.net"]
12
+ #gem.platform = Gem::Platform::RUBY
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ gem.licenses = ["GPLv2+", "Ruby 1.8"]
17
+ gem.require_paths = ['lib']
18
+ gem.add_dependency "unicorn", [">= 4", "< 7"]
19
+ gem.add_dependency "get_process_mem", "~> 0"
20
+ gem.add_development_dependency "rake", ">= 0.9.2"
21
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unicorn-worker-killer-fork-for-newer-unicorn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.5
5
+ platform: ruby
6
+ authors:
7
+ - Kazuki Ohta
8
+ - Sadayuki Furuhashi
9
+ - Jonathan Clem
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 1980-01-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: unicorn
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '4'
21
+ - - "<"
22
+ - !ruby/object:Gem::Version
23
+ version: '7'
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: '4'
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '7'
34
+ - !ruby/object:Gem::Dependency
35
+ name: get_process_mem
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.2
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.2
62
+ description: Kill unicorn workers by memory and request counts
63
+ email:
64
+ - kazuki.ohta@gmail.com
65
+ - frsyuki@gmail.com
66
+ - jonathan@jclem.net
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - AUTHORS
72
+ - ChangeLog
73
+ - Gemfile
74
+ - LICENSE
75
+ - README.md
76
+ - Rakefile
77
+ - VERSION
78
+ - lib/unicorn/worker_killer.rb
79
+ - lib/unicorn/worker_killer/configuration.rb
80
+ - unicorn-worker-killer.gemspec
81
+ homepage: https://github.com/jrupinski/unicorn-worker-killer
82
+ licenses:
83
+ - GPLv2+
84
+ - Ruby 1.8
85
+ metadata: {}
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.6.8
101
+ specification_version: 4
102
+ summary: Kill unicorn workers by memory and request counts
103
+ test_files: []