Sha256: 192cd3198459b465b0dae69cfa565ddb563f03295eeb5cbbbdecd24357d62d90

Contents?: true

Size: 1.43 KB

Versions: 4

Compression:

Stored size: 1.43 KB

Contents

require 'aws-sdk'
require 'thread'
require 'timeout'

module Alephant
  module Lookup
    class LookupTable
      attr_reader :table_name

      TIMEOUT = 120
      DEFAULT_CONFIG = {
        :write_units => 5,
        :read_units  => 10
      }
      SCHEMA = {
        :hash_key => {
          :component_id => :string
        },
        :range_key => {
          :opts_hash => :string
        }
      }

      def initialize(table_name, config = DEFAULT_CONFIG)
        @mutex      = Mutex.new
        @dynamo_db  = AWS::DynamoDB.new
        @table_name = table_name
        @config     = config
      end

      def create
        @mutex.synchronize do
          ensure_table_exists
          ensure_table_active
        end
      end

      def table
        @table ||= @dynamo_db.tables[@table_name]
      end

      private

      def ensure_table_exists
        create_dynamodb_table unless table.exists?
      end

      def ensure_table_active
        sleep_until_table_active unless table_active?
      end

      def create_dynamodb_table
        @table = @dynamo_db.tables.create(
          @table_name,
          @config[:read_units],
          @config[:write_units],
          SCHEMA
        )
      end

      def table_active?
        table.status == :active
      end

      def sleep_until_table_active
        begin
          Timeout::timeout(TIMEOUT) do
            sleep 1 until table_active?
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
alephant-lookup-0.1.2 lib/alephant/lookup/lookup_table.rb
alephant-lookup-0.1.1 lib/alephant/lookup/lookup_table.rb
alephant-lookup-0.1.0 lib/alephant/lookup/lookup_table.rb
alephant-lookup-0.0.4 lib/alephant/lookup/lookup_table.rb