Class: Autoscale::Heroku

Inherits:
Object
  • Object
show all
Defined in:
lib/autoscale/heroku.rb

Class Method Summary collapse

Class Method Details

.appObject

the app to scale



54
55
56
# File 'lib/autoscale/heroku.rb', line 54

def app
  Autoscale.app
end

.calculate_required_workersObject



39
40
41
# File 'lib/autoscale/heroku.rb', line 39

def calculate_required_workers
  (scale.rindex{|x| x <= job_count}.to_i + 1)
end

.clientObject

heroku api client



68
69
70
# File 'lib/autoscale/heroku.rb', line 68

def client
  @@client ||= ::Heroku::API.new( params )
end

.downObject

shutdown if no jobs exist



32
33
34
35
36
37
# File 'lib/autoscale/heroku.rb', line 32

def down
  if job_count < 1
    QC.log(action: :scale, workers: min_workers)
    self.workers = min_workers
  end
end

.job_countObject



19
20
21
# File 'lib/autoscale/heroku.rb', line 19

def job_count
  QC::Queries.count.to_i
end

.min_workersObject



63
64
65
# File 'lib/autoscale/heroku.rb', line 63

def min_workers
  Autoscale.min || 0
end

.paramsObject



43
44
45
46
47
48
49
50
51
# File 'lib/autoscale/heroku.rb', line 43

def params
  {
    api_key:          Autoscale.api_key           || ENV['HEROKU_API_KEY'],
    connect_timeout:  Autoscale.connect_timeout,
    read_timeout:     Autoscale.read_timeout,
    write_timeout:    Autoscale.write_timeout,
    mock:             Autoscale.mock              || false
  }
end

.scaleObject

the scale



59
60
61
# File 'lib/autoscale/heroku.rb', line 59

def scale
  Autoscale.scale || [1, 15, 30, 40, 50]
end

.shutdownObject

shutdown all workers



15
16
17
# File 'lib/autoscale/heroku.rb', line 15

def shutdown
  self.workers = 0
end

.upObject

scale workers based on scale



24
25
26
27
28
29
# File 'lib/autoscale/heroku.rb', line 24

def up
  unless calculate_required_workers <= workers
    QC.log(action: :scale, workers: calculate_required_workers)
    self.workers = calculate_required_workers
  end
end

.workersObject



6
7
8
# File 'lib/autoscale/heroku.rb', line 6

def workers
  client.get_app(app).body.fetch("workers", 0).to_i
end

.workers=(qty) ⇒ Object



10
11
12
# File 'lib/autoscale/heroku.rb', line 10

def workers=(qty)
  client.post_ps_scale(app, "worker", qty)
end