Class: Heroku::Scalr::App

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

Constant Summary collapse

DEFAULTS =
{
  interval: 30,
  min_dynos: 1,
  max_dynos: 2,
  wait_low: 10,
  wait_high: 100,
  ping_low: 200,
  ping_high: 500,
  metric: :ping,
  min_frequency: 60
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ App

Returns a new instance of App.

Parameters:

  • name (String)

    Heroku app name

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :interval (Integer)

    perform checks every ‘interval` seconds, default: 60

  • :min_dynos (Integer)

    the minimum number of dynos, default: 1

  • :max_dynos (Integer)

    the maximum number of dynos, default: 2

  • :wait_low (Integer)

    lowers the number of dynos if queue wait time is less than ‘wait_low` ms, default: 10

  • :wait_high (Integer)

    lowers the number of dynos if queue wait time is more than ‘wait_high` ms, default: 100

  • :min_frequency (Integer)

    leave at least ‘min_frequency` seconds before scaling again, default: 60

  • :api_key (String)

    the Heroku account’s API key



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/heroku/scalr/app.rb', line 35

def initialize(name, opts = {})
  @name = name.to_s

  opts = DEFAULTS.merge(opts)
  fail("no API key given") unless opts[:api_key]
  fail("min_dynos must be at least 1") unless opts[:min_dynos] >= 1
  fail("max_dynos must be at least 1") unless opts[:max_dynos] >= 1
  fail("interval must be at least 10") unless opts[:interval] >= 10

  @url       = opts[:url] || "http://#{@name}.herokuapp.com/robots.txt"
  @api_key   = opts[:api_key]
  @interval  = opts[:interval].to_i
  @min_dynos = opts[:min_dynos].to_i
  @max_dynos = opts[:max_dynos].to_i
  @wait_low  = opts[:wait_low].to_i
  @wait_high = opts[:wait_high].to_i
  @ping_low  = opts[:ping_low].to_i
  @ping_high = opts[:ping_high].to_i
  @metric    = Heroku::Scalr::Metric.new(opts[:metric], self)
  @min_frequency  = opts[:min_frequency].to_i
  @last_scaled_at = Time.at(0)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#intervalObject (readonly)

Returns the value of attribute interval.



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

def interval
  @interval
end

#last_scaled_atObject (readonly)

Returns the value of attribute last_scaled_at.



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

def last_scaled_at
  @last_scaled_at
end

#max_dynosObject (readonly)

Returns the value of attribute max_dynos.



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

def max_dynos
  @max_dynos
end

#metricObject (readonly)

Returns the value of attribute metric.



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

def metric
  @metric
end

#min_dynosObject (readonly)

Returns the value of attribute min_dynos.



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

def min_dynos
  @min_dynos
end

#min_frequencyObject (readonly)

Returns the value of attribute min_frequency.



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

def min_frequency
  @min_frequency
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#ping_highObject (readonly)

Returns the value of attribute ping_high.



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

def ping_high
  @ping_high
end

#ping_lowObject (readonly)

Returns the value of attribute ping_low.



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

def ping_low
  @ping_low
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

#wait_highObject (readonly)

Returns the value of attribute wait_high.



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

def wait_high
  @wait_high
end

#wait_lowObject (readonly)

Returns the value of attribute wait_low.



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

def wait_low
  @wait_low
end

Instance Method Details

#log(level, message) ⇒ Object

Parameters:

  • level (Symbol)
  • message (String)


77
78
79
# File 'lib/heroku/scalr/app.rb', line 77

def log(level, message)
  Heroku::Scalr.logger.send(level, "[#{name}] #{message}")
end

#scale!Object

Scales the app



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/heroku/scalr/app.rb', line 59

def scale!
  scale_at = next_scale_attempt
  now      = Time.now
  if now < scale_at
    log :debug, "skip check, next attempt in #{(scale_at - now).to_i}s"
    return
  end

  do_scale(metric.by)
rescue => e
  msg = "#{e.class}: #{e.to_s}"
  msg << "\n\t" << e.backtrace.join("\n\t") if e.backtrace
  log :error, msg
  nil
end