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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f35f1ea9a4591062e331c861888dbe5119191e94510e42d3900ab0826ccfad9f
4
- data.tar.gz: 170b036d6c77ffbc483318f683479266d05df0a97af83ed417f27b8b2fdc380e
3
+ metadata.gz: f82e8e7af98e4d6412f25fba67ce6557012f4837173ac2447d1c87fdabe8fef1
4
+ data.tar.gz: 4a7d13da9e325d0514fbc4fe75dc15890ea82cef14ff847c8c60dbb859c63b91
5
5
  SHA512:
6
- metadata.gz: 036ca0c4a7f7107c6c793b094f6f93282a54a3c053b7080f14fdea1ef3211aa806d2e336483e09ea59153ddb209229d64dd0af577d34268f599dac76e43e8f6a
7
- data.tar.gz: 41c34fa4b40ce654871bb1f1b6e6a0591894d7aa10e546848dc2778b2ddcc8f34eef3dbd6b3a14b15f80b76a4c4d0970a3c3479eb5a3b779a0e0c6bd0e3e57f9
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
- logger.warn "#{self} send SIG#{sig} (pid: #{self_pid}) alive: #{alive_sec} sec (trial #{@kill_attempts})"
47
- Process.kill sig, self_pid
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
 
@@ -9,6 +9,8 @@ module WorkerKiller
9
9
  def initialize(app, klass:, reaction: nil, **opts)
10
10
  @app = app
11
11
 
12
+ @passenger = system('which passenger')
13
+
12
14
  reaction ||= proc do |limiter|
13
15
  WorkerKiller.kill_self(limiter.logger, limiter.started_at)
14
16
  end
@@ -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(Process).to receive(:kill).with(:QUIT, anything).exactly(2).times
39
- expect(Process).to receive(:kill).with(:TERM, anything).exactly(2).times
40
- expect(Process).to receive(:kill).with(:KILL, anything).exactly(5).times
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(Process).to receive(:kill).with(:TERM, anything).exactly(2).times
59
- expect(Process).to receive(:kill).with(:KILL, anything).exactly(5).times
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.19844
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: 2019-11-28 00:00:00.000000000 Z
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