Sha256: 7d0a49278c5389cb79a586c6c86890e1dac2d6428403f2243835c67399a3459c

Contents?: true

Size: 1.69 KB

Versions: 12

Compression:

Stored size: 1.69 KB

Contents

module Remi

  class DataTarget::SftpFile < Remi::DataSubject
    include Remi::DataSubject::DataTarget

    def initialize(*args, **kargs, &block)
      super
      init_sftp_file(*args, **kargs, &block)
    end

    attr_reader :local_path
    attr_reader :remote_path

    # Public: Performs the load operation, regardless of whether it has
    # already executed.
    #
    # Returns true if the load operation was successful
    def load!
      @logger.info "Uploading #{@local_path} to #{@credentials[:username]}@#{@credentials[:host]}: #{@remote_path}"
      connection do |sftp|
        retry_upload { sftp.upload! @local_path, @remote_path }
      end

      true
    end


    private

    def init_sftp_file(*args, credentials:, local_path:, remote_path: File.basename(local_path), **kargs, &block)
      @credentials = credentials
      @local_path = local_path
      @remote_path = remote_path
      init_df
    end

    def init_df
      parameter_df = Daru::DataFrame.new(
        local_path: Array(@local_path),
        remote_path: Array(@remote_path)
      )
      self.df = parameter_df
    end

    def connection(&block)
      result = nil
      Net::SFTP.start(@credentials[:host], @credentials[:username], password: @credentials[:password], port: @credentials[:port] || '22') do |sftp|
        result = yield sftp
      end
      result
    end

    def retry_upload(ntry=2, &block)
      1.upto(ntry).each do |itry|
        begin
          block.call
        rescue RuntimeError => err
          raise err unless itry < ntry
          @logger.error "Upload failed with error: #{err.message}"
          @logger.error "Retry attempt #{itry}/#{ntry-1}"
          sleep(1)
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
remi-0.2.39 lib/remi/data_subject/sftp_file.rb
remi-0.2.38 lib/remi/data_subject/sftp_file.rb
remi-0.2.37 lib/remi/data_subject/sftp_file.rb
remi-0.2.36 lib/remi/data_subject/sftp_file.rb
remi-0.2.35 lib/remi/data_subject/sftp_file.rb
remi-0.2.34 lib/remi/data_subject/sftp_file.rb
remi-0.2.33 lib/remi/data_subject/sftp_file.rb
remi-0.2.32 lib/remi/data_subject/sftp_file.rb
remi-0.2.31 lib/remi/data_subject/sftp_file.rb
remi-0.2.30 lib/remi/data_subject/sftp_file.rb
remi-0.2.29 lib/remi/data_subject/sftp_file.rb
remi-0.2.28 lib/remi/data_subject/sftp_file.rb