Class: EcsDeploy::AutoScaler::ServiceConfig

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

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ServiceConfig

Returns a new instance of ServiceConfig.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ecs_deploy/auto_scaler.rb', line 139

def initialize(attributes = {})
  super(attributes)
  self.idle_time ||= 60
  self.max_task_count = Array(max_task_count)
  self.upscale_triggers = upscale_triggers.to_a.map do |t|
    TriggerConfig.new(t.merge(region: region))
  end
  self.downscale_triggers = downscale_triggers.to_a.map do |t|
    TriggerConfig.new(t.merge(region: region))
  end
  self.max_task_count.sort!
  self.desired_count = fetch_service.desired_count
  @reach_max_at = nil
  @last_updated_at = nil
end

Instance Method Details

#client ⇒ Object



155
156
157
158
159
160
161
# File 'lib/ecs_deploy/auto_scaler.rb', line 155

def client
  Aws::ECS::Client.new(
    access_key_id: EcsDeploy.config.access_key_id,
    secret_access_key: EcsDeploy.config.secret_access_key,
    region: region
  )
end

#current_min_task_count ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/ecs_deploy/auto_scaler.rb', line 170

def current_min_task_count
  return min_task_count if scheduled_min_task_count.nil? || scheduled_min_task_count.empty?

  scheduled_min_task_count.find(-> { {"count" => min_task_count} }) { |s|
    from = Time.parse(s["from"])
    to = Time.parse(s["to"])
    (from..to).cover?(Time.now)
  }["count"]
end

#fetch_container_instances ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/ecs_deploy/auto_scaler.rb', line 240

def fetch_container_instances
  arns = []
  resp = nil
  cl = client
  loop do
    options = {cluster: cluster}
    options.merge(next_token: resp.next_token) if resp && resp.next_token
    resp = cl.list_container_instances(options)
    arns.concat(resp.container_instance_arns)
    break unless resp.next_token
  end

  chunk_size = 50
  container_instances = []
  arns.each_slice(chunk_size) do |arn_chunk|
    is = cl.describe_container_instances(cluster: cluster, container_instances: arn_chunk).container_instances
    container_instances.concat(is)
  end

  container_instances
end

#fetch_service ⇒ Object



185
186
187
188
189
190
191
# File 'lib/ecs_deploy/auto_scaler.rb', line 185

def fetch_service
  res = client.describe_services(cluster: cluster, services: [name])
  raise "Service \"#{name}\" is not found" if res.services.empty?
  res.services[0]
rescue => e
  AutoScaler.error_logger.error(e)
end

#idle? ⇒ Boolean

Returns:



163
164
165
166
167
168
# File 'lib/ecs_deploy/auto_scaler.rb', line 163

def idle?
  return false unless @last_updated_at

  diff = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) - @last_updated_at
  diff < idle_time
end

#overheat? ⇒ Boolean

Returns:



180
181
182
183
# File 'lib/ecs_deploy/auto_scaler.rb', line 180

def overheat?
  return false unless @reach_max_at
  (Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) - @reach_max_at) > cooldown_time_for_reach_max
end

#update_service(difference) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/ecs_deploy/auto_scaler.rb', line 193

def update_service(difference)
  next_desired_count = desired_count + difference
  current_level = max_task_level(desired_count)
  next_level = max_task_level(next_desired_count)
  if current_level < next_level && overheat? # next max
    level = next_level
    @reach_max_at = nil
    AutoScaler.logger.info "Service \"#{name}\" is overheat, uses next max count"
  elsif current_level < next_level && !overheat? # wait cooldown
    level = current_level
    now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
    @reach_max_at ||= now
    AutoScaler.logger.info "Service \"#{name}\" waits cooldown elapsed #{(now - @reach_max_at).to_i}sec"
  elsif current_level == next_level && next_desired_count >= max_task_count[current_level] # reach current max
    level = current_level
    now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
    @reach_max_at ||= now
    AutoScaler.logger.info "Service \"#{name}\" waits cooldown elapsed #{(now - @reach_max_at).to_i}sec"
  elsif current_level == next_level && next_desired_count < max_task_count[current_level]
    level = current_level
    @reach_max_at = nil
    AutoScaler.logger.info "Service \"#{name}\" clears cooldown state"
  elsif current_level > next_level
    level = next_level
    @reach_max_at = nil
    AutoScaler.logger.info "Service \"#{name}\" clears cooldown state"
  end

  cl = client
  next_desired_count = [next_desired_count, max_task_count[level]].min
  cl.update_service(
    cluster: cluster,
    service: name,
    desired_count: next_desired_count,
  )
  cl.wait_until(:services_stable, cluster: cluster, services: [name]) do |w|
    w.before_wait do
      AutoScaler.logger.debug "wait service stable [#{name}]"
    end
  end if difference < 0
  @last_updated_at = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
  self.desired_count = next_desired_count
  AutoScaler.logger.info "Update service \"#{name}\": desired_count -> #{next_desired_count}"
rescue => e
  AutoScaler.error_logger.error(e)
end