puma-hunter 0.0.1

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: 5e41d8e5d99d25b3a31ee4b535579175b6cdcdb6
4
+ data.tar.gz: efce5bda047150179e45868599fa86fcb88155ef
5
+ SHA512:
6
+ metadata.gz: 709a915ed5cd45446bea26c3dd68d8f0dd8c0e70d89fff6e0c81f4a1dda89f3b97a4b2295002aa51aa6b1a47f353ccc00f4e90ec5049caaa5dab5c590d2fd0ed
7
+ data.tar.gz: 971f41eef45302c0e53dbfedb7f43a03e6f25a07f47d10a89f75a0e1b16b687cc300bb5f24a82376664f02805e320fbe55b5ff58eaf734da68cbe2aa9bae9ef6
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /.ruby-version
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in puma-hunter.gemspec
4
+ gemspec
5
+
6
+ gem 'byebug', group: [:development, :test]
7
+
8
+ group :development, :test do
9
+ # You need these
10
+ gem "rspec"
11
+ gem "benchmark-ips"
12
+ gem "sourcify"
13
+ gem "awesome_print"
14
+ gem "pry"
15
+ gem "pry-doc"
16
+ end
@@ -0,0 +1,36 @@
1
+ # Puma::Hunter
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/puma/hunter`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'puma-hunter'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install puma-hunter
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/puma-hunter.
36
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "puma/hunter"
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
@@ -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,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.expand_path('../../lib', __FILE__)
4
+
5
+ require 'rubygems' unless Object.const_defined?(:Gem)
6
+ require 'puma/hunter/cli'
7
+
8
+ Puma::Hunter::CLI.go!
@@ -0,0 +1,51 @@
1
+ require "pathname"
2
+ require "procps"
3
+ require "puma/hunter/version"
4
+ require "puma/hunter/errors"
5
+
6
+ class Puma::Hunter
7
+ def self.from_pidfile(path, *args)
8
+ pidfile = Pathname(path)
9
+
10
+ if pid = pidfile.read
11
+ new(pid.to_i, *args)
12
+ else
13
+ fail PIDNotFound, path
14
+ end
15
+ end
16
+
17
+ def initialize(ppid, signal: "INT", max_mb: 1000, simulate: false)
18
+ @ppid = ppid
19
+ @max_bytes = (max_mb * (1024 ** 2)).to_i
20
+ @simulate = simulate
21
+ @signal = signal
22
+ end
23
+
24
+ def call
25
+ used_rss = Procps::Memsize.new(used_bytes / 1024)
26
+
27
+ warn "Running in simulate mode" if simulate?
28
+ puts "#{used_rss.inspect} with #{workers.size} workers without master (#{@ppid})."
29
+
30
+ if used_bytes >= @max_bytes
31
+ pid_to_kill = workers[-1][:pid]
32
+ warn "Out of max #{Procps::Memsize.new(@max_bytes / 1024).inspect}. Sending #{@signal} to PID #{pid_to_kill}."
33
+
34
+ unless simulate?
35
+ Process.kill(@signal, pid_to_kill)
36
+ end
37
+ end
38
+ end
39
+
40
+ def workers
41
+ @workers ||= Procps::PS.new("/bin/ps").select(:pid, :ppid, :rss).where(ppid: @ppid).sort("+rss").to_a
42
+ end
43
+
44
+ def used_bytes
45
+ @used_bytes ||= workers.reduce { |a, e| a + e[:rss] }
46
+ end
47
+
48
+ def simulate?
49
+ !!@simulate
50
+ end
51
+ end
@@ -0,0 +1,43 @@
1
+ require 'puma/hunter'
2
+ require 'optparse'
3
+ require 'methadone'
4
+
5
+ class Puma::Hunter
6
+ class CLI
7
+ include Methadone::Main
8
+ include Methadone::CLILogging
9
+
10
+ main do |path|
11
+ hunter = Puma::Hunter.from_pidfile(path,
12
+ :max_mb => options['max-rss'].to_f,
13
+ :signal => options['signal'],
14
+ :simulate => options['simulate']
15
+ )
16
+ hunter.call
17
+ end
18
+
19
+ version VERSION
20
+
21
+ description "Finds fingerings of the requested chord."
22
+
23
+
24
+ ##
25
+ # Arguments
26
+ #
27
+ arg :pidfile, "Puma's master worker PIDFile."
28
+
29
+
30
+ ##
31
+ # Options
32
+ #
33
+ options['max-rss'] = ENV.fetch('PUMA_MAX_RSS_MB') { 1000 }
34
+ options['signal'] = ENV.fetch('SIGNAL') { 'INT' }
35
+ options['simulate'] = ENV['SIMULATE'] == '1'
36
+
37
+ on "-m N", "--max-rss N", Integer, "Max total memory used by workers in Mb."
38
+ on "-s", "--signal SIGNAL", "Signal to send to kill worker."
39
+ on "-S", "--[no-]simulate", "Only simulate."
40
+
41
+ use_log_level_option
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ class Puma::Hunter
2
+ class PIDNotFound < RuntimeError
3
+ def initialize(path)
4
+ super("PID not found when reading pidfile from path '#{path}'.")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Puma
2
+ class Hunter
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'puma/hunter/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "puma-hunter"
9
+ spec.version = Puma::Hunter::VERSION
10
+ spec.authors = ["Anton Semenov"]
11
+ spec.email = ["anton.estum@gmail.com"]
12
+
13
+ spec.summary = %q{Task to kill Puma web server workers.}
14
+ spec.description = %q{Like the puma_worker_killer, but designed to run separately from a cron job.}
15
+ spec.homepage = "https://github.com/estum/puma-hunter"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "procps-rb"
24
+ spec.add_runtime_dependency "methadone"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.11"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puma-hunter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Anton Semenov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: procps-rb
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: methadone
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: Like the puma_worker_killer, but designed to run separately from a cron
70
+ job.
71
+ email:
72
+ - anton.estum@gmail.com
73
+ executables:
74
+ - puma-hunter
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - exe/puma-hunter
87
+ - lib/puma/hunter.rb
88
+ - lib/puma/hunter/cli.rb
89
+ - lib/puma/hunter/errors.rb
90
+ - lib/puma/hunter/version.rb
91
+ - puma-hunter.gemspec
92
+ homepage: https://github.com/estum/puma-hunter
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Task to kill Puma web server workers.
116
+ test_files: []
117
+ has_rdoc: