worker_killer 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d3e59b4a8d832b2bb2e7fa013b615962ba339055320fa194a8358cd37047001
4
- data.tar.gz: b3d7cd5c9f2b236369e326254d1e80ab3dc922d6a6fe9395c90ac9fdfc460245
3
+ metadata.gz: dd68e0bf581f102ddbf11561cbafec2dea05b555f8349d9a86ff24aedf7fa682
4
+ data.tar.gz: 6d7df0e370ec8efc7375b727b99df99e806bdcc3bd28bca342d572a10ea3b80b
5
5
  SHA512:
6
- metadata.gz: e8d2536fd509599eff5b74f97231e456ffe8dc80ea50cb853b03753d5d93dc4d359e9371a83c8dfe3131487ed8fc5d7f21163de1556bed127cd54cf342694ef7
7
- data.tar.gz: a6d69e7e7b37854c4cafddb6a14c4a91c3f3c4a7329882c76e33f5ee19e41133ae1efa4a19ef7f181172cb8a04b8b82c09206211eaf1774bdc1aca3890fa4126
6
+ metadata.gz: 459b5cbc8c683dde9bd4ad995b4d61bfc1b82fb76c6b602ef84aec5dc0ba0dff8e0ac92eeac99c9a1cacff3d30e6e48a62da3f68c0235cd5aa67dff4f8aad926
7
+ data.tar.gz: 516c7d461f44f2ce30ef8d0d02421a1765d8e8a56f510d6f8f7b691c259e92dd85bc0166cfd15a9512c2de53ece760a418841b14d666020085331cfc97016bd7
data/.dockerignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ log/*
3
+ tmp/*
4
+ doc/*
5
+ envs/*
6
+ coverage/*
7
+ Dockerfile*
8
+ docker-compose*
9
+ /node_modules
10
+ config/keys/*
11
+ config/database.yml
12
+ config/secrets.yml
13
+ vendor/cache
14
+ rspec*
15
+
16
+ .gitignore
17
+ .gitlab-ci.yml
18
+
19
+
20
+
data/.gitignore ADDED
@@ -0,0 +1,124 @@
1
+ #----------------------------------------------------------------------------
2
+ # Ignore these files when commiting to a git repository.
3
+ #
4
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
5
+ #
6
+ # The original version of this file is found here:
7
+ # https://github.com/RailsApps/rails-composer/blob/master/files/gitignore.txt
8
+ #
9
+ # Corrections? Improvements? Create a GitHub issue:
10
+ # http://github.com/RailsApps/rails-composer/issues
11
+ #----------------------------------------------------------------------------
12
+
13
+ # bundler state
14
+ /.bundle
15
+ /vendor/bundle/
16
+ /vendor/ruby/
17
+ vendor/cache
18
+
19
+ # minimal Rails specific artifacts
20
+ db/*.sqlite3
21
+ db/schema.rb
22
+ /db/*.sqlite3-journal
23
+ /log/*
24
+ /tmp/*
25
+ /swagger/*
26
+
27
+
28
+ # add /config/database.yml if it contains passwords
29
+ # /config/database.yml
30
+
31
+ # various artifacts
32
+ **.war
33
+ *.rbc
34
+ *.sassc
35
+ .redcar/
36
+ .sass-cache
37
+ /config/config.yml
38
+ /coverage.data
39
+ /coverage/
40
+ /db/*.javadb/
41
+ /db/*.sqlite3
42
+ config/database.yml
43
+ config/secrets.yml
44
+ config/app.yml
45
+ config/cable.yml
46
+ config/bunny.yml
47
+ oxymoron.js
48
+ /doc/api/
49
+ /doc/app/
50
+ /doc/features.html
51
+ /doc/specs.html
52
+ /public/cache
53
+ /public/uploads
54
+ /public/stylesheets/compiled
55
+ /public/system/*
56
+ /spec/tmp/*
57
+ /cache
58
+ /capybara*
59
+ /capybara-*.html
60
+ /gems
61
+ /specifications
62
+ rerun.txt
63
+ pickle-email-*.html
64
+ *.sublime-project
65
+ *.sublime-workspace
66
+ .zeus.sock
67
+ .byebug_history
68
+
69
+ #test
70
+ /docs
71
+
72
+
73
+ # If you find yourself ignoring temporary files generated by your text editor
74
+ # or operating system, you probably want to add a global ignore instead:
75
+ # git config --global core.excludesfile ~/.gitignore_global
76
+ #
77
+ # Here are some files you may want to ignore globally:
78
+
79
+ # scm revert files
80
+ **.orig
81
+
82
+ # Mac finder artifacts
83
+ .DS_Store
84
+
85
+ # Netbeans project directory
86
+ /nbproject/
87
+
88
+ # RubyMine project files
89
+ .idea
90
+
91
+ # VsCode project files
92
+ .vscode
93
+ *.code-workspace
94
+
95
+ # Textmate project files
96
+ /*.tmproj
97
+
98
+ # vim artifacts
99
+ **.swp
100
+
101
+ # Environment files that may contain sensitive data
102
+ .powenv
103
+
104
+ # tilde files are usually backup files from a text editor
105
+ *~
106
+ public/assets/
107
+ config/keys
108
+ public/.revision
109
+ storage/*
110
+ spec/reports/
111
+
112
+ erd-schema.*
113
+ .ruby-gemset
114
+ .ruby-version
115
+ passenger.*.pid.lock
116
+ *.sublime-project
117
+ *.sublime-workspace
118
+
119
+ /public/packs
120
+ /public/packs-test
121
+ /node_modules
122
+ /yarn-error.log
123
+ yarn-debug.log*
124
+ .yarn-integrity
data/.rubocop.yml ADDED
@@ -0,0 +1,133 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - "storage/**/*"
5
+
6
+ Layout/TrailingBlankLines:
7
+ Enabled: true
8
+ AutoCorrect: true
9
+ EnforcedStyle: final_blank_line
10
+
11
+ #Layout/IndentationWidth:
12
+ # Enabled: true
13
+
14
+ #Layout/IndentAssignment:
15
+ # Enabled: false
16
+ # IndentationWidth: false
17
+
18
+ #Layout/ElseAlignment:
19
+ # Enabled: false
20
+
21
+ Layout/MultilineOperationIndentation:
22
+ EnforcedStyle: indented
23
+
24
+ Layout/MultilineAssignmentLayout:
25
+ EnforcedStyle: new_line
26
+
27
+ Layout/RescueEnsureAlignment:
28
+ Enabled: true
29
+
30
+ Layout/EndAlignment:
31
+ Enabled: true
32
+ EnforcedStyleAlignWith: variable
33
+ AutoCorrect: true
34
+
35
+ Layout/AlignHash:
36
+ EnforcedColonStyle: table
37
+ EnforcedHashRocketStyle: table
38
+
39
+ Layout/IndentationConsistency:
40
+ EnforcedStyle: indented_internal_methods
41
+
42
+ Layout/EmptyLines:
43
+ Enabled: false
44
+
45
+ Layout/EmptyLinesAroundClassBody:
46
+ EnforcedStyle: empty_lines_except_namespace
47
+
48
+ Layout/EmptyLinesAroundModuleBody:
49
+ EnforcedStyle: empty_lines_except_namespace
50
+
51
+ Layout/SpaceInsideBlockBraces:
52
+ EnforcedStyle: space
53
+ SpaceBeforeBlockParameters: false
54
+
55
+ Layout/SpaceAroundBlockParameters:
56
+ EnforcedStyleInsidePipes: no_space
57
+
58
+ Layout/SpaceBeforeBlockBraces:
59
+ Enabled: false
60
+
61
+ #Lint/AssignmentInCondition:
62
+ # Enabled: false
63
+
64
+ Metrics/BlockLength:
65
+ Exclude:
66
+ - "spec/**/*.rb"
67
+
68
+ Metrics/LineLength:
69
+ Max: 100
70
+ IgnoredPatterns: ['(\A|\s)#']
71
+
72
+ Metrics/AbcSize:
73
+ Max: 20
74
+
75
+ Metrics/MethodLength:
76
+ Max: 20
77
+
78
+ Security/YAMLLoad:
79
+ Enabled: false
80
+
81
+ Style/AsciiComments:
82
+ Enabled: false
83
+
84
+ Style/RedundantBegin:
85
+ Enabled: false
86
+
87
+ Style/GlobalVars:
88
+ AllowedVariables: ["$logger", "$root"]
89
+
90
+ Style/ClassAndModuleChildren:
91
+ EnforcedStyle: compact
92
+ Enabled: false
93
+
94
+ Style/Documentation:
95
+ Enabled: false
96
+
97
+ Style/Lambda:
98
+ Enabled: false
99
+
100
+ Style/RedundantSelf:
101
+ Enabled: false
102
+
103
+ Style/RaiseArgs:
104
+ EnforcedStyle: compact
105
+
106
+ Style/SpecialGlobalVars:
107
+ Enabled: false
108
+
109
+ Style/BracesAroundHashParameters:
110
+ EnforcedStyle: context_dependent
111
+
112
+ Style/NumericPredicate:
113
+ Enabled: false
114
+
115
+ Style/FrozenStringLiteralComment:
116
+ Enabled: false
117
+
118
+ Style/DoubleNegation:
119
+ Enabled: false
120
+
121
+ Style/SymbolArray:
122
+ Enabled: false
123
+
124
+ Style/RescueModifier:
125
+ Enabled: false
126
+
127
+ Style/TrailingCommaInArrayLiteral:
128
+ Enabled: true
129
+ AutoCorrect: true
130
+
131
+ Style/TrailingCommaInArguments:
132
+ Enabled: true
133
+ AutoCorrect: true
data/AUTHORS ADDED
@@ -0,0 +1 @@
1
+ Samoilenko Yuri <kinnalru _at_ gmail.com>
data/ChangeLog ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014-2019 Рнд Софт
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
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/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.4
@@ -0,0 +1,19 @@
1
+ require 'logger'
2
+
3
+ module WorkerKiller
4
+ # Methods for configuring WorkerKiller
5
+ class Configuration
6
+
7
+ attr_accessor :logger, :quit_attempts, :kill_attempts, :use_quit
8
+
9
+ # Override defaults for configuration
10
+ def initialize(quit_attempts: 5, kill_attempts: 10, use_quit: true)
11
+ @quit_attempts = quit_attempts
12
+ @kill_attempts = kill_attempts
13
+ @use_quit = use_quit
14
+ @logger = Logger.new(STDOUT, level: Logger::INFO, progname: 'WorkerKiller')
15
+ end
16
+
17
+ end
18
+ end
19
+
@@ -0,0 +1,38 @@
1
+ module WorkerKiller
2
+ class CountLimiter
3
+
4
+ attr_reader :min, :max, :limit, :started_at
5
+
6
+ def initialize(min: 3072, max: 4096, verbose: false, &block)
7
+ @min = min
8
+ @max = max
9
+ @limit = @min + WorkerKiller.randomize(@max - @min + 1)
10
+ @left = @limit
11
+ @verbose = verbose
12
+ @reaction = block
13
+ end
14
+
15
+ def check
16
+ return nil if @limit <= 1
17
+
18
+ @started_at ||= Time.now
19
+
20
+ if @verbose
21
+ logger.info "#{self}: worker (pid: #{Process.pid}) has #{@left} left before being limited"
22
+ end
23
+
24
+ return false if (@left -= 1) > 0
25
+
26
+ logger.warn "#{self}: worker (pid: #{Process.pid}) exceeds max number of requests (limit: #{@limit})"
27
+ @reaction.call(self)
28
+
29
+ true
30
+ end
31
+
32
+ def logger
33
+ WorkerKiller.configuration.logger
34
+ end
35
+
36
+ end
37
+ end
38
+
@@ -0,0 +1,46 @@
1
+ require 'get_process_mem'
2
+
3
+ module WorkerKiller
4
+ class MemoryLimiter
5
+
6
+ attr_reader :min, :max, :limit, :started_at, :check_cycle
7
+
8
+ def initialize(min: (1024**3), max: (2 * (1024**3)), check_cycle: 16, verbose: false, &block)
9
+ @min = min
10
+ @max = max
11
+ @limit = @min + WorkerKiller.randomize(@max - @min + 1)
12
+ @check_cycle = check_cycle
13
+ @check_count = 0
14
+ @verbose = verbose
15
+ @reaction = block
16
+ end
17
+
18
+ def check
19
+ return nil if @limit <= 1024**2
20
+
21
+ @started_at ||= Time.now
22
+ @check_count += 1
23
+
24
+ return false if (@check_count % @check_cycle) != 0
25
+
26
+ rss = GetProcessMem.new.bytes
27
+ if @verbose
28
+ logger.info "#{self}: worker (pid: #{Process.pid}) using #{rss} bytes(#{rss / 1024 / 1024}mb)."
29
+ end
30
+ @check_count = 0
31
+
32
+ return false if rss <= @limit
33
+
34
+ logger.warn "#{self}: worker (pid: #{Process.pid}) exceeds memory limit (#{rss} bytes > #{@limit} bytes)"
35
+ @reaction.call(self)
36
+
37
+ true
38
+ end
39
+
40
+ def logger
41
+ WorkerKiller.configuration.logger
42
+ end
43
+
44
+ end
45
+ end
46
+
@@ -0,0 +1,43 @@
1
+ require 'worker_killer/memory_limiter'
2
+ require 'worker_killer/count_limiter'
3
+
4
+ module WorkerKiller
5
+ class Middleware
6
+
7
+ def initialize(app, klass:, reaction: nil, **opts)
8
+ @app = app
9
+
10
+ reaction ||= proc do |limiter|
11
+ WorkerKiller.kill_self(limiter.logger, limiter.started_at)
12
+ end
13
+
14
+ @limiter = klass.new(opts) do |limiter|
15
+ reaction.call(limiter)
16
+ end
17
+ end
18
+
19
+ def call(env)
20
+ response = @app.call(env)
21
+ @limiter.check
22
+ response
23
+ end
24
+
25
+ class RequestsLimiter < ::WorkerKiller::Middleware
26
+
27
+ def initialize(app, **opts)
28
+ super(app, klass: ::WorkerKiller::CountLimiter, **opts)
29
+ end
30
+
31
+ end
32
+
33
+ class OOMLimiter < ::WorkerKiller::Middleware
34
+
35
+ def initialize(app, **opts)
36
+ super(app, klass: ::WorkerKiller::MemoryLimiter, **opts)
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+ end
43
+
@@ -0,0 +1,50 @@
1
+ require 'worker_killer/configuration'
2
+ require 'worker_killer/count_limiter'
3
+ require 'worker_killer/memory_limiter'
4
+ require 'worker_killer/middleware'
5
+
6
+ module WorkerKiller
7
+
8
+ class << self
9
+
10
+ attr_accessor :configuration
11
+
12
+ end
13
+
14
+ def self.configure
15
+ self.configuration ||= WorkerKiller::Configuration.new
16
+ yield(configuration) if block_given?
17
+ end
18
+
19
+ self.configure
20
+
21
+ def self.randomize(integer)
22
+ RUBY_VERSION > '1.9' ? Random.rand(integer.abs) : rand(integer)
23
+ end
24
+
25
+ # Kill the current process by telling it to send signals to itself. If
26
+ # the process isn't killed after `configuration.quit_attempts` QUIT signals,
27
+ # send TERM signals until `configuration.kill_attempts`. Finally, send a KILL
28
+ # signal. A single signal is sent per request.
29
+ def self.kill_self(logger, start_time)
30
+ alive_sec = (Time.now - start_time).round
31
+ self_pid = Process.pid
32
+
33
+ @kill_attempts ||= 0
34
+ @kill_attempts += 1
35
+
36
+ if configuration.use_quit
37
+ sig = :QUIT
38
+ sig = :TERM if @kill_attempts > configuration.quit_attempts
39
+ else
40
+ sig = :TERM
41
+ end
42
+
43
+ sig = :KILL if @kill_attempts > configuration.kill_attempts
44
+
45
+ logger.warn "#{self} send SIG#{sig} (pid: #{self_pid}) alive: #{alive_sec} sec (trial #{@kill_attempts})"
46
+ Process.kill sig, self_pid
47
+ end
48
+
49
+ end
50
+
@@ -0,0 +1,24 @@
1
+ $:.push File.expand_path('lib', __dir__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'worker_killer'
5
+ gem.description = 'Kill any workers by memory and request counts or take custom reaction'
6
+ gem.homepage = 'https://github.com/RnD-Soft/worker_killer'
7
+ gem.summary = gem.description
8
+ gem.version = File.read('VERSION').strip
9
+ gem.authors = ['Samoilenko Yuri']
10
+ gem.email = ['kinnalru@gmail.com']
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f) }
14
+ gem.license = 'MIT'
15
+ gem.require_paths = ['lib']
16
+
17
+ gem.add_dependency 'get_process_mem'
18
+
19
+ gem.add_development_dependency 'rspec'
20
+ gem.add_development_dependency 'rspec_junit_formatter'
21
+ gem.add_development_dependency 'simplecov'
22
+ gem.add_development_dependency 'simplecov-console'
23
+ end
24
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: worker_killer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samoilenko Yuri
@@ -86,7 +86,22 @@ email:
86
86
  executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
- files: []
89
+ files:
90
+ - ".dockerignore"
91
+ - ".gitignore"
92
+ - ".rubocop.yml"
93
+ - AUTHORS
94
+ - ChangeLog
95
+ - Gemfile
96
+ - LICENSE
97
+ - README.md
98
+ - VERSION
99
+ - lib/worker_killer.rb
100
+ - lib/worker_killer/configuration.rb
101
+ - lib/worker_killer/count_limiter.rb
102
+ - lib/worker_killer/memory_limiter.rb
103
+ - lib/worker_killer/middleware.rb
104
+ - worker_killer.gemspec
90
105
  homepage: https://github.com/RnD-Soft/worker_killer
91
106
  licenses:
92
107
  - MIT
@@ -106,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
121
  - !ruby/object:Gem::Version
107
122
  version: '0'
108
123
  requirements: []
109
- rubygems_version: 3.0.3
124
+ rubygems_version: 3.0.6
110
125
  signing_key:
111
126
  specification_version: 4
112
127
  summary: Kill any workers by memory and request counts or take custom reaction