Sha256: 06356a4cdf4ee12db0a8a02933c69b1cca90126b056ce5c77b4cfc732fe796ab

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module FlareUp

  class Boot

    # TODO: This control flow is untested and too procedural
    def self.boot(options)
      conn = create_connection(options)
      copy = create_copy_command(options)

      begin
        trap('SIGINT') do
          Emitter.warn('CTRL-C received; cancelling COPY command...')
          error_message = conn.cancel_current_command
          if error_message
            Emitter.error("Error cancelling COPY: #{error_message}")
          else
            Emitter.success('COPY command cancelled.')
          end
          CLI.bailout(1)
        end

        Emitter.info("Executing command: #{copy.get_command}")
        handle_load_errors(copy.execute(conn))
      rescue ConnectionError => e
        Emitter.error(e.message)
        CLI.bailout(1)
      rescue CopyCommandError => e
        Emitter.error(e.message)
        CLI.bailout(1)
      end

    end

    def self.create_connection(options)
      FlareUp::Connection.new(
        options[:redshift_endpoint],
        options[:database],
        options[:redshift_username],
        options[:redshift_password]
      )
    end

    def self.create_copy_command(options)
      copy = FlareUp::CopyCommand.new(
        options[:table],
        options[:data_source],
        options[:aws_access_key],
        options[:aws_secret_key]
      )
      copy.columns = options[:column_list] if options[:column_list]
      copy.options = options[:copy_options] if options[:copy_options]
      copy
    end

    # TODO: How can we test this?
    def self.handle_load_errors(stl_load_errors)
      return if stl_load_errors.empty?
      Emitter.error("There was an error processing the COPY command.  Displaying the last (#{stl_load_errors.length}) errors.")
      stl_load_errors.each do |e|
        Emitter.error(e.pretty_print)
      end
      CLI.bailout(1)
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flare-up-0.7 lib/flare_up/boot.rb