resque-cluster 0.0.1

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.
@@ -0,0 +1,70 @@
1
+ class TestMemberManager
2
+
3
+ def initialize(local_config_path, global_config_path, cluster_name = "test-cluster", environment = "test")
4
+ @local_config_path = local_config_path
5
+ @global_config_path = global_config_path
6
+ @cluster_name = cluster_name
7
+ @environment = environment
8
+ @pid = nil
9
+ @pool_master_pid = nil
10
+ end
11
+
12
+ def start
13
+ ENV['GRU_HOSTNAME'] = hostname
14
+ @pid = spawn("bundle exec spec/integration/bin/resque-cluster_member_test -c #{@local_config_path} -E #{@environment}#{@cluster_name.nil? ? "" : " -C "+@cluster_name} -G #{@global_config_path}")
15
+
16
+ while ( @pool_master_pid.nil? ) do
17
+ sleep(0.1)
18
+ child_process = @pid #`pgrep -P #{@pid}`.strip
19
+ pool = `ps -p #{child_process} -hf | grep 'resque-pool-master\\[resque-cluster\\]: managing \\[' | awk '{print $1}'`.strip.to_i
20
+ @pool_master_pid = pool > 0 ? pool : nil
21
+ end
22
+ puts "Pool Master pid is ---------- #{@pool_master_pid}"
23
+ end
24
+
25
+ def stop
26
+ puts "************************************************ About to kill : Pool Master pid ---------- #{@pool_master_pid}"
27
+ Process.kill(:TERM, @pool_master_pid)
28
+ while ( @pool_master_pid ) do
29
+ pool = `ps -p #{@pool_master_pid} -hf | grep 'resque-pool-master\\[resque-cluster\\]: managing \\[' | awk '{print $1}'`.strip.to_i
30
+ @pool_master_pid = pool > 0 ? pool : nil
31
+ end
32
+ @pid = nil
33
+ sleep(5)
34
+ end
35
+
36
+ def counts
37
+ return {} unless @pool_master_pid
38
+ local_workers = `ps --ppid #{@pool_master_pid} -fh | awk '{print $8}'`.split
39
+ TestMemberManager.worker_counts(local_workers)
40
+ end
41
+
42
+ def self.counts
43
+ all_workers = `ps -ef | grep "resque-" | grep "Waiting for" | grep -v ps| awk '{print $11}'`.split
44
+ worker_counts(all_workers)
45
+ end
46
+
47
+ def self.worker_counts(worker_array)
48
+ final_counts = Hash.new(0)
49
+
50
+ worker_array.each do |worker|
51
+ final_counts[worker] += 1
52
+ end
53
+
54
+ final_counts
55
+ end
56
+
57
+ def hostname
58
+ @hostname ||= "#{Socket.gethostname}-#{member_count+1}"
59
+ end
60
+
61
+ def self.stop_all
62
+ pools = `ps -ef | grep 'resque-pool-master\\[resque-cluster\\]: managing \\[' | awk '{print $2}'`.split
63
+ `kill #{pools.join(' ')}` unless pools.empty?
64
+ sleep(3)
65
+ end
66
+
67
+ def member_count
68
+ `ps -ef | grep resque-pool-master | grep -v grep|wc -l`.strip.to_i
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ foo: 1
2
+ bar: 9
3
+ "foo,bar,baz": 1
@@ -0,0 +1,100 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ RSpec.describe Resque::Cluster::Member do
4
+ before :all do
5
+ @redis = Redis.new
6
+
7
+ Resque::Cluster.config = {
8
+ cluster_name: 'unit-test-cluster',
9
+ environment: 'unit-test',
10
+ local_config_path: File.expand_path(File.dirname(__FILE__) + '/../local_config.yml'),
11
+ global_config_path: File.expand_path(File.dirname(__FILE__) + '/../global_config.yml')
12
+ }
13
+ @pool = Resque::Pool.new({})
14
+ @member = Resque::Cluster.init(@pool)
15
+ end
16
+
17
+ after :all do
18
+ Resque::Cluster.member.unregister
19
+ Resque::Cluster.config = nil
20
+ Resque::Cluster.member = nil
21
+ end
22
+
23
+ context '#cluster_member_settings' do
24
+ before :all do
25
+ @settings_hash = {
26
+ :cluster_maximums => {'foo' => 2, 'bar' => 50, "foo,bar,baz" => 1},
27
+ :host_maximums => {'foo' => 1, 'bar' => 9, "foo,bar,baz" => 1},
28
+ :client_settings => @redis.client.options,
29
+ :rebalance_flag => false,
30
+ :cluster_name => "unit-test-cluster",
31
+ :environment_name => "unit-test"
32
+ }
33
+ end
34
+
35
+ it 'returns a correct cluster settings hash' do
36
+ expect(Resque::Cluster.member.send(:cluster_member_settings)).to eq(@settings_hash)
37
+ Resque::Cluster.member.unregister
38
+ end
39
+
40
+ it 'returns a correct cluster settings hash with global_config with a rebalance param' do
41
+ Resque::Cluster.config[:global_config_path] = File.expand_path(File.dirname(__FILE__) + '/../global_rebalance_config.yml')
42
+ @settings_hash[:rebalance_flag] = true
43
+ @member = Resque::Cluster.init(@pool)
44
+ expect(Resque::Cluster.member.send(:cluster_member_settings)).to eq(@settings_hash)
45
+ end
46
+ end
47
+
48
+ context '#register' do
49
+ before :all do
50
+ @member.register
51
+ end
52
+
53
+ it 'pings into redis to let the rest of the cluster know of it' do
54
+ expect(@redis.hget('resque:cluster:unit-test-cluster:unit-test', @@hostname)).to_not be_nil
55
+ end
56
+ end
57
+
58
+ context '#check_for_worker_count_adjustment' do
59
+ before :all do
60
+ @redis.hset("GRU:unit-test:unit-test-cluster:global:max_workers","bar",2)
61
+ @redis.hset("GRU:unit-test:unit-test-cluster:#{@@hostname}:max_workers","bar",2)
62
+ @redis.hset("GRU:unit-test:unit-test-cluster:global:workers_running","bar",0)
63
+ @redis.hset("GRU:unit-test:unit-test-cluster:#{@@hostname}:workers_running","bar",0)
64
+ end
65
+
66
+ it 'adjust worker counts if an adjustment exists on the local command queue' do
67
+ 2.times do
68
+ @member.check_for_worker_count_adjustment
69
+ expect(@redis.hget("GRU:unit-test:unit-test-cluster:#{@@hostname}:workers_running", 'foo')).to eq '1'
70
+ expect(@redis.hget("GRU:unit-test:unit-test-cluster:#{@@hostname}:workers_running", 'bar')).to eq '2'
71
+ expect(@member.pool.config['foo']).to eq(1)
72
+ expect(@member.pool.config['bar']).to eq(2)
73
+ end
74
+ end
75
+ end
76
+
77
+ context '#unregister' do
78
+ before :all do
79
+ @member.unregister
80
+ end
81
+
82
+ it 'removes everything about itself from redis' do
83
+ expect(@redis.hget('resque:cluster:unit-test-cluster:unit-test', @@hostname)).to be_nil
84
+ expect(@redis.get("resque:cluster:unit-test-cluster:unit-test:#{@@hostname}:running_workers")).to be_nil
85
+ end
86
+
87
+ it 'moves all the current running workers into the global queue if rebalance_on_termination is set to true' do
88
+ expect(@redis.hget('GRU:unit-test:unit-test-cluster:global:workers_running', 'bar')).to eq '0'
89
+ expect(@redis.hget('GRU:unit-test:unit-test-cluster:global:workers_running', 'foo')).to eq '0'
90
+ expect(@redis.hget("GRU:unit-test:unit-test-cluster:#{@@hostname}:workers_running", 'bar')).to be_nil
91
+ expect(@redis.hget("GRU:unit-test:unit-test-cluster:#{@@hostname}:workers_running", 'foo')).to be_nil
92
+ end
93
+ end
94
+
95
+ after :all do
96
+ @redis.del("GRU:unit-test:unit-test-cluster:global:max_workers")
97
+ @redis.del("GRU:unit-test:unit-test-cluster:global:workers_running")
98
+ end
99
+
100
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'resque/pool/cli_patches'
3
+
4
+ RSpec.describe Resque::Pool::CLI do
5
+ after :all do
6
+ Resque::Cluster.config = nil
7
+ Resque::Cluster.member = nil
8
+ end
9
+
10
+ context "#run" do
11
+ before :each do
12
+ Resque::Cluster.config = nil
13
+ Resque::Cluster.member = nil
14
+ end
15
+
16
+ it "sets up to run resque-pool the standard way if no params are passed in" do
17
+ stub_const("ARGV", [])
18
+ allow(Resque::Pool::CLI).to receive(:start_pool).and_return({ })
19
+ Resque::Pool::CLI.run
20
+ expect(Resque::Cluster.config).to be_nil
21
+ end
22
+
23
+ it "sets up to run resque-pool the standard way if no cluster params are passed in" do
24
+ stub_const("ARGV", ["-c", "spec/local_config.yml", "-E", "test", "-G", "spec/global_config.yml"])
25
+ allow(Resque::Pool::CLI).to receive(:start_pool).and_return({ })
26
+ Resque::Pool::CLI.run
27
+ expect(Resque::Cluster.config).to be_nil
28
+ expect(ENV["RESQUE_POOL_CONFIG"]).to eq("spec/local_config.yml")
29
+ expect(ENV["RESQUE_ENV"]).to eq("test")
30
+ end
31
+
32
+ it "sets up to run resque-pool the cluster way if cluster param is passed in" do
33
+ stub_const("ARGV", ["-c", "spec/local_config.yml", "-E", "test", "-C", "test-cluster", "-G", "spec/global_config.yml"])
34
+ allow(Resque::Pool::CLI).to receive(:start_pool).and_return({ })
35
+ Resque::Pool::CLI.run
36
+ expect(Resque::Cluster.config[:cluster_name]).to eq("test-cluster")
37
+ expect(ENV["RESQUE_POOL_CONFIG"]).to eq("spec/local_config.yml")
38
+ expect(ENV["RESQUE_ENV"]).to eq("test")
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,70 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ RSpec.describe Resque::Pool do
4
+ after :each do
5
+ Resque::Cluster.member.unregister if Resque::Cluster.member
6
+ Resque::Cluster.config = nil
7
+ Resque::Cluster.member = nil
8
+ end
9
+
10
+ context "#run" do
11
+ it "pool config is not empty if running in a non cluster mode after initialization" do
12
+ pool = nil
13
+ ENV['RESQUE_POOL_CONFIG'] = "spec/local_config.yml"
14
+ allow_any_instance_of(Resque::Pool).to receive(:join) {|instance| pool = instance }
15
+ allow_any_instance_of(Resque::Cluster::Member).to receive(:unregister).and_return("")
16
+ Resque::Pool.run
17
+ expect(pool.config['foo']).to eq(1)
18
+ end
19
+
20
+ it "pool config is empty if running in a cluster mode after initialization" do
21
+ pool = nil
22
+ ENV['RESQUE_POOL_CONFIG'] = "spec/local_config.yml"
23
+ allow_any_instance_of(Resque::Pool).to receive(:join) {|instance| pool = instance }
24
+ allow_any_instance_of(Resque::Cluster::Member).to receive(:unregister).and_return("")
25
+
26
+ Resque::Cluster.config = {:cluster_name=>"unit-test-cluster",
27
+ :environment=>"unit-test",
28
+ :local_config_path=>"spec/local_config.yml",
29
+ :global_config_path=>"spec/global_config.yml",
30
+ :rebalance=>true}
31
+
32
+ Resque::Pool.run
33
+ expect(pool.config).to be_empty
34
+ end
35
+
36
+ end
37
+
38
+ context "#maintain_worker_count" do
39
+ it "should adjust current number of workers" do
40
+ pool = nil
41
+ ENV['RESQUE_POOL_CONFIG'] = "spec/local_config.yml"
42
+ allow_any_instance_of(Resque::Pool).to receive(:join) {|instance| pool = instance }
43
+ allow_any_instance_of(Resque::Cluster::Member).to receive(:unregister).and_return("")
44
+
45
+ Resque::Cluster.config = {:cluster_name=>"unit-test-cluster",
46
+ :environment=>"unit-test",
47
+ :local_config_path=>"spec/local_config.yml",
48
+ :global_config_path=>"spec/global_config.yml",
49
+ :rebalance=>true}
50
+
51
+ Resque::Pool.run
52
+ expect(pool.config).to be_empty
53
+ pool.maintain_worker_count
54
+ expect(pool.config['foo']).to eq(1)
55
+ expect(pool.config['bar']).to eq(9)
56
+ expect(pool.config['foo,bar,baz']).to eq(1)
57
+ end
58
+ end
59
+
60
+ after :all do
61
+ @redis = Redis.new
62
+ @redis.del("resque:cluster:unit-test-cluster:unit-test:#{@@hostname}:running_workers")
63
+ @redis.del("GRU:unit-test:unit-test-cluster:#{@@hostname}:max_workers")
64
+ @redis.del("GRU:unit-test:unit-test-cluster:#{@@hostname}:workers_running")
65
+ @redis.del("GRU:unit-test:unit-test-cluster:global:max_workers")
66
+ @redis.del("GRU:unit-test:unit-test-cluster:global:workers_running")
67
+ @redis.del("resque:cluster:unit-test-cluster:unit-test")
68
+ end
69
+
70
+ end
@@ -0,0 +1,13 @@
1
+ gem 'resque-pool'
2
+ require 'pry'
3
+ require 'resque/pool/cli'
4
+ require 'rspec'
5
+ require 'socket'
6
+
7
+ $LOAD_PATH << File.expand_path('lib/resque/*', File.dirname(__FILE__))
8
+ $LOAD_PATH << File.expand_path('lib/resque/pool/*', File.dirname(__FILE__))
9
+
10
+ require 'resque/cluster'
11
+ require 'resque/pool/patches'
12
+
13
+ @@hostname = Socket.gethostname
metadata ADDED
@@ -0,0 +1,249 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-cluster
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yasha Portnoy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: resque-pool
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: gru
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: awesome_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.1.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rdoc
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.12'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.12'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.7'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.7'
111
+ - !ruby/object:Gem::Dependency
112
+ name: jeweler
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 2.0.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 2.0.1
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.31'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.31'
153
+ - !ruby/object:Gem::Dependency
154
+ name: mock_redis
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 0.15.0
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 0.15.0
167
+ description: A management tool for resque workers. Allows spinning up and managing
168
+ resque workers across multiple machines sharing the same Redis server
169
+ email: yash.portnoy@gmail.com
170
+ executables:
171
+ - resque-cluster_member
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".document"
176
+ - ".gitignore"
177
+ - ".rspec"
178
+ - ".rubocop.yml"
179
+ - Gemfile
180
+ - Gemfile.lock
181
+ - LICENSE.txt
182
+ - README.rdoc
183
+ - Rakefile
184
+ - bin/resque-cluster_member
185
+ - lib/resque/cluster.rb
186
+ - lib/resque/cluster/member.rb
187
+ - lib/resque/cluster/version.rb
188
+ - lib/resque/pool/cli_patches.rb
189
+ - lib/resque/pool/patches.rb
190
+ - resque-cluster.gemspec
191
+ - spec/global_config.yml
192
+ - spec/global_rebalance_config.yml
193
+ - spec/integration/bin/resque-cluster_member_test
194
+ - spec/integration/cluster_spec.rb
195
+ - spec/integration/config/global_config.yml
196
+ - spec/integration/config/global_config2.yml
197
+ - spec/integration/config/global_rebalance_config2.yml
198
+ - spec/integration/config/local_config.yml
199
+ - spec/integration/config/local_config2.yml
200
+ - spec/integration/spec_helper.rb
201
+ - spec/integration/standalone_spec.rb
202
+ - spec/integration/test_member_manager.rb
203
+ - spec/local_config.yml
204
+ - spec/unit/member_spec.rb
205
+ - spec/unit/resque_pool_cli_patches_spec.rb
206
+ - spec/unit/resque_pool_patches_spec.rb
207
+ - spec/unit/spec_helper.rb
208
+ homepage: https://github.com/yportnoy/resque-cluster
209
+ licenses:
210
+ - MIT
211
+ metadata: {}
212
+ post_install_message:
213
+ rdoc_options: []
214
+ require_paths:
215
+ - lib
216
+ required_ruby_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ required_rubygems_version: !ruby/object:Gem::Requirement
222
+ requirements:
223
+ - - ">="
224
+ - !ruby/object:Gem::Version
225
+ version: '0'
226
+ requirements: []
227
+ rubyforge_project:
228
+ rubygems_version: 2.4.8
229
+ signing_key:
230
+ specification_version: 4
231
+ summary: Creates and manages resque worker in a distributed cluster
232
+ test_files:
233
+ - spec/global_config.yml
234
+ - spec/global_rebalance_config.yml
235
+ - spec/integration/bin/resque-cluster_member_test
236
+ - spec/integration/cluster_spec.rb
237
+ - spec/integration/config/global_config.yml
238
+ - spec/integration/config/global_config2.yml
239
+ - spec/integration/config/global_rebalance_config2.yml
240
+ - spec/integration/config/local_config.yml
241
+ - spec/integration/config/local_config2.yml
242
+ - spec/integration/spec_helper.rb
243
+ - spec/integration/standalone_spec.rb
244
+ - spec/integration/test_member_manager.rb
245
+ - spec/local_config.yml
246
+ - spec/unit/member_spec.rb
247
+ - spec/unit/resque_pool_cli_patches_spec.rb
248
+ - spec/unit/resque_pool_patches_spec.rb
249
+ - spec/unit/spec_helper.rb