Sha256: faefd3d6f55f9e8e437ad25a7bc65d53f82286e2700a3379637618c23e56c6b5

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'net/http'
require 'uri'

module JobsAutoscaling
  class AwsAction
    attr_reader :asg_name, :client

    def initialize(asg_name:)
      @asg_name = asg_name
      @client = Aws::AutoScaling::Client.new(retry_limit: 10)
    end

    def idle
      @client.set_instance_protection(
        protected_from_scale_in: false,
        auto_scaling_group_name: asg_name,
        instance_ids: [instance_id],
      )
    end

    # it's intentional that if this call fails, the error bubbles up to
    # inst-jobs and errors the WorkQueue. We don't want to start running the
    # job if we weren't able to block scaledowns.
    def busy
      @client.set_instance_protection(
        protected_from_scale_in: true,
        auto_scaling_group_name: asg_name,
        instance_ids: [instance_id],
      )
    end

    # This is the hard-coded EC2 endpoint for getting instance metadata.
    # Oddly the ruby SDK doesn't include a method to get this information,
    # you are expected to just hit the endpoint yourself.
    INSTANCE_ID_ENDPOINT = 'http://169.254.169.254/latest/meta-data/instance-id'

    def instance_id
      @instance_id ||= Net::HTTP.get(URI.parse(INSTANCE_ID_ENDPOINT))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
inst-jobs-autoscaling-1.0.1 lib/jobs_autoscaling/aws_action.rb
inst-jobs-autoscaling-1.0.0 lib/jobs_autoscaling/aws_action.rb