autoscaler 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +7 -0
- data/README.md +61 -0
- data/examples/complex.rb +40 -0
- data/examples/simple.rb +28 -0
- data/lib/autoscaler/version.rb +1 -1
- metadata +8 -4
data/CHANGELOG.md
ADDED
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Sidekiq Heroku Autoscaler
|
2
|
+
|
3
|
+
[Sidekiq](https://github.com/mperham/sidekiq) performs background jobs. While it's threading model allows it to scale easier than worker-pre-process background systems, people running test or lightly loaded systems on [Heroku](http://www.heroku.com/) still want to scale down to zero to avoid racking up charges.
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
Tested on Ruby 1.9.2 and Heroku Cedar stack.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
gem install sidekiq
|
12
|
+
|
13
|
+
## Getting Started
|
14
|
+
|
15
|
+
This gem uses the [Herkou-Api](https://github.com/heroku/heroku.rb) gem, which requires an API key from Heroku. It will also need the heroku app name. By default, these are specified through environment variables. You can also pass them to HerkouScaler explicitly.
|
16
|
+
|
17
|
+
HEROKU_API_KEY=.....
|
18
|
+
HEROKU_APP=....
|
19
|
+
|
20
|
+
Install the middleware in your `Sidekiq.configure_` blocks
|
21
|
+
|
22
|
+
Sidekiq.configure_client do |config|
|
23
|
+
config.client_middleware do |chain|
|
24
|
+
chain.add Autoscaler::Sidekiq::Client, 'default' => Autoscaler::HerokuScaler.new
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Sidekiq.configure_server do |config|
|
29
|
+
config.server_middleware do |chain|
|
30
|
+
chain.add(Autoscaler::Sidekiq::Server, Autoscaler::HerokuScaler.new, 60)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
## Limits and Challenges
|
35
|
+
|
36
|
+
- HerokuScaler includes an attempt at current-worker cache that may be overcomplication, and doesn't work very well (see next)
|
37
|
+
- Multiple threads often send scaling requests at once. Heroku seems to handle this well.
|
38
|
+
- Workers sleep-loop and are not actually returned to the pool; when a job or timeout happen, they can all release at once.
|
39
|
+
|
40
|
+
### Long Jobs
|
41
|
+
|
42
|
+
Since the shutdown check gets performed every time a job completes, the timeout will need to be longer than the longest job. For mixed workloads, you might want to have multiple sidekiq processes defined. I use one with many workers for general work, and a single-worker process for long import jobs. See `examples/complex.rb`
|
43
|
+
|
44
|
+
## Tests
|
45
|
+
|
46
|
+
The project is setup to run RSpec with Guard.
|
47
|
+
|
48
|
+
The HerokuScaler is not tested by default because it makes live API requests. Specify `HEROKU_APP` and `HEROKU_API_KEY` on the command line, and then watch your app's logs.
|
49
|
+
|
50
|
+
HEROKU_APP=... HEROKU_API_KEY=... guard
|
51
|
+
heroku logs --app ...
|
52
|
+
|
53
|
+
## Authors
|
54
|
+
|
55
|
+
Justin Love, [@wondible](http://twitter.com/wondible), [https://github.com/JustinLove](https://github.com/JustinLove)
|
56
|
+
|
57
|
+
Ported to Heroku-Api by Fix Peña, [https://github.com/fixr](https://github.com/fixr)
|
58
|
+
|
59
|
+
## Licence
|
60
|
+
|
61
|
+
Released under the [MIT license](http://www.opensource.org/licenses/mit-license.php).
|
data/examples/complex.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'securerandom' # bug in Sidekiq as of 2.2.1
|
2
|
+
require 'sidekiq'
|
3
|
+
require 'autoscaler/sidekiq'
|
4
|
+
require 'autoscaler/heroku_scaler'
|
5
|
+
|
6
|
+
heroku = nil
|
7
|
+
if ENV['HEROKU_APP']
|
8
|
+
heroku = {}
|
9
|
+
scaleable = %w[default import] - (ENV['ALWAYS'] || '').split(' ')
|
10
|
+
scaleable.each do |queue|
|
11
|
+
heroku[queue] = Autoscaler::HerokuScaler.new(
|
12
|
+
queue,
|
13
|
+
ENV['HEROKU_API_KEY'],
|
14
|
+
ENV['HEROKU_APP'])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Sidekiq.configure_client do |config|
|
19
|
+
if heroku
|
20
|
+
config.client_middleware do |chain|
|
21
|
+
chain.add Autoscaler::Sidekiq::Client, heroku
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# define HEROKU_PROCESS in the Procfile:
|
27
|
+
#
|
28
|
+
# default: env HEROKU_PROCESS=default bundle exec sidekiq -r ./background/boot.rb
|
29
|
+
# import: env HEROKU_PROCESS=import bundle exec sidekiq -q import -c 1 -r ./background/boot.rb
|
30
|
+
|
31
|
+
Sidekiq.configure_server do |config|
|
32
|
+
config.server_middleware do |chain|
|
33
|
+
if heroku && ENV['HEROKU_PROCESS'] && heroku[ENV['HEROKU_PROCESS']]
|
34
|
+
p "Setting up auto-scaledown"
|
35
|
+
chain.add(Autoscaler::Sidekiq::Server, heroku[ENV['HEROKU_PROCESS']], 60, [ENV['HEROKU_PROCESS']])
|
36
|
+
else
|
37
|
+
p "Not scaleable"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'securerandom' # bug in Sidekiq as of 2.2.1
|
2
|
+
require 'sidekiq'
|
3
|
+
require 'autoscaler/sidekiq'
|
4
|
+
require 'autoscaler/heroku_scaler'
|
5
|
+
|
6
|
+
heroku = nil
|
7
|
+
if ENV['HEROKU_APP']
|
8
|
+
heroku = Autoscaler::HerokuScaler.new
|
9
|
+
end
|
10
|
+
|
11
|
+
Sidekiq.configure_client do |config|
|
12
|
+
if heroku
|
13
|
+
config.client_middleware do |chain|
|
14
|
+
chain.add Autoscaler::Sidekiq::Client, 'default' => heroku
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Sidekiq.configure_server do |config|
|
20
|
+
config.server_middleware do |chain|
|
21
|
+
if heroku
|
22
|
+
p "Setting up auto-scaledown"
|
23
|
+
chain.add(Autoscaler::Sidekiq::Server, heroku, 60)
|
24
|
+
else
|
25
|
+
p "Not scaleable"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/autoscaler/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: autoscaler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-10-
|
13
|
+
date: 2012-10-25 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: sidekiq
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 2.2
|
22
|
+
version: '2.2'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,7 +27,7 @@ dependencies:
|
|
27
27
|
requirements:
|
28
28
|
- - ~>
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
version: 2.2
|
30
|
+
version: '2.2'
|
31
31
|
- !ruby/object:Gem::Dependency
|
32
32
|
name: heroku-api
|
33
33
|
requirement: !ruby/object:Gem::Requirement
|
@@ -120,6 +120,10 @@ files:
|
|
120
120
|
- lib/autoscaler/sidekiq.rb
|
121
121
|
- lib/autoscaler/version.rb
|
122
122
|
- lib/autoscaler.rb
|
123
|
+
- README.md
|
124
|
+
- CHANGELOG.md
|
125
|
+
- examples/complex.rb
|
126
|
+
- examples/simple.rb
|
123
127
|
- spec/autoscaler/heroku_scaler_spec.rb
|
124
128
|
- spec/autoscaler/sidekiq_spec.rb
|
125
129
|
- spec/spec_helper.rb
|