Class: EcsDeploy::AutoScaler::SpotFleetRequestConfig

Inherits:
Struct
  • Object
show all
Includes:
ConfigBase
Defined in:
lib/ecs_deploy/auto_scaler/spot_fleet_request_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigBase

#logger

Constructor Details

#initialize(attributes = {}, logger) ⇒ SpotFleetRequestConfig

Returns a new instance of SpotFleetRequestConfig.



14
15
16
17
18
19
20
21
# File 'lib/ecs_deploy/auto_scaler/spot_fleet_request_config.rb', line 14

def initialize(attributes = {}, logger)
  attributes = attributes.dup
  services = attributes.delete("services")
  super(attributes, logger)
  self.service_configs = services.map do |s|
    ServiceConfig.new(s.merge("cluster" => cluster, "region" => region), logger)
  end
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer

Returns:

  • (Object)

    the current value of buffer



11
12
13
# File 'lib/ecs_deploy/auto_scaler/spot_fleet_request_config.rb', line 11

def buffer
  @buffer
end

#clusterObject

Returns the value of attribute cluster

Returns:

  • (Object)

    the current value of cluster



11
12
13
# File 'lib/ecs_deploy/auto_scaler/spot_fleet_request_config.rb', line 11

def cluster
  @cluster
end

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



11
12
13
# File 'lib/ecs_deploy/auto_scaler/spot_fleet_request_config.rb', line 11

def id
  @id
end

#regionObject

Returns the value of attribute region

Returns:

  • (Object)

    the current value of region



11
12
13
# File 'lib/ecs_deploy/auto_scaler/spot_fleet_request_config.rb', line 11

def region
  @region
end

#service_configsObject

Returns the value of attribute service_configs

Returns:

  • (Object)

    the current value of service_configs



11
12
13
# File 'lib/ecs_deploy/auto_scaler/spot_fleet_request_config.rb', line 11

def service_configs
  @service_configs
end

Instance Method Details

#cluster_resource_managerObject



51
52
53
54
55
56
57
58
59
# File 'lib/ecs_deploy/auto_scaler/spot_fleet_request_config.rb', line 51

def cluster_resource_manager
  @cluster_resource_manager ||= EcsDeploy::AutoScaler::ClusterResourceManager.new(
    region: region,
    cluster: cluster,
    service_configs: service_configs,
    capacity_based_on: "vCPUs",
    logger: @logger,
  )
end

#nameObject



23
24
25
# File 'lib/ecs_deploy/auto_scaler/spot_fleet_request_config.rb', line 23

def name
  id
end

#update_desired_capacity(required_capacity) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ecs_deploy/auto_scaler/spot_fleet_request_config.rb', line 27

def update_desired_capacity(required_capacity)
  terminate_orphan_instances

  desired_capacity = (required_capacity + buffer.to_f).ceil

  request_config = ec2_client.describe_spot_fleet_requests(
    spot_fleet_request_ids: [id]
  ).spot_fleet_request_configs[0].spot_fleet_request_config

  return if desired_capacity == request_config.target_capacity

  ec2_client.modify_spot_fleet_request(spot_fleet_request_id: id, target_capacity: desired_capacity)

  cluster_resource_manager.trigger_capacity_update(
    request_config.target_capacity,
    desired_capacity,
    # Wait until the capacity is updated to prevent the process from terminating before container draining is completed
    wait_until_capacity_updated: desired_capacity < request_config.target_capacity,
  )
  @logger.info "#{log_prefix} Update desired_capacity to #{desired_capacity}"
rescue => e
  AutoScaler.error_logger.error(e)
end