class MultiEc2Kiq::Dynamodb

Public Class Methods

new() click to toggle source
# File lib/threads/dynamodb.rb, line 7
def initialize
  @STATUS = OpenStruct.new(started: "started", stopped: "stopped", to_stop: "to_stop")
end

Public Instance Methods

create_status_table() click to toggle source
# File lib/threads/dynamodb.rb, line 32
def create_status_table
  options = {
    table_name: Settings.aws.dynamodb.status_table_name,
    key_schema: [
      {
          attribute_name: "instance_id",
          key_type: "HASH"
      }
    ],
    attribute_definitions: [
      {
          attribute_name: "instance_id",
          attribute_type: "S"
      }
    ],
    provisioned_throughput: {
      read_capacity_units:  1,
      write_capacity_units:  1
    }
  }

  create_table(options, Settings.aws.dynamodb.status_table_name)

  true
end
started(instance_id) click to toggle source
# File lib/threads/dynamodb.rb, line 11
def started(instance_id)
  put_item(instance_id, @STATUS.started)
  true
end
stopped(instance_id) click to toggle source
# File lib/threads/dynamodb.rb, line 26
def stopped(instance_id)
  raise_not_exist if !get_item(instance_id).item
  put_item(instance_id, @STATUS.stopped)
  true
end
wait_until_to_stop(instance_id) click to toggle source
# File lib/threads/dynamodb.rb, line 16
def wait_until_to_stop(instance_id)
  (0...Settings.wait_to_stop.max_attempts).each{|i|
    data = get_item(instance_id)
    raise_not_exist if !data.item
    return true if data.item["status"] == @STATUS.to_stop
    sleep(Settings.wait_to_stop.delay)
  }
  false
end

Private Instance Methods

create_table(options, table_name) click to toggle source
# File lib/threads/dynamodb.rb, line 60
def create_table(options, table_name)
  dynamodb.create_table(options)

  dynamodb.wait_until(:table_exists, {table_name: table_name}) do |w|
    w.max_attempts = 5
    w.delay = 5
  end
end
dynamodb() click to toggle source
# File lib/threads/dynamodb.rb, line 87
def dynamodb
  @dynamodb ||= Aws::DynamoDB::Client.new(
    region: Settings.aws.dynamodb.region,
    profile: Settings.aws.profile
  )
end
get_item(instance_id) click to toggle source
# File lib/threads/dynamodb.rb, line 73
def get_item(instance_id)
  dynamodb.get_item(
    table_name: Settings.aws.dynamodb.status_table_name,
    key: {instance_id: instance_id}
  )
end
put_item(instance_id, status) click to toggle source
# File lib/threads/dynamodb.rb, line 80
def put_item(instance_id, status)
  dynamodb.put_item(
    table_name: Settings.aws.dynamodb.status_table_name,
    item: {instance_id: instance_id, status: status}
  )
end
raise_not_exist() click to toggle source
# File lib/threads/dynamodb.rb, line 69
def raise_not_exist
  raise "instance_id doesn't exist on status table."
end