sidekiq-pool 0.1.7 → 1.0.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: 8a2728fe9ae5b84fa988903d628cbad0f324c36f
4
- data.tar.gz: 4e52dd10fb17ec666e0a6d88adb285fb193df8e3
3
+ metadata.gz: e74bb16b2feb36fa68a28e744185d26383037361
4
+ data.tar.gz: 9a00a58fca89b6fb52f389f88ceb29d65a6d6e61
5
5
  SHA512:
6
- metadata.gz: 030ad8a026626669776b71838b70f5a0a0146feb96e84b851bd3104b773df9b795686a831c5e513a0356ca51fd13d9ffe64517f1f6cdc40df6ee01273d05b663
7
- data.tar.gz: 3ab4d8c7af989d4cc2f597b9f7574d434fb57b2ff405592af343c3c499cdb145b2fcc449dfcbf09a4202e245baeafefe9d97db1a81a78a07e3766e1aff5f68d6
6
+ metadata.gz: 1cbccc3d4aab0e5ef4c6e8d50fd001ced4efb6b67cfa74e2d23508f6e986e8fc72ec75d9e33cef23433c41c5ffc82d4e5b3ab237a6597193f05c373198627eaa
7
+ data.tar.gz: 70dd6d7a38ade7d05470aff93906659cb4aca5944f77ec00c786b07c3c14dc58d45d3f2fbe0bedc784320ec1c48370afc77bdf6a4245a796b9a6f1f12bc34cb4
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Sidekiq::Pool
2
2
 
3
3
  Allows Sidekiq using more CPU cores on Ruby MRI by forking multiple processes.
4
+ Also adds an option to use different command line option workers in the same pool.
4
5
 
5
6
  ## Installation
6
7
 
@@ -10,28 +11,34 @@ Add this line to your application's Gemfile:
10
11
  gem 'sidekiq-pool'
11
12
  ```
12
13
 
14
+ Create a config file and specify it's path with the *p* command line option (the default is config/sidekiq-pool.yml)
15
+
16
+ Paste the following config and modify it to your needs:
17
+
18
+ ```yaml
19
+ :workers:
20
+ -
21
+ :command: '-q default -q high'
22
+ :amount: 2
23
+ -
24
+ :command: '-q high -L high_logfile.txt'
25
+ :amount: 1
26
+ ```
27
+
13
28
  ## Usage
14
29
 
15
30
  Help
16
31
 
17
32
  $ bundle exec sidekiq-pool -h
18
33
 
19
- Start pool with 3 instances
34
+ Start pool with a non-default path config
20
35
 
21
- $ bundle exec sidekiq-pool --pool-size 3
36
+ $ bundle exec sidekiq-pool -p config/pool_config.yml
22
37
 
23
38
  ## Signals
24
39
 
25
40
  Signals `USR1`, `USR2` are forwarded to the children.
26
-
27
- `TTIN` forks new child
28
-
29
- $ kill -TTIN masterpid
30
-
31
- `TTOU` stops one child
32
-
33
- $ kill -TTOU masterpid
34
-
41
+
35
42
  ## Contributing
36
43
 
37
44
  Bug reports and pull requests are welcome on GitHub at https://github.com/laurynas/sidekiq-pool.
@@ -14,21 +14,36 @@ module Sidekiq
14
14
  alias_method :run_child, :run
15
15
 
16
16
  def run
17
- logger.info "Starting pool with #{@pool_size} instances"
18
-
17
+ @settings = parse_config_file(@pool_config)
18
+ @types = @settings[:workers]
19
19
  @master_pid = $$
20
20
 
21
21
  trap_signals
22
22
  update_process_name
23
-
24
- @pool_size.times do
25
- sleep @fork_wait || DEFAULT_FORK_WAIT
26
- fork_child
23
+ @types.each do |type|
24
+ type[:amount].times do
25
+ sleep @fork_wait || DEFAULT_FORK_WAIT
26
+ add_child(type[:command])
27
+ end
27
28
  end
28
29
 
29
30
  wait_for_signals
30
31
  end
31
32
 
33
+ def parse_config_file(filename)
34
+ config = YAML.load(ERB.new(File.read(filename)).result)
35
+ unless config.key?(:workers)
36
+ raise ArgumentError, 'Invalid configuration file - "workers" key must be present'
37
+ end
38
+ unless config[:workers].is_a?(Array)
39
+ raise ArgumentError, 'Invalid configuration file - "workers" key must be a list'
40
+ end
41
+ unless config[:workers].size > 0
42
+ raise ArgumentError, 'Invalid configuration file - Atleast one worker must be present'
43
+ end
44
+ config
45
+ end
46
+
32
47
  private
33
48
 
34
49
  DEFAULT_FORK_WAIT = 1
@@ -37,10 +52,6 @@ module Sidekiq
37
52
  opts = {}
38
53
 
39
54
  @parser = OptionParser.new do |o|
40
- o.on '--pool-size INT', "pool size" do |arg|
41
- @pool_size = Integer(arg)
42
- end
43
-
44
55
  o.on '-c', '--concurrency INT', "processor threads to use" do |arg|
45
56
  opts[:concurrency] = Integer(arg)
46
57
  end
@@ -90,6 +101,10 @@ module Sidekiq
90
101
  opts[:pidfile] = arg
91
102
  end
92
103
 
104
+ o.on '-p', '--pool-config PATH', "path to pool config file" do |arg|
105
+ @pool_config = arg
106
+ end
107
+
93
108
  o.on '-V', '--version', "Print version and exit" do |arg|
94
109
  puts "Sidekiq #{Sidekiq::VERSION}"
95
110
  die(0)
@@ -107,18 +122,17 @@ module Sidekiq
107
122
  opts[:config_file] ||= filename if File.exist?(filename)
108
123
  end
109
124
 
110
- opts
111
- end
125
+ %w[config/sidekiq-pool.yml config/sidekiq-pool.yml.erb].each do |filename|
126
+ @pool_config ||= filename if File.exist?(filename)
127
+ end
112
128
 
113
- def validate!
114
- raise ArgumentError, 'Please specify pool size using --pool-size N' unless @pool_size
115
- super
129
+ opts
116
130
  end
117
131
 
118
132
  def trap_signals
119
133
  @self_read, @self_write = IO.pipe
120
134
 
121
- %w(INT TERM USR1 USR2 TTIN TTOU CHLD).each do |sig|
135
+ %w(INT TERM USR1 USR2 CHLD).each do |sig|
122
136
  begin
123
137
  trap sig do
124
138
  @self_write.puts(sig) unless fork?
@@ -129,13 +143,15 @@ module Sidekiq
129
143
  end
130
144
  end
131
145
 
132
- def fork_child
133
- @pool << fork do
146
+ def fork_child(command)
147
+ pid = fork do
148
+ setup_options(command.split)
134
149
  @self_write.close
135
150
  $0 = 'sidekiq starting'
136
151
  options[:index] = @child_index++
137
152
  run_child
138
153
  end
154
+ @pool << { pid: pid, command: command }
139
155
  end
140
156
 
141
157
  def wait_for_signals
@@ -151,42 +167,25 @@ module Sidekiq
151
167
  stop_children
152
168
  logger.info 'Bye!'
153
169
  exit(0)
154
- when 'TTIN'
155
- add_child
156
- when 'TTOU'
157
- remove_child
158
170
  when 'CHLD'
159
171
  check_pool
160
172
  when 'USR1'
161
173
  @done = true
162
174
  update_process_name
163
- logger.info "Sending #{sig} signal to the pool"
164
- signal_to_pool(sig)
165
- when 'USR2'
175
+ when 'USR1', 'USR2'
166
176
  logger.info "Sending #{sig} signal to the pool"
167
177
  signal_to_pool(sig)
168
178
  end
169
179
  end
170
180
 
171
- def add_child
172
- logger.info 'Adding child'
173
- fork_child
181
+ def add_child(*arg)
182
+ logger.info "Adding child with args: #{arg}"
183
+ fork_child(*arg)
174
184
  end
175
185
 
176
- def remove_child
177
- return if @pool.empty?
178
-
179
- if @pool.size == 1
180
- logger.info 'Cowardly refusing to kill the last child'
181
- return
182
- end
183
-
184
- logger.info 'Removing child'
185
- signal_to_child('TERM', @pool.shift)
186
- end
187
186
 
188
187
  def signal_to_pool(sig)
189
- @pool.each { |pid| signal_to_child(sig, pid) }
188
+ @pool.each { |child| signal_to_child(sig, child[:pid]) }
190
189
  end
191
190
 
192
191
  def signal_to_child(sig, pid)
@@ -197,16 +196,16 @@ module Sidekiq
197
196
 
198
197
  def check_pool
199
198
  ::Process.waitpid2(-1, ::Process::WNOHANG)
200
- @pool.each do |pid|
201
- next if alive?(pid)
202
- handle_dead_child(pid)
199
+ @pool.each do |child|
200
+ next if alive?(child[:pid])
201
+ handle_dead_child(child)
203
202
  end
204
203
  end
205
204
 
206
- def handle_dead_child(pid)
207
- logger.info "Child #{pid} died"
208
- @pool.delete(pid)
209
- add_child
205
+ def handle_dead_child(child)
206
+ logger.info "Child #{child[:pid]} died"
207
+ @pool.delete(child)
208
+ add_child(child[:command])
210
209
  end
211
210
 
212
211
  def alive?(pid)
@@ -232,7 +231,7 @@ module Sidekiq
232
231
  end
233
232
  sleep(1)
234
233
  ::Process.waitpid2(-1, ::Process::WNOHANG)
235
- break if @pool.none? { |pid| alive?(pid) }
234
+ break if @pool.none? { |child| alive?(child[:pid]) }
236
235
  end
237
236
  end
238
237
 
@@ -1,5 +1,5 @@
1
1
  module Sidekiq
2
2
  module Pool
3
- VERSION = '0.1.7'
3
+ VERSION = '1.0.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidekiq-pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Laurynas Butkus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-14 00:00:00.000000000 Z
11
+ date: 2016-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq