heroku_resque_autoscaler 0.0.1 → 0.0.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.
data/README.md CHANGED
@@ -8,7 +8,7 @@ Inspired by [@darkhelmet](https://github.com/darkhelmet)'s
8
8
 
9
9
  ## Current Version
10
10
 
11
- 0.0.1
11
+ 0.0.2
12
12
 
13
13
 
14
14
  ## Requirements
@@ -8,15 +8,15 @@ Gem::Specification.new do |gem|
8
8
  gem.email = ["jlsuttles@gmail.com"]
9
9
  gem.description = %q{Uses Resque Job Hooks and the Heroku API gem to autoscale Heroku Resque workers}
10
10
  gem.summary = %q{Uses Resque Job Hooks and the Heroku API gem to autoscale Heroku Resque workers}
11
- gem.homepage = ""
11
+ gem.homepage = "https://github.com/G5/heroku_resque_autoscaler"
12
12
 
13
13
  gem.files = `git ls-files`.split($\)
14
14
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
15
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
16
  gem.require_paths = ["lib"]
17
17
 
18
- gem.add_runtime_dependency "heroku-api", "~> 0.3.5"
19
- gem.add_runtime_dependency "resque", "~> 1.23.0"
18
+ gem.add_dependency "heroku-api", "~> 0.3.5"
19
+ gem.add_dependency "resque", "~> 1.23.0"
20
20
 
21
21
  gem.add_development_dependency "rspec", "~> 2.11.0"
22
22
  gem.add_development_dependency "guard-rspec", "~> 2.1.0"
@@ -3,6 +3,14 @@ require "heroku_resque_autoscaler/configuration"
3
3
  require "heroku_resque_autoscaler/scaler"
4
4
 
5
5
  module HerokuResqueAutoscaler
6
+ WORKER_SCALE = [
7
+ { :workers => 1, :job_count => 1 },
8
+ { :workers => 2, :job_count => 15 },
9
+ { :workers => 3, :job_count => 25 },
10
+ { :workers => 4, :job_count => 40 },
11
+ { :workers => 5, :job_count => 60 }
12
+ ]
13
+
6
14
  class << self
7
15
  # A HerokuResqueAutoscaler configuration object. Must act like a hash and
8
16
  # return sensible values for all HerokuResqueAutoscaler configuration options.
@@ -28,4 +36,24 @@ module HerokuResqueAutoscaler
28
36
  yield(configuration)
29
37
  end
30
38
  end
39
+
40
+ def after_enqueue_scale_up(*args)
41
+ desired_workers = num_desired_heroku_workers
42
+ if Scaler.workers < desired_workers
43
+ Scaler.workers = desired_workers
44
+ end
45
+ end
46
+
47
+ def after_perform_scale_down(*args)
48
+ # Scale everything down if we have no pending jobs and one working job (this one)
49
+ Scaler.workers = 0 if Scaler.job_count.zero? && Scaler.working_job_count == 1
50
+ end
51
+
52
+ def num_desired_heroku_workers(*args)
53
+ WORKER_SCALE.reverse_each do |scale_info|
54
+ if Scaler.job_count >= scale_info[:job_count]
55
+ return scale_info[:workers]
56
+ end
57
+ end
58
+ end
31
59
  end
@@ -4,8 +4,9 @@ require "resque"
4
4
  module HerokuResqueAutoscaler
5
5
  module Scaler
6
6
  class << self
7
- # TODO: use HerokuResqueAutoscaler.configuration for api_key
8
- @@heroku = Heroku::API.new(api_key: ENV["HEROKU_API_KEY"])
7
+ def heroku
8
+ @heroku ||= Heroku::API.new(api_key: api_key)
9
+ end
9
10
 
10
11
  def api_key
11
12
  HerokuResqueAutoscaler.configuration["heroku_api_key"]
@@ -16,13 +17,13 @@ module HerokuResqueAutoscaler
16
17
  end
17
18
 
18
19
  def workers
19
- @@heroku.get_ps(app_name).body.keep_if do |ps|
20
+ heroku.get_ps(app_name).body.keep_if do |ps|
20
21
  ps["process"] =~ /worker/
21
22
  end.length.to_i
22
23
  end
23
24
 
24
25
  def workers=(qty)
25
- @@heroku.post_ps_scale(app_name, :worker, qty)
26
+ heroku.post_ps_scale(app_name, :worker, qty)
26
27
  end
27
28
 
28
29
  def job_count
@@ -34,45 +35,4 @@ module HerokuResqueAutoscaler
34
35
  end
35
36
  end
36
37
  end
37
-
38
- def after_perform_scale_down(*args)
39
- # Scale everything down if we have no pending jobs and one working job (this one)
40
- Scaler.workers = 0 if Scaler.job_count.zero? && Scaler.working_job_count == 1
41
- end
42
-
43
- def num_desired_heroku_workers(*args)
44
- [
45
- {
46
- :workers => 1, # This many workers
47
- :job_count => 1 # For this many jobs or more, until the next level
48
- },
49
- {
50
- :workers => 2,
51
- :job_count => 15
52
- },
53
- {
54
- :workers => 3,
55
- :job_count => 25
56
- },
57
- {
58
- :workers => 4,
59
- :job_count => 40
60
- },
61
- {
62
- :workers => 5,
63
- :job_count => 60
64
- }
65
- ].reverse_each do |scale_info|
66
- if Scaler.job_count >= scale_info[:job_count]
67
- return scale_info[:workers]
68
- end
69
- end
70
- end
71
-
72
- def after_enqueue_scale_up(*args)
73
- desired_workers = num_desired_heroku_workers
74
- if Scaler.workers < desired_workers
75
- Scaler.workers = desired_workers
76
- end
77
- end
78
38
  end
@@ -1,3 +1,3 @@
1
1
  module HerokuResqueAutoscaler
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -11,18 +11,25 @@ end
11
11
  describe HerokuResqueAutoscaler do
12
12
  before :each do
13
13
  @heroku = mock(Heroku::API)
14
- HerokuResqueAutoscaler::Scaler.class_variable_set(:@@heroku, @heroku)
14
+ HerokuResqueAutoscaler::Scaler.stub(:heroku).and_return(@heroku)
15
15
  end
16
16
 
17
17
  let(:heroku_app) {ENV['HEROKU_APP_NAME']}
18
18
 
19
19
  context "#workers" do
20
20
  it "returns the number of workers from the Heroku application" do
21
- @heroku.should_receive(:get_ps).with(heroku_app).and_return([
22
- {"process" => "web.1"},
23
- {"process" => "worker.1"},
24
- {"process" => "worker.1"},
25
- ])
21
+ Excon.stub(method: :get) do |params|
22
+ { status: 200,
23
+ body: [
24
+ {"process" => "web.1"},
25
+ {"process" => "worker.1"},
26
+ {"process" => "worker.2"},
27
+ ]
28
+ }
29
+ end
30
+ connection = Excon.new("http://example.com", mock: true)
31
+ response = connection.request(method: :get)
32
+ @heroku.should_receive(:get_ps).with(heroku_app).and_return(response)
26
33
  HerokuResqueAutoscaler::Scaler.workers.should == 2
27
34
  end
28
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heroku_resque_autoscaler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-24 00:00:00.000000000 Z
12
+ date: 2012-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: heroku-api
@@ -130,7 +130,7 @@ files:
130
130
  - spec/lib/heroku_resque_autoscaler/scaler_spec.rb
131
131
  - spec/lib/heroku_resque_autoscaler_spec.rb
132
132
  - spec/spec_helper.rb
133
- homepage: ''
133
+ homepage: https://github.com/G5/heroku_resque_autoscaler
134
134
  licenses: []
135
135
  post_install_message:
136
136
  rdoc_options: []
@@ -144,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
144
144
  version: '0'
145
145
  segments:
146
146
  - 0
147
- hash: -4159626300690751562
147
+ hash: 1281113482684956416
148
148
  required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  version: '0'
154
154
  segments:
155
155
  - 0
156
- hash: -4159626300690751562
156
+ hash: 1281113482684956416
157
157
  requirements: []
158
158
  rubyforge_project:
159
159
  rubygems_version: 1.8.24