worker_killer 1.0.4.189871 → 1.0.5.213889

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: 1481f9bea1f77d274c4155a94bd83c6527ab72eb53fe6958b8036985d714720c
4
- data.tar.gz: 155090caa35e210c9e130f395b00d4b83eab0ec7dcc3aba1a4419e783410c442
3
+ metadata.gz: 1d0558051a55e944e97bb26729239a97d7f150fb37885af8761246f78db4e280
4
+ data.tar.gz: 417bb8c4f0c4818d4e2a19ad4f2cb8cc5e3ca82621b925c81c2e4b33102803fb
5
5
  SHA512:
6
- metadata.gz: 3b3d7dbef3dc17f218341dace5fca0a1f4c92263ef104b1c1d7f9d1a5f659650e21578f00844f55cd073228117b17744cb5526c388936e90a862e215d9c9f497
7
- data.tar.gz: 7cd8957b9e57ee14f6ae13ff03dbec12369fc45e82f97d4f93b3f378606460254110c37209f7afb53cd2d053a81ccc2a021089386e15dd83bee7a174783e0683
6
+ metadata.gz: 855f6852cb2c7c8115388038f07d234958a6183c008d21b28ad8867a34a53dcfd713f34645c1b0f435fafc67f5f7ad0c1272b7b4f94f2d91b170ca91c0f97133
7
+ data.tar.gz: 8699bc63da513186e47cb1ef626f732e61cfe7a603231370223cbcea5ec3b6d8897a314ed5fe8dee9a23765822b4c20aa43ddc6fda7b02bb775f01aa1f838a7e
data/README.md CHANGED
@@ -17,6 +17,7 @@ Features:
17
17
 
18
18
  - generic middleware implementation
19
19
  - Phusion Passenger support(through `passenger-config detach-process <PID>`)
20
+ - Puma phased-restart support(through `pumactl phased-restart`)
20
21
  - DelayedJob support
21
22
  - custom reaction hook
22
23
 
@@ -42,14 +43,14 @@ Add these lines to your `config.ru` or `application.rb`. (These lines should be
42
43
 
43
44
  # Max requests per worker
44
45
  middleware.insert_before(
45
- Rack::Sendfile,
46
+ Rack::Runtime,
46
47
  WorkerKiller::Middleware::RequestsLimiter, killer: killer, min: 3072, max: 4096
47
48
  )
48
49
 
49
50
  # Max memory size (RSS) per worker
50
51
  middleware.insert_before(
51
- Rack::Sendfile,
52
- WorkerKiller::Middleware::OOMLimiter, killer: killer, min: 500 * (1024**2), max: 600 * (1024**2)
52
+ Rack::Runtime,
53
+ WorkerKiller::Middleware::OOMLimiter, killer: killer, min: nil, max: 0.5, check_cycle: 16
53
54
  )
54
55
  ```
55
56
 
@@ -74,6 +75,39 @@ Add these lines to your `initializers/delayed_job.rb` or `application.rb`.
74
75
  end
75
76
  ```
76
77
 
78
+ ## Puma Web-server
79
+
80
+ Add these lines to your `puma.rb` AND `application.rb`.
81
+
82
+ ```ruby
83
+ # puma.rb
84
+
85
+ on_worker_boot do |*args|
86
+ require 'worker_killer/killer'
87
+ require 'active_support/notifications'
88
+
89
+ ::ActiveSupport::Notifications.subscribe 'initialize.custom.rails' do |*eargs|
90
+ event = ActiveSupport::Notifications::Event.new(*eargs)
91
+ config = event.payload[:config]
92
+
93
+ killer = ::WorkerKiller::Killer::Puma.new
94
+ config.middleware.insert_before(
95
+ ::Rack::Runtime,
96
+ ::WorkerKiller::Middleware::RequestsLimiter, killer: killer, min: 3072, max: 4096
97
+ )
98
+
99
+ config.middleware.insert_before(
100
+ ::Rack::Runtime,
101
+ ::WorkerKiller::Middleware::OOMLimiter, killer: killer, min: nil, max: 0.5, verbose: true, check_cycle: 16
102
+ )
103
+ end
104
+ end
105
+
106
+ # application.rb
107
+
108
+ ActiveSupport::Notifications.instrument 'initialize.custom.rails', config: config
109
+ ```
110
+
77
111
  This gem provides two modules: WorkerKiller::CountLimiter and WorkerKiller::MemoryLimiter, some Rack integration and DelayedJob plugin.
78
112
 
79
113
  ### WorkerKiller::Middleware::RequestsLimiter and WorkerKiller::DelayedJobPlugin::JobsLimiter
@@ -0,0 +1,33 @@
1
+ module ::WorkerKiller
2
+ module Killer
3
+ class Puma < ::WorkerKiller::Killer::Base
4
+
5
+ def initialize **kwrags
6
+ super
7
+ end
8
+
9
+ def do_kill(sig, pid, alive_sec, **_params)
10
+ cmd = 'pumactl phased-restart'
11
+
12
+ if sig == :KILL
13
+ logger.error "#{self} force to kill self (pid: #{pid}) alive: #{alive_sec} sec (trial #{kill_attempts})"
14
+ Process.kill sig, pid
15
+ return
16
+ end
17
+
18
+ return if @already_detached
19
+
20
+ logger.warn "#{self} run #{cmd.inspect} (pid: #{pid}) alive: #{alive_sec} sec (trial #{kill_attempts})"
21
+ @already_detached = true
22
+
23
+ Thread.new(cmd) do |command|
24
+ unless Kernel.system(command)
25
+ logger.warn "#{self} run #{command.inspect} failed: #{$?.inspect}"
26
+ end
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+
@@ -38,5 +38,6 @@ end
38
38
 
39
39
  require_relative 'killer/signal'
40
40
  require_relative 'killer/passenger'
41
+ require_relative 'killer/puma'
41
42
  require_relative 'killer/delayed_job'
42
43
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module WorkerKiller
4
4
 
5
- VERSION = '1.0.4'
5
+ VERSION = '1.0.5'
6
6
 
7
7
  end
8
8
 
@@ -0,0 +1,33 @@
1
+ RSpec.describe WorkerKiller::Killer::Puma do
2
+ let(:config) do
3
+ WorkerKiller::Configuration.new.tap do |c|
4
+ c.quit_attempts = 2
5
+ c.term_attempts = 2
6
+ end
7
+ end
8
+
9
+ let(:killer){ described_class.new }
10
+
11
+ describe '#kill' do
12
+ around do |example|
13
+ prev = WorkerKiller.configuration
14
+ WorkerKiller.configuration = config
15
+ example.run
16
+ ensure
17
+ WorkerKiller.configuration = prev
18
+ end
19
+
20
+ it 'expect right signal order' do
21
+ expect(Kernel).to receive(:system).with('pumactl phased-restart').and_return(true)
22
+ expect(Process).to receive(:kill).with(:KILL, anything).exactly(5).times
23
+
24
+ thread = killer.kill(Time.now)
25
+ thread.join
26
+
27
+ 1.times { killer.kill(Time.now) } # 1 QUIT
28
+ 2.times { killer.kill(Time.now) } # 1 TERM
29
+ 5.times { killer.kill(Time.now) } # 5 KILL
30
+ end
31
+ end
32
+ end
33
+
data/spec/killer_spec.rb CHANGED
@@ -19,7 +19,7 @@ RSpec.describe WorkerKiller::Killer::Base do
19
19
  end
20
20
 
21
21
  it 'expect right signal order' do
22
- if RUBY_VERSION >= "3.0.0"
22
+ if RUBY_VERSION >= "2.7.0"
23
23
  expect(killer).to receive(:do_kill).with(:QUIT, anything, anything).exactly(2).times
24
24
  expect(killer).to receive(:do_kill).with(:TERM, anything, anything).exactly(2).times
25
25
  expect(killer).to receive(:do_kill).with(:KILL, anything, anything).exactly(5).times
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: 1.0.4.189871
4
+ version: 1.0.5.213889
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samoilenko Yuri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-05 00:00:00.000000000 Z
11
+ date: 2024-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: get_process_mem
@@ -131,6 +131,7 @@ files:
131
131
  - lib/worker_killer/killer.rb
132
132
  - lib/worker_killer/killer/delayed_job.rb
133
133
  - lib/worker_killer/killer/passenger.rb
134
+ - lib/worker_killer/killer/puma.rb
134
135
  - lib/worker_killer/killer/signal.rb
135
136
  - lib/worker_killer/memory_limiter.rb
136
137
  - lib/worker_killer/middleware.rb
@@ -140,6 +141,7 @@ files:
140
141
  - spec/delayed_job_plugin/oom_limiter_spec.rb
141
142
  - spec/killer/delayed_job_spec.rb
142
143
  - spec/killer/passenger_spec.rb
144
+ - spec/killer/puma_spec.rb
143
145
  - spec/killer/signal_spec.rb
144
146
  - spec/killer_spec.rb
145
147
  - spec/memory_limiter_spec.rb
@@ -171,15 +173,16 @@ signing_key:
171
173
  specification_version: 4
172
174
  summary: Kill any workers by memory and request counts or take custom reaction
173
175
  test_files:
174
- - spec/middleware_spec.rb
176
+ - spec/killer/signal_spec.rb
177
+ - spec/killer/puma_spec.rb
178
+ - spec/killer/delayed_job_spec.rb
179
+ - spec/killer/passenger_spec.rb
175
180
  - spec/killer_spec.rb
181
+ - spec/count_limiter_spec.rb
182
+ - spec/worker_killer_spec.rb
183
+ - spec/support/logger.rb
184
+ - spec/middleware_spec.rb
176
185
  - spec/spec_helper.rb
177
186
  - spec/delayed_job_plugin/oom_limiter_spec.rb
178
187
  - spec/delayed_job_plugin/jobs_limiter_spec.rb
179
- - spec/killer/passenger_spec.rb
180
- - spec/killer/signal_spec.rb
181
- - spec/killer/delayed_job_spec.rb
182
188
  - spec/memory_limiter_spec.rb
183
- - spec/support/logger.rb
184
- - spec/worker_killer_spec.rb
185
- - spec/count_limiter_spec.rb