Class: Autoscaler::HerokuPlatformScaler

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type: 'worker', token: ENV['AUTOSCALER_HEROKU_ACCESS_TOKEN'], app: ENV['AUTOSCALER_HEROKU_APP']) ⇒ HerokuPlatformScaler

Returns a new instance of HerokuPlatformScaler.

Parameters:

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

    process type this scaler controls

  • token (String) (defaults to: ENV['AUTOSCALER_HEROKU_ACCESS_TOKEN'])

    Heroku OAuth access token

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

    Heroku app name



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

def initialize(
    type: 'worker',
    token: ENV['AUTOSCALER_HEROKU_ACCESS_TOKEN'],
    app: ENV['AUTOSCALER_HEROKU_APP'])

  @client = PlatformAPI.connect_oauth(token)
  @type = type
  @app = app
  @workers = CounterCacheMemory.new
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

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
}


51
52
53
# File 'lib/autoscaler/heroku_platform_scaler.rb', line 51

def exception_handler=(value)
  @exception_handler = value
end

#typeObject (readonly)

Returns the value of attribute type.



22
23
24
# File 'lib/autoscaler/heroku_platform_scaler.rb', line 22

def type
  @type
end

Instance Method Details

#counter_cache=(cache) ⇒ Object

Object which supports #counter and #counter= Defaults to CounterCacheMemory



55
56
57
# File 'lib/autoscaler/heroku_platform_scaler.rb', line 55

def counter_cache=(cache)
  @workers = cache
end

#workersNumeric

Read the current worker count (value may be cached)

Returns:

  • (Numeric)

    number of workers



26
27
28
# File 'lib/autoscaler/heroku_platform_scaler.rb', line 26

def workers
  @workers.counter {@workers.counter = heroku_get_workers}
end

#workers=(n) ⇒ Object

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

Parameters:

  • n (Numeric)

    number of workers



32
33
34
35
36
37
38
39
40
# File 'lib/autoscaler/heroku_platform_scaler.rb', line 32

def workers=(n)
  unknown = false
  current = @workers.counter{unknown = true; 1}
  if n != current || unknown
    p "Scaling #{type} to #{n}"
    heroku_set_workers(n)
    @workers.counter = n
  end
end