Class: Autoscaler::HerokuScaler

Inherits:
Object
  • Object
show all
Defined in:
lib/autoscaler/heroku_scaler.rb

Overview

Wraps the Heroku API to provide just the interface that we need for scaling.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = 'worker', key = ENV['HEROKU_API_KEY'], app = ENV['HEROKU_APP']) ⇒ HerokuScaler

Returns a new instance of HerokuScaler.

Parameters:

  • type (String) (defaults to: 'worker')

    process type this scaler controls

  • key (String) (defaults to: ENV['HEROKU_API_KEY'])

    Heroku API key

  • app (String) (defaults to: ENV['HEROKU_APP'])

    Heroku app name



9
10
11
12
13
14
15
16
17
18
# File 'lib/autoscaler/heroku_scaler.rb', line 9

def initialize(
    type = 'worker',
    key = ENV['HEROKU_API_KEY'],
    app = ENV['HEROKU_APP'])
  @client = Heroku::API.new(:api_key => key)
  @type = type
  @app = app
  @workers = 0
  @known = Time.now - 1
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



20
21
22
# File 'lib/autoscaler/heroku_scaler.rb', line 20

def app
  @app
end

#exception_handler=(value) ⇒ Object

Callable object which responds to exceptions during api calls

Examples:

heroku.exception_handler = lambda {|exception| MyApp.logger.error(exception)}
heroku.exception_handler = lambda {|exception| raise}
# default
lambda {|exception|
  p exception
  puts exception.backtrace
}


53
54
55
# File 'lib/autoscaler/heroku_scaler.rb', line 53

def exception_handler=(value)
  @exception_handler = value
end

#typeObject (readonly)

Returns the value of attribute type.



21
22
23
# File 'lib/autoscaler/heroku_scaler.rb', line 21

def type
  @type
end

Instance Method Details

#workersNumeric

Read the current worker count (value may be cached)

Returns:

  • (Numeric)

    number of workers



25
26
27
28
29
30
31
# File 'lib/autoscaler/heroku_scaler.rb', line 25

def workers
  if known?
    @workers
  else
    know heroku_get_workers
  end
end

#workers=(n) ⇒ Object

Set the number of workers (noop if workers the same)

Parameters:

  • n (Numeric)

    number of workers



35
36
37
38
39
40
41
# File 'lib/autoscaler/heroku_scaler.rb', line 35

def workers=(n)
  if n != @workers || !known?
    p "Scaling #{type} to #{n}"
    heroku_set_workers(n)
    know n
  end
end