Class: Scaltainer::ServiceTypeWorker

Inherits:
ServiceTypeBase show all
Defined in:
lib/scaltainer/service_types/worker.rb

Instance Method Summary collapse

Methods inherited from ServiceTypeBase

#adjust_desired_replicas, #yield_to_scale

Constructor Details

#initialize(app_endpoint = nil) ⇒ ServiceTypeWorker

Returns a new instance of ServiceTypeWorker.



3
4
5
# File 'lib/scaltainer/service_types/worker.rb', line 3

def initialize(app_endpoint = nil)
  super
end

Instance Method Details

#determine_desired_replicas(metric, service_config, current_replicas) ⇒ Object



22
23
24
25
26
27
28
29
# 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) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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_sObject



31
32
33
# File 'lib/scaltainer/service_types/worker.rb', line 31

def to_s
  "Worker"
end