sidekiq-pool 1.0.2 → 1.1.0

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
  SHA1:
3
- metadata.gz: 1b8e14e29f0d6c84a30148c3e6ad2b18bc4f5c3d
4
- data.tar.gz: bc8370193f03b299eb2dbdbdc4f28a6f9cf8670f
3
+ metadata.gz: 1eae1143714aedde01e2e9b38713e1d017836950
4
+ data.tar.gz: f4ffeca132df5655eb44cfd6fe8e7d67236312e4
5
5
  SHA512:
6
- metadata.gz: 513c0b7b8d2306e24cadec43b58ea46cba3dfc329ccf98a750ca477442528be1fcf06be4b0eaaf8bf686e61fabc0d89c3f87facef90c43683a485523f9164bcc
7
- data.tar.gz: b265f6ee9de615966096ad2f55f482926ef185a8dac989cee72504348b10865a389c467630a4472a9eb953b3c0b62110561acd08ea558f1d266631783f611fb5
6
+ metadata.gz: fe81588f152fcf36f0b52888180c5b70dd2e4fbcdd258ccc01ac2cda72327af7d37e4a6533e5b052e6c274ef9b7cdc5ec9dcb4e941410b8ee2cdc0fef967ccd6
7
+ data.tar.gz: da14ea02a6af1ff6c71e30eaf968edff21996e80fef4cd0aedd24ecc91524f88dd4c0e3474a4938f16d83d517075c0438642f6885b0a7bd8a359c61c892a3887
data/README.md CHANGED
@@ -16,6 +16,7 @@ Create a config file and specify it's path with the *p* command line option (the
16
16
  Paste the following config and modify it to your needs:
17
17
 
18
18
  ```yaml
19
+ :working_directory: /path/to/working/direcory # optional, needed if HUP reload is used with symlink
19
20
  :workers:
20
21
  -
21
22
  :command: '-q default -q high'
@@ -39,6 +40,11 @@ Start pool with a non-default path config
39
40
 
40
41
  Signals `USR1`, `USR2` are forwarded to the children.
41
42
 
43
+ Signal `HUP` to parent starts new children and then stops old.
44
+
45
+ When using symlinked working directory `working_directory` configuration
46
+ option must be used to pick up new code.
47
+
42
48
  ## Contributing
43
49
 
44
50
  Bug reports and pull requests are welcome on GitHub at https://github.com/laurynas/sidekiq-pool.
@@ -14,18 +14,11 @@ module Sidekiq
14
14
  alias_method :run_child, :run
15
15
 
16
16
  def run
17
- @settings = parse_config_file(@pool_config)
18
- @types = @settings[:workers]
19
17
  @master_pid = $$
20
18
 
21
19
  trap_signals
22
20
  update_process_name
23
- @types.each do |type|
24
- type[:amount].times do
25
- sleep @fork_wait || DEFAULT_FORK_WAIT
26
- add_child(type[:command])
27
- end
28
- end
21
+ start_new_pool
29
22
 
30
23
  wait_for_signals
31
24
  end
@@ -48,6 +41,19 @@ module Sidekiq
48
41
 
49
42
  DEFAULT_FORK_WAIT = 1
50
43
 
44
+ def start_new_pool
45
+ logger.info 'Starting new pool'
46
+ @settings = parse_config_file(@pool_config)
47
+ Dir.chdir(@settings[:working_directory]) if @settings[:working_directory]
48
+ @types = @settings[:workers]
49
+ @types.each do |type|
50
+ type[:amount].times do
51
+ sleep @fork_wait || DEFAULT_FORK_WAIT
52
+ add_child(type[:command])
53
+ end
54
+ end
55
+ end
56
+
51
57
  def parse_options(argv)
52
58
  opts = {}
53
59
 
@@ -132,7 +138,7 @@ module Sidekiq
132
138
  def trap_signals
133
139
  @self_read, @self_write = IO.pipe
134
140
 
135
- %w(INT TERM USR1 USR2 CHLD).each do |sig|
141
+ %w(INT TERM USR1 USR2 CHLD HUP).each do |sig|
136
142
  begin
137
143
  trap sig do
138
144
  @self_write.puts(sig) unless fork?
@@ -178,6 +184,23 @@ module Sidekiq
178
184
  when 'USR2'
179
185
  logger.info "Sending #{sig} signal to the pool"
180
186
  signal_to_pool(sig)
187
+ when 'HUP'
188
+ logger.info 'Gracefully reloading pool'
189
+ old_pool = @pool.dup
190
+
191
+ # Signal old pool
192
+ # USR1 tells Sidekiq it will be shutting down in near future.
193
+ signal_to_pool('USR1')
194
+
195
+ # Reset pool
196
+ @pool = []
197
+
198
+ # Start new pool
199
+ start_new_pool
200
+
201
+ # Stop old pool
202
+ stop_children(old_pool)
203
+ logger.info 'Graceful reload completed'
181
204
  end
182
205
  end
183
206
 
@@ -186,9 +209,8 @@ module Sidekiq
186
209
  fork_child(*arg)
187
210
  end
188
211
 
189
-
190
- def signal_to_pool(sig)
191
- @pool.each { |child| signal_to_child(sig, child[:pid]) }
212
+ def signal_to_pool(sig, given_pool = @pool)
213
+ given_pool.each { |child| signal_to_child(sig, child[:pid]) }
192
214
  end
193
215
 
194
216
  def signal_to_child(sig, pid)
@@ -218,7 +240,7 @@ module Sidekiq
218
240
  false
219
241
  end
220
242
 
221
- def stop_children
243
+ def stop_children(given_pool = @pool)
222
244
  @done = true
223
245
  logger.info 'Stopping children'
224
246
  update_process_name
@@ -228,13 +250,13 @@ module Sidekiq
228
250
  wait_time = (Time.now - time).to_i
229
251
  if wait_time > options[:timeout] + 2
230
252
  logger.warn("Children didn't stop in #{wait_time}s, killing")
231
- signal_to_pool('KILL')
253
+ signal_to_pool('KILL', given_pool)
232
254
  else
233
- signal_to_pool('TERM')
255
+ signal_to_pool('TERM', given_pool)
234
256
  end
235
257
  sleep(1)
236
258
  ::Process.waitpid2(-1, ::Process::WNOHANG)
237
- break if @pool.none? { |child| alive?(child[:pid]) }
259
+ break if given_pool.none? { |child| alive?(child[:pid]) }
238
260
  end
239
261
  end
240
262
 
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Pool
3
- VERSION = '1.0.2'
3
+ VERSION = '1.1.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Laurynas Butkus