class OpenStax::Aws::AutoScalingInstance

Attributes

raw[R]

Public Class Methods

me() click to toggle source
# File lib/openstax/aws/auto_scaling_instance.rb, line 16
def self.me
  instance_id = Ec2InstanceData.instance_id
  region = Ec2InstanceData.region

  client = Aws::AutoScaling::Client.new(region: region)
  instance_info = client.describe_auto_scaling_instances({instance_ids: [instance_id]})
                        .auto_scaling_instances[0]

  new(
    group_name: instance_info.auto_scaling_group_name,
    id: instance_id,
    region: region
  )
end
new(group_name:, id:, region:) click to toggle source
# File lib/openstax/aws/auto_scaling_instance.rb, line 8
def initialize(group_name:, id:, region:)
  @raw = Aws::AutoScaling::Instance.new(
    group_name,
    id,
    client: Aws::AutoScaling::Client.new(region: region)
  )
end

Public Instance Methods

continue_to_termination(hook_name:) click to toggle source
# File lib/openstax/aws/auto_scaling_instance.rb, line 86
def continue_to_termination(hook_name:)
  raw.client.complete_lifecycle_action({
    lifecycle_hook_name: hook_name,
    auto_scaling_group_name: raw.group_name,
    lifecycle_action_result: "CONTINUE",
    instance_id: raw.id,
  })
end
lifecycle_state_refresh_seconds() click to toggle source
# File lib/openstax/aws/auto_scaling_instance.rb, line 74
def lifecycle_state_refresh_seconds
  5
end
recent_lifecycle_state() click to toggle source
# File lib/openstax/aws/auto_scaling_instance.rb, line 63
def recent_lifecycle_state
  if @recent_lifecycle_state_last_refreshed_at.nil? ||
     Time.now - @recent_lifecycle_state_last_refreshed_at > lifecycle_state_refresh_seconds
    reload
    @recent_lifecycle_state_last_refreshed_at = Time.now
    @recent_lifecycle_state = lifecycle_state
  else
    @recent_lifecycle_state
  end
end
terminate(options = {}) click to toggle source
# File lib/openstax/aws/auto_scaling_instance.rb, line 31
def terminate(options = {})
  hook_name = options.delete(:continue_hook_name)
  raw.terminate(options)
  if hook_name
    sleep(terminate_wait_sleep_seconds) until terminating_wait?
    continue_to_termination(hook_name: hook_name)
  end
end
terminate_wait_sleep_seconds() click to toggle source
# File lib/openstax/aws/auto_scaling_instance.rb, line 78
def terminate_wait_sleep_seconds
  6
end
terminating_wait?() click to toggle source
# File lib/openstax/aws/auto_scaling_instance.rb, line 82
def terminating_wait?
  "Terminating:Wait" == recent_lifecycle_state
end
unless_waiting_for_termination(hook_name:) { || ... } click to toggle source
# File lib/openstax/aws/auto_scaling_instance.rb, line 40
def unless_waiting_for_termination(hook_name:)
  # "Terminating" is a transition state to "Terminating:Wait", but we don't
  # check for it because if we try to continue from "Terminating", AWS freaks
  # out because it needs to continue from the wait state

  if terminating_wait?
    continue_to_termination(hook_name: hook_name)
    return
  end

  yield

  # In case the yield takes a long time and this code isn't called
  # again for a while (e.g. an infrequent cron job), check the terminating
  # state again.  If this method is called in a loop, the check here
  # and the next check at the start of this method will not cause duplicate
  # network calls because the lifecycle state is cached for a few seconds.

  if terminating_wait?
    continue_to_termination(hook_name: hook_name)
  end
end