class Scaltainer::ServiceTypeWorker

Public Class Methods

new(app_endpoint = nil) click to toggle source
Calls superclass method Scaltainer::ServiceTypeBase::new
# File lib/scaltainer/service_types/worker.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/worker.rb, line 22
def determine_desired_replicas(metric, service_config, current_replicas)
  super
  raise ConfigurationError.new "Missing ratio in worker resource configuration" unless service_config["ratio"]
  if !metric.is_a?(Integer) || metric < 0
    raise ConfigurationError.new "#{metric} is an invalid metric value, must be a non-negative number" 
  end
  desired_replicas = (metric * 1.0 / service_config["ratio"]).ceil
end
get_metrics(services) click to toggle source
Calls superclass method Scaltainer::ServiceTypeBase#get_metrics
# File lib/scaltainer/service_types/worker.rb, line 7
def get_metrics(services)
  super
  begin
    response = Excon.get(@app_endpoint)
    m = JSON.parse(response.body)
    m.reduce({}){|hash, item| hash.merge!({item["name"] => item["quantity"]})}
  rescue JSON::ParserError => e
    raise ConfigurationError.new "app_endpoint returned non json response: #{response.body[0..128]}"
  rescue TypeError => e
    raise ConfigurationError.new "app_endpoint returned unexpected json response: #{response.body[0..128]}"
  rescue => e
    raise NetworkError.new "Could not retrieve metrics from application endpoint: #{@app_endpoint}.\n#{e.message}"
  end
end
to_s() click to toggle source
# File lib/scaltainer/service_types/worker.rb, line 31
def to_s
  "Worker"
end