process_kill 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: a73237c3852a6dd3ce22777274c48b09e9d87405
4
+ data.tar.gz: 479e0c5da548f6bfeb09c6df8c833635382f66fb
5
+ SHA512:
6
+ metadata.gz: d3e392eca021095d0c5b6532fc02ea8707701e9a70bf0da7550bcc5fc1eb82ac6e8f41239f538da05cb73e25b8c119f8791d1e0a6f950f46dd0fd182c8be58c0
7
+ data.tar.gz: c12b35bcb53b4859a2769dfd202aaf3f298b0b247457897d29f680403076d6f06d26f6f61463acf01661426b7ee929fd5c78e9114f15177195beaf6f21c38c97
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
11
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in process_kill.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # ProcessKill
2
+
3
+ ProcessKill is a Ruby implementation of the flow to attempt to kill processes with different signals and intervals.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'process_kill'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install process_kill
20
+
21
+ ## Usage
22
+
23
+ $ process_kill PID
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ 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).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/process_kill.
34
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "process_kill"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/exec/process_kill ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "process_kill"
5
+
6
+ puts ProcessKill.execute([ARGV[0].to_i]).inspect
@@ -0,0 +1,78 @@
1
+ require "process_kill/version"
2
+
3
+ module ProcessKill
4
+
5
+ DEFAULT_FLOW = [
6
+ { signal: 'QUIT', interval: [1,2,3,5,8,13] },
7
+ { signal: 'TERM', max_retry: 5 },
8
+ { signal: 'KILL', max_retry: 3, interval: 5 },
9
+ ].freeze
10
+
11
+ def self.compile_flow(flow)
12
+ result = flow.map do |step|
13
+ intervals = if step[:interval].kind_of?(Array) && step[:max_retry]
14
+ step[:interval].slice(0, step[:max_retry])
15
+ elsif step[:interval].kind_of?(Array) && !step[:max_retry]
16
+ step[:interval]
17
+ elsif step[:interval].kind_of?(Fixnum) && step[:max_retry]
18
+ [step[:interval]] * step[:max_retry]
19
+ elsif step[:max_retry]
20
+ [1] * step[:max_retry]
21
+ else
22
+ [1]
23
+ end
24
+ {signal: step[:signal], intervals: intervals}
25
+ end
26
+ end
27
+
28
+ def self.generate_result_template(pids, flow)
29
+ stats_template = flow.map{ |f| {signal: f[:signal], attempts: []} }
30
+ template = pids.reduce({}) do |hash, pid|
31
+ hash.merge!(pid => {steps: stats_template.dup, killed: false, resolved: false})
32
+ end
33
+
34
+ return template
35
+ end
36
+
37
+ def self.execute(pids, flow=nil)
38
+ result = generate_result_template(pids, flow || DEFAULT_FLOW)
39
+ compiled_flow = compile_flow(flow || DEFAULT_FLOW)
40
+ compiled_flow.each_with_index do |step, step_index|
41
+ step[:intervals].each do |interval|
42
+ pids.each do |pid|
43
+ result_step_item = result[pid][:steps][step_index]
44
+ next if result_step_item[:error] == 'not_found' || result[pid][:resolved]
45
+ begin
46
+ result_step_item[:attempts] << interval # increase attempts counter
47
+ ProcessKill.kill(step[:signal], pid) # send signal
48
+ ProcessKill.sleep(interval) # sleep before next interval
49
+ rescue ProcessNotFoundError => e
50
+ result_step_item[:error] = 'not_found'
51
+ result[pid][:killed] = true
52
+ result[pid][:resolved] = true
53
+ rescue UnknownError, ProcessPermissionError => e
54
+ result_step_item[:error] = 'no_permission'
55
+ result[pid][:resolved] = true
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ result
62
+ end
63
+
64
+ def self.kill(signal, pid)
65
+ Process.kill(signal, pid)
66
+ rescue Errno::ESRCH => e
67
+ raise ProcessNotFoundError
68
+ rescue Errno::EPERM => e
69
+ end
70
+
71
+ def self.sleep(seconds)
72
+ Kernel.sleep(seconds)
73
+ end
74
+
75
+ class ProcessNotFoundError < StandardError; end
76
+ class ProcessPermissionError < StandardError; end
77
+ class UnknownError < StandardError; end
78
+ end
@@ -0,0 +1,3 @@
1
+ module ProcessKill
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'process_kill/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "process_kill"
8
+ spec.version = ProcessKill::VERSION
9
+ spec.authors = ["Ming Liu"]
10
+ spec.email = ["liuming@lmws.net"]
11
+
12
+ spec.summary = %q{Kill processes with different signals and intervals}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/liuming/process_kill_rb/"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.13.6"
30
+ spec.add_development_dependency "rake", "~> 11.3"
31
+ spec.add_development_dependency "rspec"
32
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: process_kill
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ming Liu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-07 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.13.6
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.13.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '11.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '11.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Kill processes with different signals and intervals
56
+ email:
57
+ - liuming@lmws.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - exec/process_kill
71
+ - lib/process_kill.rb
72
+ - lib/process_kill/version.rb
73
+ - process_kill.gemspec
74
+ homepage: https://github.com/liuming/process_kill_rb/
75
+ licenses: []
76
+ metadata:
77
+ allowed_push_host: https://rubygems.org
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.6.8
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Kill processes with different signals and intervals
98
+ test_files: []