Class: Scaltainer::ServiceTypeBase

Inherits:
Object
  • Object
show all
Defined in:
lib/scaltainer/service_types/base.rb

Direct Known Subclasses

ServiceTypeWeb, ServiceTypeWorker

Instance Method Summary collapse

Constructor Details

#initialize(app_endpoint) ⇒ ServiceTypeBase

Returns a new instance of ServiceTypeBase.



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

def initialize(app_endpoint)
  @app_endpoint = app_endpoint.sub('$HIREFIRE_TOKEN', ENV['HIREFIRE_TOKEN'] || '') if app_endpoint
end

Instance Method Details

#adjust_desired_replicas(desired_replicas, config) ⇒ Object



17
18
19
20
# File 'lib/scaltainer/service_types/base.rb', line 17

def adjust_desired_replicas(desired_replicas, config)
  desired_replicas = [config["max"], desired_replicas].min if config["max"]
  [config["min"], desired_replicas].max
end

#determine_desired_replicas(metric, service_config, current_replicas) ⇒ Object



12
13
14
15
# File 'lib/scaltainer/service_types/base.rb', line 12

def determine_desired_replicas(metric, service_config, current_replicas)
  raise ConfigurationError.new 'No metric found for requested service' unless metric
  raise ConfigurationError.new 'No configuration found for requested service' unless service_config
end

#get_metrics(services) ⇒ Object



7
8
9
10
# File 'lib/scaltainer/service_types/base.rb', line 7

def get_metrics(services)
  services_count = services.keys.length rescue 0
  raise Scaltainer::Warning.new "No services found for #{self.class.name}" if services_count == 0
end

#to_sObject



62
63
64
# File 'lib/scaltainer/service_types/base.rb', line 62

def to_s
  "Base"
end

#yield_to_scale(replica_diff, config, state, metric, service_name, logger) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/scaltainer/service_types/base.rb', line 22

def yield_to_scale(replica_diff, config, state, metric, service_name, logger)
  # Force up/down when below/above min/max?
  # one could argue that this could only happen on first deployment or when manually
  # scaled outside the scope of scaltainer. It is OK in this case to still apply sensitivity rules
  if replica_diff > 0
    # breach: change state and scale up
    state["upscale_sensitivity"] ||= 0
    state["upscale_sensitivity"] += 1
    state["downscale_sensitivity"] = 0
    if state["upscale_sensitivity"] >= config["upscale_sensitivity"]
      yield
      state["upscale_sensitivity"] = 0
    else
      logger.debug "Scaling up of service #{service_name} blocked by upscale_sensitivity at level " +
        "#{state["upscale_sensitivity"]} while level #{config["upscale_sensitivity"]} is required"
    end
  elsif replica_diff < 0  # TODO force down when above max?
    # breach: change state and scale down
    if can_scale_down? metric, config
      state["downscale_sensitivity"] ||= 0
      state["downscale_sensitivity"] += 1
      state["upscale_sensitivity"] = 0
      if state["downscale_sensitivity"] >= config["downscale_sensitivity"]
        yield
        state["downscale_sensitivity"] = 0
      else
        logger.debug "Scaling down of service #{service_name} blocked by downscale_sensitivity at level " +
          "#{state["downscale_sensitivity"]} while level #{config["downscale_sensitivity"]} is required"
      end
    else
      logger.debug "Scaling down of service #{service_name} to #{metric} replicas blocked by a non-decrementable config"
    end
  else
    # no breach, change state
    state["upscale_sensitivity"] = 0
    state["downscale_sensitivity"] = 0
    logger.info "No need to scale service #{service_name}"
  end
end