heroku-resque-auto-scale 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ gem 'resque', "~> 1.10.0"
9
+ gem 'heroku' # You will need the heroku gem for this too.
10
+
11
+ group :development do
12
+ gem "shoulda", ">= 0"
13
+ gem "bundler", "~> 1.0.0"
14
+ gem "jeweler", "~> 1.5.2"
15
+ gem "rcov", ">= 0"
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ configuration (1.1.0)
5
+ git (1.2.5)
6
+ heroku (1.11.0)
7
+ json_pure (>= 1.2.0, < 1.5.0)
8
+ launchy (~> 0.3.2)
9
+ rest-client (>= 1.4.0, < 1.7.0)
10
+ jeweler (1.5.2)
11
+ bundler (~> 1.0.0)
12
+ git (>= 1.2.5)
13
+ rake
14
+ json (1.4.6)
15
+ json_pure (1.4.6)
16
+ launchy (0.3.7)
17
+ configuration (>= 0.0.5)
18
+ rake (>= 0.8.1)
19
+ mime-types (1.16)
20
+ rack (1.2.1)
21
+ rake (0.8.7)
22
+ rcov (0.9.9)
23
+ redis (2.1.1)
24
+ redis-namespace (0.8.0)
25
+ redis (< 3.0.0)
26
+ resque (1.10.0)
27
+ json (~> 1.4.6)
28
+ redis-namespace (~> 0.8.0)
29
+ sinatra (>= 0.9.2)
30
+ vegas (~> 0.1.2)
31
+ rest-client (1.6.1)
32
+ mime-types (>= 1.16)
33
+ shoulda (2.11.3)
34
+ sinatra (1.1.0)
35
+ rack (~> 1.1)
36
+ tilt (~> 1.1)
37
+ tilt (1.1)
38
+ vegas (0.1.8)
39
+ rack (>= 1.0.0)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ bundler (~> 1.0.0)
46
+ heroku
47
+ jeweler (~> 1.5.2)
48
+ rcov
49
+ resque (~> 1.10.0)
50
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Mark Quezada
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ = heroku-resque-auto-scale
2
+
3
+ Auto scale your resque workers on Heroku. Original code by darkhelmet:
4
+
5
+ http://blog.darkhax.com/2010/07/30/auto-scale-your-resque-workers-on-heroku
6
+ https://gist.github.com/501160
7
+
8
+ I just bundled it into a gem for easy inclusion into other projects.
9
+
10
+ = Usage
11
+
12
+ Once the gem is installed, simply extend your job class as follows:
13
+
14
+ class ScalingJob
15
+ extend HerokuResqueAutoScale if Rails.env.production? # only extend in production
16
+
17
+ def self.perform
18
+ # Do something long running
19
+ end
20
+ end
21
+
22
+ == Contributing to heroku-resque-auto-scale
23
+
24
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
25
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
26
+ * Fork the project
27
+ * Start a feature/bugfix branch
28
+ * Commit and push until you are happy with your contribution
29
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
30
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
31
+
32
+ == Copyright
33
+
34
+ Copyright (c) 2010 Mark Quezada. See LICENSE.txt for
35
+ further details.
36
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "heroku-resque-auto-scale"
16
+ gem.homepage = "http://github.com/mirthlab/heroku-resque-auto-scale"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Auto scale your resque workers on Heroku.}
19
+ gem.description = %Q{Auto scale your resque workers on Heroku.}
20
+ gem.email = "mark@mirthlab.com"
21
+ gem.authors = ["Mark Quezada"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "heroku-resque-auto-scale #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,70 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{heroku-resque-auto-scale}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mark Quezada"]
12
+ s.date = %q{2010-12-20}
13
+ s.description = %q{Auto scale your resque workers on Heroku.}
14
+ s.email = %q{mark@mirthlab.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "heroku-resque-auto-scale.gemspec",
28
+ "lib/heroku-resque-auto-scale.rb",
29
+ "test/helper.rb",
30
+ "test/test_heroku-resque-auto-scale.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/mirthlab/heroku-resque-auto-scale}
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{Auto scale your resque workers on Heroku.}
37
+ s.test_files = [
38
+ "test/helper.rb",
39
+ "test/test_heroku-resque-auto-scale.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<resque>, ["~> 1.10.0"])
48
+ s.add_runtime_dependency(%q<heroku>, [">= 0"])
49
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
50
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
52
+ s.add_development_dependency(%q<rcov>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<resque>, ["~> 1.10.0"])
55
+ s.add_dependency(%q<heroku>, [">= 0"])
56
+ s.add_dependency(%q<shoulda>, [">= 0"])
57
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
59
+ s.add_dependency(%q<rcov>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<resque>, ["~> 1.10.0"])
63
+ s.add_dependency(%q<heroku>, [">= 0"])
64
+ s.add_dependency(%q<shoulda>, [">= 0"])
65
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
66
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
67
+ s.add_dependency(%q<rcov>, [">= 0"])
68
+ end
69
+ end
70
+
@@ -0,0 +1,63 @@
1
+ require 'heroku'
2
+
3
+ module HerokuResqueAutoScale
4
+ module Scaler
5
+ class << self
6
+ @@heroku = Heroku::Client.new(ENV['HEROKU_USER'], ENV['HEROKU_PASSWORD'])
7
+
8
+ def workers
9
+ @@heroku.info(ENV['HEROKU_APP'])[:workers].to_i
10
+ end
11
+
12
+ def workers=(qty)
13
+ @@heroku.set_workers(ENV['HEROKU_APP'], qty)
14
+ end
15
+
16
+ def job_count
17
+ Resque.info[:pending].to_i
18
+ end
19
+ end
20
+ end
21
+
22
+ def after_perform_scale_down(*args)
23
+ # Nothing fancy, just shut everything down if we have no jobs
24
+ Scaler.workers = 0 if Scaler.job_count.zero?
25
+ end
26
+
27
+ def after_enqueue_scale_up(*args)
28
+ [
29
+ {
30
+ :workers => 1, # This many workers
31
+ :job_count => 1 # For this many jobs or more, until the next level
32
+ },
33
+ {
34
+ :workers => 2,
35
+ :job_count => 15
36
+ },
37
+ {
38
+ :workers => 3,
39
+ :job_count => 25
40
+ },
41
+ {
42
+ :workers => 4,
43
+ :job_count => 40
44
+ },
45
+ {
46
+ :workers => 5,
47
+ :job_count => 60
48
+ }
49
+ ].reverse_each do |scale_info|
50
+ # Run backwards so it gets set to the highest value first
51
+ # Otherwise if there were 70 jobs, it would get set to 1, then 2, then 3, etc
52
+
53
+ # If we have a job count greater than or equal to the job limit for this scale info
54
+ if Scaler.job_count >= scale_info[:job_count]
55
+ # Set the number of workers unless they are already set to a level we want. Don't scale down here!
56
+ if Scaler.workers <= scale_info[:workers]
57
+ Scaler.workers = scale_info[:workers]
58
+ end
59
+ break # We've set or ensured that the worker count is high enough
60
+ end
61
+ end
62
+ end
63
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'heroku-resque-auto-scale'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestHerokuResqueAutoScale < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heroku-resque-auto-scale
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Mark Quezada
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-20 00:00:00 -10:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: resque
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 10
30
+ - 0
31
+ version: 1.10.0
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: heroku
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: shoulda
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ type: :development
59
+ prerelease: false
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 1
70
+ - 0
71
+ - 0
72
+ version: 1.0.0
73
+ type: :development
74
+ prerelease: false
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: jeweler
78
+ requirement: &id005 !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 1
85
+ - 5
86
+ - 2
87
+ version: 1.5.2
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *id005
91
+ - !ruby/object:Gem::Dependency
92
+ name: rcov
93
+ requirement: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ type: :development
102
+ prerelease: false
103
+ version_requirements: *id006
104
+ description: Auto scale your resque workers on Heroku.
105
+ email: mark@mirthlab.com
106
+ executables: []
107
+
108
+ extensions: []
109
+
110
+ extra_rdoc_files:
111
+ - LICENSE.txt
112
+ - README.rdoc
113
+ files:
114
+ - .document
115
+ - Gemfile
116
+ - Gemfile.lock
117
+ - LICENSE.txt
118
+ - README.rdoc
119
+ - Rakefile
120
+ - VERSION
121
+ - heroku-resque-auto-scale.gemspec
122
+ - lib/heroku-resque-auto-scale.rb
123
+ - test/helper.rb
124
+ - test/test_heroku-resque-auto-scale.rb
125
+ has_rdoc: true
126
+ homepage: http://github.com/mirthlab/heroku-resque-auto-scale
127
+ licenses:
128
+ - MIT
129
+ post_install_message:
130
+ rdoc_options: []
131
+
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ hash: 745442095568484091
140
+ segments:
141
+ - 0
142
+ version: "0"
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ segments:
149
+ - 0
150
+ version: "0"
151
+ requirements: []
152
+
153
+ rubyforge_project:
154
+ rubygems_version: 1.3.7
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: Auto scale your resque workers on Heroku.
158
+ test_files:
159
+ - test/helper.rb
160
+ - test/test_heroku-resque-auto-scale.rb