sidekiq-pool 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -2
- data/lib/sidekiq/pool/cli.rb +63 -35
- data/lib/sidekiq/pool/version.rb +1 -1
- data/sidekiq-pool.gemspec +1 -1
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b18df834e83b27a7507863976c2af09c93fea20d
|
4
|
+
data.tar.gz: 197cf40e14dda748bf363f33324fc753878de775
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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).
|
data/lib/sidekiq/pool/cli.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
118
|
-
|
136
|
+
stop_children
|
137
|
+
logger.info 'Bye!'
|
138
|
+
exit(0)
|
119
139
|
when 'TTIN'
|
120
|
-
|
121
|
-
fork_child
|
140
|
+
add_child
|
122
141
|
when 'TTOU'
|
123
142
|
remove_child
|
124
143
|
when 'CLD'
|
125
144
|
check_pool
|
126
|
-
|
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
|
132
|
-
|
133
|
-
|
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
|
-
|
165
|
+
signal_to_child('TERM', @pool.shift)
|
148
166
|
end
|
149
167
|
|
150
168
|
def signal_to_pool(sig)
|
151
|
-
|
152
|
-
|
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
|
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
|
-
|
181
|
-
|
208
|
+
def fork?
|
209
|
+
::Process.pid != @master_pid
|
182
210
|
end
|
183
211
|
end
|
184
212
|
end
|
data/lib/sidekiq/pool/version.rb
CHANGED
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
|
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.
|
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
|
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:
|