-
Notifications
You must be signed in to change notification settings - Fork 0
/
heroku_resque_auto_scale.rb
69 lines (62 loc) · 1.89 KB
/
heroku_resque_auto_scale.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'heroku'
# ENABLE_AUTO_SCALER = Rails.env.production?
ENABLE_AUTO_SCALER = false
unless ENABLE_AUTO_SCALER
module HerokuResqueAutoScale
end
else
module HerokuResqueAutoScale
module Scaler
class << self
@@heroku = Heroku::Client.new(ENV['HEROKU_USER'], ENV['HEROKU_PASSWORD'])
def workers
@@heroku.ps(ENV['HEROKU_APP']).count { |a| a["process"] =~ /worker/ }
end
def workers=(qty)
@@heroku.ps_scale(ENV['HEROKU_APP'], :type=>'worker', :qty=>qty)
end
def job_count
Resque.info[:pending].to_i
end
end
end
def after_perform_scale_up(*args)
Logger.new(STDERR).debug "Scaling..."
[
{
:workers => 1, # This many workers
:job_count => 1 # For this many jobs or more, until the next level
},
{
:workers => 2,
:job_count => 15
},
{
:workers => 3,
:job_count => 25
},
{
:workers => 4,
:job_count => 40
},
{
:workers => 5,
:job_count => 60
}
].reverse_each do |scale_info|
# Run backwards so it gets set to the highest value first
# Otherwise if there were 70 jobs, it would get set to 1, then 2, then 3, etc
# If we have a job count greater than or equal to the job limit for this scale info
if Scaler.job_count >= scale_info[:job_count]
# Set the number of workers unless they are already set to a level we want. Don't scale down here!
if Scaler.workers <= scale_info[:workers]
Logger.new(STDERR).info "Scaling to #{scale_info[:workers]} workers"
Scaler.workers = scale_info[:workers]
Logger.new(STDERR).info "...Done!"
end
break # We've set or ensured that the worker count is high enough
end
end
end
end
end