sidekiq-pool 0.1.1 → 0.1.2

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: 776f60e5732aaa7c89050bc670fd2d484460ae16
4
- data.tar.gz: fd47cc8507e4b72fdfc00895b9632eead8abbc00
3
+ metadata.gz: b18df834e83b27a7507863976c2af09c93fea20d
4
+ data.tar.gz: 197cf40e14dda748bf363f33324fc753878de775
5
5
  SHA512:
6
- metadata.gz: b7fb8b535f3eb9b3197e43ab256f5cf140f9fc73bcaf8d51288959a80cd5afaf40fca850eb547f5ac77a37c2616b7e1910c831e43e7602819d60c24cc4be3c60
7
- data.tar.gz: 2d49c444e7f49f45941858b7c38912731a042b70a2c584e4ef57fcb505f19a01b0d25cb9160875f208f28285021d5dc408c4f84b5367300b0c52be1c94645720
6
+ metadata.gz: 7fa18f3e36552979d1d0ae685323ef145fb3cdf5e8ce2eb3f8ba9825c8001829bf46b57481d9daffba77cbcb65262da67818416ed68352fde1e2436fe272b5bf
7
+ data.tar.gz: c4ccef02df6cfa61e0ef0263769e987953107b3949faaad1d8a0ee22351fdca799b0cb439b41c1517b63580f8586d6bc3fb17bfb1b496e63db73b3db2ec9c89a
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Sidekiq::Pool
2
2
 
3
- Allows using more CPU cores with Sidekiq on Ruby MRI by forking multiple processes.
3
+ Allows Sidekiq using more CPU cores on Ruby MRI by forking multiple processes.
4
4
 
5
5
  ## Installation
6
6
 
@@ -36,7 +36,6 @@ Signals `USR1`, `USR2` are forwarded to the children.
36
36
 
37
37
  Bug reports and pull requests are welcome on GitHub at https://github.com/laurynas/sidekiq-pool.
38
38
 
39
-
40
39
  ## License
41
40
 
42
41
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -4,6 +4,7 @@ module Sidekiq
4
4
  module Pool
5
5
  class CLI < Sidekiq::CLI
6
6
  def initialize
7
+ @master_pid = ::Process.pid
7
8
  @child_index = 0
8
9
  @pool = []
9
10
  super
@@ -12,28 +13,12 @@ module Sidekiq
12
13
  alias_method :run_child, :run
13
14
 
14
15
  def run
15
- raise ArgumentError, 'Please specify pool size' unless @pool_size
16
-
17
- self_read, self_write = IO.pipe
18
-
19
- %w(INT TERM USR1 USR2 TTIN TTOU CLD).each do |sig|
20
- begin
21
- trap sig do
22
- self_write.puts(sig)
23
- end
24
- rescue ArgumentError
25
- puts "Signal #{sig} not supported"
26
- end
27
- end
28
-
29
16
  logger.info "Starting pool with #{@pool_size} instances"
17
+ trap_signals
30
18
 
31
19
  @pool_size.times { fork_child }
32
20
 
33
- while readable_io = IO.select([self_read])
34
- signal = readable_io.first[0].gets.strip
35
- handle_master_signal(signal)
36
- end
21
+ wait_for_signals
37
22
  end
38
23
 
39
24
  private
@@ -111,28 +96,61 @@ module Sidekiq
111
96
  opts
112
97
  end
113
98
 
99
+ def validate!
100
+ raise ArgumentError, 'Please specify pool size using --pool-size N' unless @pool_size
101
+ super
102
+ end
103
+
104
+ def trap_signals
105
+ @self_read, @self_write = IO.pipe
106
+
107
+ %w(INT TERM USR1 USR2 TTIN TTOU CLD).each do |sig|
108
+ begin
109
+ trap sig do
110
+ @self_write.puts(sig) unless fork?
111
+ end
112
+ rescue ArgumentError
113
+ puts "Signal #{sig} not supported"
114
+ end
115
+ end
116
+ end
117
+
118
+ def fork_child
119
+ @pool << fork do
120
+ @self_write.close
121
+ options[:index] = @child_index++
122
+ run_child
123
+ end
124
+ end
125
+
126
+ def wait_for_signals
127
+ while readable_io = IO.select([@self_read])
128
+ signal = readable_io.first[0].gets.strip
129
+ handle_master_signal(signal)
130
+ end
131
+ end
132
+
114
133
  def handle_master_signal(sig)
115
134
  case sig
116
135
  when 'INT', 'TERM'
117
- signal_to_pool(sig)
118
- wait_and_exit
136
+ stop_children
137
+ logger.info 'Bye!'
138
+ exit(0)
119
139
  when 'TTIN'
120
- logger.info 'Adding child'
121
- fork_child
140
+ add_child
122
141
  when 'TTOU'
123
142
  remove_child
124
143
  when 'CLD'
125
144
  check_pool
126
- else
145
+ when 'USR1', 'USR2'
146
+ logger.info "Sending #{sig} signal to the pool"
127
147
  signal_to_pool(sig)
128
148
  end
129
149
  end
130
150
 
131
- def fork_child
132
- @pool << fork do
133
- options[:index] = @child_index++
134
- run_child
135
- end
151
+ def add_child
152
+ logger.info 'Adding child'
153
+ fork_child
136
154
  end
137
155
 
138
156
  def remove_child
@@ -144,12 +162,17 @@ module Sidekiq
144
162
  end
145
163
 
146
164
  logger.info 'Removing child'
147
- ::Process.kill('TERM', @pool.pop)
165
+ signal_to_child('TERM', @pool.shift)
148
166
  end
149
167
 
150
168
  def signal_to_pool(sig)
151
- logger.info "Sending #{sig} signal to the pool"
152
- @pool.each { |pid| ::Process.kill(sig, pid) }
169
+ @pool.each { |pid| signal_to_child(sig, pid) }
170
+ end
171
+
172
+ def signal_to_child(sig, pid)
173
+ ::Process.kill(sig, pid)
174
+ rescue Errno::ESRCH
175
+ @pool.delete(pid)
153
176
  end
154
177
 
155
178
  def check_pool
@@ -162,6 +185,7 @@ module Sidekiq
162
185
  def handle_dead_child(pid)
163
186
  logger.info "Child #{pid} died"
164
187
  @pool.delete(pid)
188
+ add_child
165
189
  end
166
190
 
167
191
  def alive?(pid)
@@ -171,14 +195,18 @@ module Sidekiq
171
195
  false
172
196
  end
173
197
 
174
- def wait_and_exit
198
+ def stop_children
199
+ logger.info 'Stopping children'
200
+
175
201
  loop do
202
+ signal_to_pool('TERM')
203
+ sleep(1)
176
204
  break if @pool.none? { |pid| alive?(pid) }
177
- sleep(0.1)
178
205
  end
206
+ end
179
207
 
180
- logger.info 'Bye!'
181
- exit(0)
208
+ def fork?
209
+ ::Process.pid != @master_pid
182
210
  end
183
211
  end
184
212
  end
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Pool
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
data/sidekiq-pool.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ['laurynas.butkus@gmail.com']
11
11
 
12
12
  spec.summary = %q{Forks and manages multiple Sidekiq processes}
13
- spec.description = %q{Allows using more CPU cores with Sidekiq on Ruby MRI by forking multiple processes.}
13
+ spec.description = %q{Allows Sidekiq using more CPU cores on Ruby MRI by forking multiple processes.}
14
14
  spec.homepage = 'https://github.com/laurynas/sidekiq-pool'
15
15
  spec.license = 'MIT'
16
16
 
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: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Laurynas Butkus
@@ -66,8 +66,7 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
- description: Allows using more CPU cores with Sidekiq on Ruby MRI by forking multiple
70
- processes.
69
+ description: Allows Sidekiq using more CPU cores on Ruby MRI by forking multiple processes.
71
70
  email:
72
71
  - laurynas.butkus@gmail.com
73
72
  executables: