Class: Heroku::Scalr::App
- Inherits:
-
Object
- Object
- Heroku::Scalr::App
- 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, cool_freq: 180, heat_freq: 60, }.freeze
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#cool_freq ⇒ Object
readonly
Returns the value of attribute cool_freq.
-
#heat_freq ⇒ Object
readonly
Returns the value of attribute heat_freq.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#last_scaled_at ⇒ Object
readonly
Returns the value of attribute last_scaled_at.
-
#max_dynos ⇒ Object
readonly
Returns the value of attribute max_dynos.
-
#metric ⇒ Object
readonly
Returns the value of attribute metric.
-
#min_dynos ⇒ Object
readonly
Returns the value of attribute min_dynos.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ping_high ⇒ Object
readonly
Returns the value of attribute ping_high.
-
#ping_low ⇒ Object
readonly
Returns the value of attribute ping_low.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#wait_high ⇒ Object
readonly
Returns the value of attribute wait_high.
-
#wait_low ⇒ Object
readonly
Returns the value of attribute wait_low.
Instance Method Summary collapse
-
#initialize(name, opts = {}) ⇒ App
constructor
A new instance of App.
- #log(level, message) ⇒ Object
-
#scale! ⇒ Object
Scales the app.
Constructor Details
#initialize(name, opts = {}) ⇒ App
Returns a new instance of App.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/heroku/scalr/app.rb', line 42 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) @cool_freq = opts[:cool_freq].to_i @heat_freq = opts[:heat_freq].to_i @last_scaled_at = Time.at(0) end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def api_key @api_key end |
#cool_freq ⇒ Object (readonly)
Returns the value of attribute cool_freq.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def cool_freq @cool_freq end |
#heat_freq ⇒ Object (readonly)
Returns the value of attribute heat_freq.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def heat_freq @heat_freq end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def interval @interval end |
#last_scaled_at ⇒ Object (readonly)
Returns the value of attribute last_scaled_at.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def last_scaled_at @last_scaled_at end |
#max_dynos ⇒ Object (readonly)
Returns the value of attribute max_dynos.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def max_dynos @max_dynos end |
#metric ⇒ Object (readonly)
Returns the value of attribute metric.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def metric @metric end |
#min_dynos ⇒ Object (readonly)
Returns the value of attribute min_dynos.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def min_dynos @min_dynos end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def name @name end |
#ping_high ⇒ Object (readonly)
Returns the value of attribute ping_high.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def ping_high @ping_high end |
#ping_low ⇒ Object (readonly)
Returns the value of attribute ping_low.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def ping_low @ping_low end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def url @url end |
#wait_high ⇒ Object (readonly)
Returns the value of attribute wait_high.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def wait_high @wait_high end |
#wait_low ⇒ Object (readonly)
Returns the value of attribute wait_low.
16 17 18 |
# File 'lib/heroku/scalr/app.rb', line 16 def wait_low @wait_low end |
Instance Method Details
#log(level, message) ⇒ Object
86 87 88 |
# File 'lib/heroku/scalr/app.rb', line 86 def log(level, ) Heroku::Scalr.logger.send(level, "[#{name}] #{}") end |
#scale! ⇒ Object
Scales the app
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/heroku/scalr/app.rb', line 67 def scale! scale_at = last_scaled_at + [cool_freq, heat_freq].min now = Time.now if now < scale_at log :debug, "skip check, next attempt in #{(scale_at - now).to_i}s" return end by = metric.by do_scale(by) if must_scale?(by, now) rescue => e msg = "#{e.class}: #{e.to_s}" msg << "\n\t" << e.backtrace.join("\n\t") if e.backtrace log :error, msg nil end |