Class: OpenStax::Aws::AutoScalingInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/aws/auto_scaling_instance.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group_name:, id:, region:) ⇒ AutoScalingInstance

Returns a new instance of AutoScalingInstance.



8
9
10
11
12
13
14
# 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

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



4
5
6
# File 'lib/openstax/aws/auto_scaling_instance.rb', line 4

def raw
  @raw
end

Class Method Details

.meObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Method Details

#continue_to_termination(hook_name:) ⇒ Object



86
87
88
89
90
91
92
93
# 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_secondsObject



74
75
76
# File 'lib/openstax/aws/auto_scaling_instance.rb', line 74

def lifecycle_state_refresh_seconds
  5
end

#recent_lifecycle_stateObject



63
64
65
66
67
68
69
70
71
72
# 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 = {}) ⇒ Object



31
32
33
34
35
36
37
38
# 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_secondsObject



78
79
80
# File 'lib/openstax/aws/auto_scaling_instance.rb', line 78

def terminate_wait_sleep_seconds
  6
end

#terminating_wait?Boolean

Returns:

  • (Boolean)


82
83
84
# 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:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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