worker_killer 0.1.0.19844 → 0.1.0.30337
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/worker_killer.rb +20 -3
- data/lib/worker_killer/configuration.rb +33 -2
- data/lib/worker_killer/middleware.rb +2 -0
- data/spec/worker_killer_spec.rb +28 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f82e8e7af98e4d6412f25fba67ce6557012f4837173ac2447d1c87fdabe8fef1
|
4
|
+
data.tar.gz: 4a7d13da9e325d0514fbc4fe75dc15890ea82cef14ff847c8c60dbb859c63b91
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b4a853921c5fc93a16a686c8572e427cf6faa2436b670af01d01ee18e707c3974a43625ac8e393accf0be63195f8101054d6f012f25b0bde9dc8c0c974b37fc
|
7
|
+
data.tar.gz: 2c0febe3c7176a32d6b83d3654c5a098531ebc74a4bb725ce0beebfc2fb3c78bb9def46c2515ca20510b8656448c965b6ce81beb1d377c2ddf6f138cd88484ea
|
data/lib/worker_killer.rb
CHANGED
@@ -29,7 +29,6 @@ module WorkerKiller
|
|
29
29
|
# signal. A single signal is sent per request.
|
30
30
|
def self.kill_self(logger, start_time)
|
31
31
|
alive_sec = (Time.now - start_time).round
|
32
|
-
self_pid = Process.pid
|
33
32
|
|
34
33
|
@kill_attempts ||= 0
|
35
34
|
@kill_attempts += 1
|
@@ -43,8 +42,26 @@ module WorkerKiller
|
|
43
42
|
sig = :KILL if @kill_attempts > configuration.kill_attempts
|
44
43
|
end
|
45
44
|
|
46
|
-
|
47
|
-
|
45
|
+
if sig == :QUIT && configuration.passenger?
|
46
|
+
kill_by_passenger(logger, alive_sec, configuration.passenger_config, Process.pid)
|
47
|
+
else
|
48
|
+
kill_by_signal(logger, alive_sec, sig, Process.pid)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.kill_by_signal(logger, alive_sec, signal, pid)
|
53
|
+
logger.warn "#{self} send SIG#{signal} (pid: #{pid}) alive: #{alive_sec} sec (trial #{@kill_attempts})"
|
54
|
+
Process.kill signal, pid
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.kill_by_passenger(logger, alive_sec, passenger, pid)
|
58
|
+
cmd = "#{passenger} detach-process #{pid}"
|
59
|
+
logger.warn "#{self} run #{cmd.inspect} (pid: #{pid}) alive: #{alive_sec} sec (trial #{@kill_attempts})"
|
60
|
+
Thread.new(cmd) do |command|
|
61
|
+
unless Kernel.system(command)
|
62
|
+
logger.warn "#{self} run #{cmd.inspect} failed: #{$?.inspect}"
|
63
|
+
end
|
64
|
+
end
|
48
65
|
end
|
49
66
|
|
50
67
|
end
|
@@ -4,16 +4,47 @@ module WorkerKiller
|
|
4
4
|
# Methods for configuring WorkerKiller
|
5
5
|
class Configuration
|
6
6
|
|
7
|
-
attr_accessor :logger, :quit_attempts, :kill_attempts, :use_quit
|
7
|
+
attr_accessor :logger, :quit_attempts, :kill_attempts, :use_quit, :passenger_config
|
8
8
|
|
9
9
|
# Override defaults for configuration
|
10
|
-
def initialize(quit_attempts: 5, kill_attempts: 10, use_quit: true)
|
10
|
+
def initialize(quit_attempts: 5, kill_attempts: 10, use_quit: true, passenger_config: nil)
|
11
11
|
@quit_attempts = quit_attempts
|
12
12
|
@kill_attempts = kill_attempts
|
13
13
|
@use_quit = use_quit
|
14
|
+
@passenger_config = check_passenger(passenger_config)
|
14
15
|
@logger = Logger.new(STDOUT, level: Logger::INFO, progname: 'WorkerKiller')
|
15
16
|
end
|
16
17
|
|
18
|
+
def passenger?
|
19
|
+
!@passenger_config.nil?
|
20
|
+
end
|
21
|
+
|
22
|
+
def check_passenger provided_path
|
23
|
+
if provided_path.nil? || provided_path.empty?
|
24
|
+
return check_passenger_config(`which passenger-config`)
|
25
|
+
else
|
26
|
+
return check_passenger_config!(provided_path)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def check_passenger_config path
|
31
|
+
path.strip!
|
32
|
+
help = `#{path} detach-process --help 2> /dev/null`
|
33
|
+
if help['Remove an application process from the Phusion Passenger process pool']
|
34
|
+
return path
|
35
|
+
end
|
36
|
+
rescue StandardError => e
|
37
|
+
return nil
|
38
|
+
end
|
39
|
+
|
40
|
+
def check_passenger_config! path
|
41
|
+
if path = check_passenger_config(path)
|
42
|
+
return path
|
43
|
+
else
|
44
|
+
raise "Can't find passenger config at #{path.inspect}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
17
48
|
end
|
18
49
|
end
|
19
50
|
|
data/spec/worker_killer_spec.rb
CHANGED
@@ -23,6 +23,27 @@ RSpec.describe WorkerKiller do
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
describe '#kill_by_signal' do
|
27
|
+
[:QUIT, :TERM, :KILL].each do |sig|
|
28
|
+
it "must send #{sig} signal" do
|
29
|
+
pid = rand(1000)
|
30
|
+
expect(Process).to receive(:kill).with(sig, pid)
|
31
|
+
WorkerKiller.kill_by_signal(logger, 11111, sig, pid)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#kill_by_passenger' do
|
37
|
+
it "must run passenger-config" do
|
38
|
+
pid = rand(1000)
|
39
|
+
path = "passenger-config-#{rand(1000)}"
|
40
|
+
expect(Kernel).to receive(:system).with("#{path} detach-process #{pid}").and_return(true)
|
41
|
+
|
42
|
+
thread = WorkerKiller.kill_by_passenger(logger, 11111, path, pid)
|
43
|
+
thread.join
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
26
47
|
describe '#kill_self' do
|
27
48
|
context 'with use_quit TRUE' do
|
28
49
|
around do |example|
|
@@ -35,9 +56,10 @@ RSpec.describe WorkerKiller do
|
|
35
56
|
end
|
36
57
|
|
37
58
|
it 'expect right signal order' do
|
38
|
-
expect(
|
39
|
-
expect(
|
40
|
-
expect(
|
59
|
+
expect(WorkerKiller).to receive(:kill_by_signal).with(logger, anything, :QUIT, anything).exactly(2).times
|
60
|
+
expect(WorkerKiller).to receive(:kill_by_signal).with(logger, anything, :TERM, anything).exactly(2).times
|
61
|
+
expect(WorkerKiller).to receive(:kill_by_signal).with(logger, anything, :KILL, anything).exactly(5).times
|
62
|
+
|
41
63
|
2.times { WorkerKiller.kill_self(logger, Time.now) } # 2 QUIT
|
42
64
|
2.times { WorkerKiller.kill_self(logger, Time.now) } # 2 TERM
|
43
65
|
5.times { WorkerKiller.kill_self(logger, Time.now) } # other - KILL
|
@@ -55,8 +77,9 @@ RSpec.describe WorkerKiller do
|
|
55
77
|
end
|
56
78
|
|
57
79
|
it 'expect right signal order' do
|
58
|
-
expect(
|
59
|
-
expect(
|
80
|
+
expect(WorkerKiller).to receive(:kill_by_signal).with(logger, anything, :TERM, anything).exactly(2).times
|
81
|
+
expect(WorkerKiller).to receive(:kill_by_signal).with(logger, anything, :KILL, anything).exactly(5).times
|
82
|
+
|
60
83
|
2.times { WorkerKiller.kill_self(logger, Time.now) } # 2 TERM
|
61
84
|
5.times { WorkerKiller.kill_self(logger, Time.now) } # other - KILL
|
62
85
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: worker_killer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.
|
4
|
+
version: 0.1.0.30337
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samoilenko Yuri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: get_process_mem
|