resque-worker-killer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9828594da6a4652add1a03fcd46ad5b3f3e06c68
4
+ data.tar.gz: d31927afb2293274a0755de5047821fb5c99cb87
5
+ SHA512:
6
+ metadata.gz: 0d5c4fa9aebe743de6fd326ebc9e5b7491c7a79bc4fd438c5ac69cd101ddb8d59887ea1b5220a7f4424a021be808c2d4c3ee5c9e975b16c1b261ec771851e727
7
+ data.tar.gz: 9da4c88ed48f8a63f05e6b51938258b187192a756fe95a9be4f34da4d831f2d73b02b9f5441bf91b8d9a4ad2fd5846997550523564b0540ecd7179fd1a8265db
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 0.1.0 (2016/08/20)
2
+
3
+ First vesion
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at sonots@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in resque-worker-killer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Naotoshi Seo
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.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Resque::Worker::Killer
2
+
3
+ [Resque](https://github.com/resque/resque) is widely used Redis-backed Ruby library for creating background jobs. One thing we thought Resque missed, is killing a forked child of Resque worker based on consumed memories.
4
+
5
+ resque-worker-killer gem provides automatic kill of a forked child of Resque worker based on process memory size (RSS) not to exceed the maximum allowed memory size.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'resque-worker-killer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install resque-worker-killer
22
+
23
+ ## Usage
24
+
25
+ Use the plugin:
26
+
27
+ ```ruby
28
+ require 'resque'
29
+ require 'resque-worker-killer'
30
+
31
+ class MyJob
32
+ extend Resque::Plugins::WorkerKiller
33
+ @queue = :example_queue
34
+
35
+ extend Resque::Plugins::WorkerKiller
36
+ @worker_killer_monitor_interval = 0.5 # sec
37
+ @worker_killer_mem_limit = 300_000 # KB
38
+ @worker_killer_max_term = 10 # try TERM 10 times, then KILL
39
+ @worker_killer_verbose = false # verbose log
40
+ @worker_killer_logger = Resque.logger
41
+
42
+ def self.perform(*args)
43
+ puts 'started'
44
+ sleep 10
45
+ puts 'finished'
46
+ rescue Resque::TermException => e # env TERM_CHILD=1
47
+ puts 'terminated'
48
+ end
49
+ end
50
+ ```
51
+
52
+ `TERM_CHILD` environment variable must be set on starting resque worker:
53
+
54
+ ```
55
+ $ TERM_CHILD=1 bundle exec rake resque:work
56
+ ```
57
+
58
+ Options are:
59
+
60
+ * `@worker_killer_monitor_interval`: Monotring interval to check RSS size (default: 1.0 sec)
61
+ * `@worker_killer_mem_limit`: RSS usage limit, in killobytes (default: 300MB)
62
+ * `@worker_killer_max_term`: Try kiling child process with SIGTERM in `@worker_killer_max_term` times (default: 10), then SIGKILL if it still does not die. Please note that resque worker must be started `TERM_CHILD=1` environment variable.
63
+ * `@worker_killer_verbose`: Verbose log
64
+ * `@worker_killer_logger`: Logger instance (default: Resque.logger)
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sonots/resque-worker-killer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
69
+
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
74
+
75
+ ## ChangeLog
76
+
77
+ [CHANGELOG.md](./CHANGELOG.md)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "resque/worker/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
data/bin/setup ADDED
@@ -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
data/example/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rake'
4
+ gem 'resque'
5
+ gem 'resque-worker-killer', path: '../../'
6
+ gem 'pry'
7
+ gem 'pry-nav'
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: ../../
3
+ specs:
4
+ resque-worker-killer (0.1.0)
5
+ get_process_mem
6
+ resque
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ coderay (1.1.1)
12
+ get_process_mem (0.2.1)
13
+ method_source (0.8.2)
14
+ mono_logger (1.1.0)
15
+ multi_json (1.12.1)
16
+ pry (0.10.4)
17
+ coderay (~> 1.1.0)
18
+ method_source (~> 0.8.1)
19
+ slop (~> 3.4)
20
+ pry-nav (0.2.4)
21
+ pry (>= 0.9.10, < 0.11.0)
22
+ rack (1.6.4)
23
+ rack-protection (1.5.3)
24
+ rack
25
+ rake (11.2.2)
26
+ redis (3.3.1)
27
+ redis-namespace (1.5.2)
28
+ redis (~> 3.0, >= 3.0.4)
29
+ resque (1.26.0)
30
+ mono_logger (~> 1.0)
31
+ multi_json (~> 1.0)
32
+ redis-namespace (~> 1.3)
33
+ sinatra (>= 0.9.2)
34
+ vegas (~> 0.1.2)
35
+ sinatra (1.4.7)
36
+ rack (~> 1.5)
37
+ rack-protection (~> 1.4)
38
+ tilt (>= 1.3, < 3)
39
+ slop (3.6.0)
40
+ tilt (2.0.5)
41
+ vegas (0.1.11)
42
+ rack (>= 1.0.0)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ pry
49
+ pry-nav
50
+ rake
51
+ resque
52
+ resque-worker-killer!
53
+
54
+ BUNDLED WITH
55
+ 1.11.2
data/example/README.md ADDED
@@ -0,0 +1,13 @@
1
+ Start a resque worker
2
+
3
+ ```
4
+ $ QUEUE=* TERM_CHILD=1 bundle exec rake resque:work
5
+ ```
6
+
7
+ Enqueue
8
+
9
+ ```
10
+ $ bundle exec rake test
11
+ ```
12
+
13
+ See logs of resque:work
data/example/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'resque/tasks'
2
+ require_relative 'my_job'
3
+
4
+ task :test do
5
+ Resque.enqueue(MyJob, {})
6
+ end
data/example/my_job.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'logger'
2
+ require 'resque'
3
+ require 'resque-worker-killer'
4
+
5
+ class MyJob
6
+ @queue = :resque_worker_killer
7
+
8
+ extend Resque::Plugins::WorkerKiller
9
+ @worker_killer_monitor_interval = 0.5 # sec
10
+ @worker_killer_mem_limit = 10_000 # KB
11
+ @worker_killer_max_term = 10
12
+ @worker_killer_verbose = true
13
+ @worker_killer_logger = ::Logger.new(STDOUT)
14
+
15
+ def self.perform(params)
16
+ puts 'started'
17
+ sleep 3
18
+ str = 'a' * 10 * 1024 * 1024
19
+ sleep 7
20
+ puts 'finished'
21
+ rescue Resque::TermException => e # env TERM_CHILD=1
22
+ puts 'terminated'
23
+ end
24
+ end
@@ -0,0 +1 @@
1
+ require 'resque/plugins/worker_killer'
@@ -0,0 +1,93 @@
1
+ require "resque/plugins/worker_killer/version"
2
+ require 'get_process_mem'
3
+
4
+ module Resque
5
+ module Plugins
6
+ module WorkerKiller
7
+ def worker_killer_monitor_interval
8
+ @worker_killer_monitor_interval ||= 1.0 # sec
9
+ end
10
+
11
+ def worker_killer_mem_limit
12
+ @worker_killer_mem_limit ||= 300 * 1024 # killo bytes
13
+ end
14
+
15
+ def worker_killer_max_term
16
+ @worker_killer_max_term ||= 10
17
+ end
18
+
19
+ def worker_killer_verbose
20
+ @worker_killer_verbose = false if @worker_killer_verbose.nil?
21
+ @worker_killer_verbose
22
+ end
23
+
24
+ def worker_killer_logger
25
+ @worker_killer_logger ||= ::Resque.logger
26
+ end
27
+
28
+ def self.extended(klass)
29
+ Resque.after_fork do |job|
30
+ # this is ran in the forked child process
31
+ # we do not let the monitor thread die since the process itself dies
32
+ Thread.start { PrivateMethods.new(klass).monitor_oom }
33
+ end
34
+ end
35
+
36
+ class PrivateMethods
37
+ def initialize(obj)
38
+ @obj = obj
39
+ end
40
+
41
+ # delegate attr_reader
42
+ %i[
43
+ monitor_interval
44
+ mem_limit
45
+ max_term
46
+ verbose
47
+ logger
48
+ ].each do |method|
49
+ define_method(method) do
50
+ @obj.send("worker_killer_#{method}")
51
+ end
52
+ end
53
+
54
+ def plugin_name
55
+ "Resque::Plugins::WorkerKiller"
56
+ end
57
+
58
+ def monitor_oom
59
+ start_time = Time.now
60
+ loop do
61
+ one_shot_monitor_oom(start_time)
62
+ sleep monitor_interval
63
+ end
64
+ end
65
+
66
+ def one_shot_monitor_oom(start_time)
67
+ rss = GetProcessMem.new.kb
68
+ logger.info "#{plugin_name}: worker (pid: #{Process.pid}) using #{rss} KB." if verbose
69
+ if rss > mem_limit
70
+ logger.warn "#{plugin_name}: worker (pid: #{Process.pid}) exceeds memory limit (#{rss} KB > #{mem_limit} KB)"
71
+ kill_self(logger, start_time)
72
+ end
73
+ end
74
+
75
+ # Kill the current process by telling it to send signals to itself If
76
+ # the process isn't killed after `@max_term` TERM signals,
77
+ # send a KILL signal.
78
+ def kill_self(logger, start_time)
79
+ alive_sec = (Time.now - start_time).round
80
+
81
+ @@kill_attempts ||= 0
82
+ @@kill_attempts += 1
83
+
84
+ sig = :TERM
85
+ sig = :KILL if @@kill_attempts > max_term
86
+
87
+ logger.warn "#{plugin_name}: send SIG#{sig} (pid: #{Process.pid}) alive: #{alive_sec} sec (trial #{@@kill_attempts})"
88
+ Process.kill(sig, Process.pid)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,7 @@
1
+ module Resque
2
+ module Plugins
3
+ module WorkerKiller
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'resque/plugins/worker_killer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "resque-worker-killer"
8
+ spec.version = Resque::Plugins::WorkerKiller::VERSION
9
+ spec.authors = ["Naotoshi Seo"]
10
+ spec.email = ["sonots@gmail.com"]
11
+
12
+ spec.summary = %q{Automatically kill a child of resque worker based on max memory.}
13
+ spec.description = %q{Automatically kill a child of resque worker based on max memory.}
14
+ spec.homepage = "https://github.com/sonots/resque-worker-killer"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "resque"
23
+ spec.add_dependency "get_process_mem"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-worker-killer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Naotoshi Seo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: resque
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: get_process_mem
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Automatically kill a child of resque worker based on max memory.
70
+ email:
71
+ - sonots@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CHANGELOG.md
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - example/Gemfile
87
+ - example/Gemfile.lock
88
+ - example/README.md
89
+ - example/Rakefile
90
+ - example/my_job.rb
91
+ - lib/resque-worker-killer.rb
92
+ - lib/resque/plugins/worker_killer.rb
93
+ - lib/resque/plugins/worker_killer/version.rb
94
+ - resque-worker-killer.gemspec
95
+ homepage: https://github.com/sonots/resque-worker-killer
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Automatically kill a child of resque worker based on max memory.
119
+ test_files: []