noxa-sidekiq-killer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 494a438b2f6ab1d1a8ac903bad39a70ed7f2339b
4
+ data.tar.gz: 23609db323ec8ec7ec0cebefacd12a8a227a5f45
5
+ SHA512:
6
+ metadata.gz: 5f1f4848bbc27686e011b8a1dc2fef29408fd454da4a3d7ef19f1cb53cf0b7edb3b0d0276d31c9e11a8b146af8a776d692457dc35f0af2adcba08d4ea6cfd48d
7
+ data.tar.gz: 4146234f006ff56e59ffc490ad07ae5906fe960ea7e5a9b99a64038f77a7758bdb910289ca9530a29da38407108c87083d36405f5dad0ed6c64ec8ff521ef271
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at k.hammer@youngcapital.nl. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sidekiq-killer.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Karst Hammer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ # Sidekiq::Killer
2
+
3
+ Kills Sidekiq processes when the RSS is too high.
4
+
5
+ It checks the memory usage of sidekiq after each job, and if the memory usage is
6
+ higher than specified, it will wait for the specified grace_time before quieting
7
+ the sidekiq process. After the shutdown_wait time it will forcibly kill sidekiq
8
+ using the signal specified in shutdown_signal.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'noxa-sidekiq-killer'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install noxa-sidekiq-killer
25
+
26
+ ## Usage
27
+
28
+ Add the following to your Sidekiq configuration:
29
+
30
+ ```
31
+ Sidekiq.configure_server do |config|
32
+ config.server_middleware do |chain|
33
+ chain.add Sidekiq::Killer::Memory, max_rss: 250, grace_time: 0, shutdown_wait: (15 * 60), shutdown_signal: 'SIGKILL'
34
+ end
35
+ end
36
+ ```
37
+
38
+ ## Configuration options
39
+
40
+ | Setting | Description |
41
+ | --------------- | ------------------------------------------------------------------------------------------- |
42
+ | max_rss | Max RSS (in megabytes) |
43
+ | grace_time | Time to wait before quieting sidekiq (in seconds) |
44
+ | shutdown_wait | Time to wait after quieting to finish off jobs before forcibly killing sidekiq (in seconds) |
45
+ | shutdown_signal | Signal to use to shutdown sidekiq |
46
+
47
+ ## Acknowledgements
48
+
49
+ This killer is based on the code from GitLab (https://gitlab.com/gitlab-org/gitlab-ce/).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sidekiq/killer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ require 'sidekiq/killer'
@@ -0,0 +1,7 @@
1
+ require "sidekiq/killer/version"
2
+
3
+ module Sidekiq
4
+ module Killer
5
+ require "sidekiq/killer/memory"
6
+ end
7
+ end
@@ -0,0 +1,53 @@
1
+ require 'get_process_mem'
2
+
3
+ module Sidekiq
4
+ module Killer
5
+ class Memory
6
+ def initialize(options = {})
7
+ options.stringify_keys!
8
+
9
+ @max_rss = (options["max_rss"] || 0).to_s.to_i
10
+ @grace_time = (options["grace_time"] || 15 * 60).to_s.to_i
11
+ @shutdown_wait = (options["shutdown_wait"] || 30).to_s.to_i
12
+ @shutdown_signal = (options["shutdown_signal"] || "SIGKILL").to_s
13
+ end
14
+
15
+ # Create a mutex used to ensure there will be only one thread waiting to
16
+ # shut Sidekiq down
17
+ MUTEX = Mutex.new
18
+
19
+ def call(worker, job, queue)
20
+ yield
21
+ current_rss = get_rss
22
+
23
+ return unless @max_rss > 0 && current_rss > @max_rss
24
+
25
+ Thread.new do
26
+ # Return if another thread is already waiting to shut Sidekiq down
27
+ return unless MUTEX.try_lock
28
+
29
+ Sidekiq.logger.warn "current RSS #{current_rss} exceeds maximum RSS "\
30
+ "#{@max_rss}"
31
+ Sidekiq.logger.warn "this thread will shut down PID #{::Process.pid} - Worker #{worker.class} - JID-#{job['jid']}"\
32
+ "in #{@grace_time} seconds"
33
+ sleep(@grace_time)
34
+
35
+ Sidekiq.logger.warn "sending SIGTERM to PID #{::Process.pid} - Worker #{worker.class} - JID-#{job['jid']}"
36
+ ::Process.kill("SIGTERM", ::Process.pid)
37
+
38
+ Sidekiq.logger.warn "waiting #{@shutdown_wait} seconds before sending "\
39
+ "#{@shutdown_signal} to PID #{::Process.pid} - Worker #{worker.class} - JID-#{job['jid']}"
40
+ sleep(@shutdown_wait)
41
+
42
+ Sidekiq.logger.warn "sending #{@shutdown_signal} to PID #{::Process.pid} - Worker #{worker.class} - JID-#{job['jid']}"
43
+ ::Process.kill(@shutdown_signal, ::Process.pid)
44
+ end
45
+ end
46
+
47
+ private
48
+ def get_rss
49
+ GetProcessMem.new.mb
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module Killer
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "sidekiq/killer/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "noxa-sidekiq-killer"
8
+ spec.version = Sidekiq::Killer::VERSION
9
+ spec.authors = ["Karst Hammer"]
10
+ spec.email = ["k.hammer@youngcapital.nl"]
11
+
12
+ spec.summary = %q{Quiets and kills sidekiq when memory usage is too high}
13
+ spec.description = %q{Quiets and kills sidekiq processes when the RSS memory usage is too high.}
14
+ spec.homepage = "https://github.com/Noxa/sidekiq-killer"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+
27
+ spec.add_dependency "get_process_mem"
28
+ spec.add_dependency "sidekiq"
29
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: noxa-sidekiq-killer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Karst Hammer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: get_process_mem
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sidekiq
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Quiets and kills sidekiq processes when the RSS memory usage is too high.
70
+ email:
71
+ - k.hammer@youngcapital.nl
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - CODE_OF_CONDUCT.md
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/noxa-sidekiq-killer.rb
85
+ - lib/sidekiq/killer.rb
86
+ - lib/sidekiq/killer/memory.rb
87
+ - lib/sidekiq/killer/version.rb
88
+ - sidekiq-killer.gemspec
89
+ homepage: https://github.com/Noxa/sidekiq-killer
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.6.11
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Quiets and kills sidekiq when memory usage is too high
113
+ test_files: []