class Scaltainer::ServiceTypeWeb

Public Class Methods

new(app_endpoint = nil) click to toggle source
Calls superclass method Scaltainer::ServiceTypeBase::new
# File lib/scaltainer/service_types/web.rb, line 3
def initialize(app_endpoint = nil)
  super
end

Public Instance Methods

determine_desired_replicas(metric, service_config, current_replicas) click to toggle source
# File lib/scaltainer/service_types/web.rb, line 29
def determine_desired_replicas(metric, service_config, current_replicas)
  super
  raise ConfigurationError.new "Missing max_response_time in web resource configuration" unless service_config["max_response_time"]
  raise ConfigurationError.new "Missing min_response_time in web resource configuration" unless service_config["min_response_time"]
  unless service_config["min_response_time"] <= service_config["max_response_time"]
    raise ConfigurationError.new "min_response_time and max_response_time are not in order"
  end
  desired_replicas = if metric > service_config["max_response_time"]
    current_replicas + service_config["upscale_quantity"]
  elsif metric < service_config["min_response_time"]
    current_replicas - service_config["downscale_quantity"]
  else
    current_replicas
  end
end
get_metrics(services) click to toggle source
Calls superclass method Scaltainer::ServiceTypeBase#get_metrics
# File lib/scaltainer/service_types/web.rb, line 7
def get_metrics(services)
  super
  nr_key = ENV['NEW_RELIC_API_KEY']
  raise ConfigurationError.new 'NEW_RELIC_API_KEY not set in environment' unless nr_key
  nr = Newrelic::Metrics.new nr_key
  to = Time.now
  from = to - (ENV['RESPONSE_TIME_WINDOW'] || '5').to_i * 60

  services.reduce({}) do |hash, (service_name, service_config)|
    app_id = service_config["newrelic_app_id"]
    raise ConfigurationError.new "Resource #{service_name} does not have a corresponding newrelic_app_id" unless app_id

    begin
      metric = nr.get_avg_response_time app_id, from, to
    rescue => e
      raise NetworkError.new "Could not retrieve metrics from New Relic API for #{service_name}: #{e.message}"
    end

    hash.merge!(service_name => metric)
  end
end
to_s() click to toggle source
# File lib/scaltainer/service_types/web.rb, line 45
def to_s
  "Web"
end