Sha256: 9465961af7e379cb8fb766ab3f17565ec4a4a8438b91ce10e245f38ffbffea29

Contents?: true

Size: 1.24 KB

Versions: 12

Compression:

Stored size: 1.24 KB

Contents

module Remi
  module DataTarget
    class SftpFile
      include DataTarget

      def initialize(credentials:, local_path:, remote_path: File.basename(local_path), logger: Remi::Settings.logger)
        @credentials = credentials
        @local_path = local_path
        @remote_path = remote_path
        @logger = logger
      end

      attr_reader :local_path
      attr_reader :remote_path

      def load
        return true if @loaded

        connection do |sftp|
          retry_upload { sftp.upload! @local_path, @remote_path }
        end

        @loaded = true
      end



      private

      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
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
remi-0.2.27 lib/remi/data_target/sftp_file.rb
remi-0.2.26 lib/remi/data_target/sftp_file.rb
remi-0.2.25 lib/remi/data_target/sftp_file.rb
remi-0.2.24 lib/remi/data_target/sftp_file.rb
remi-0.2.23 lib/remi/data_target/sftp_file.rb
remi-0.2.22 lib/remi/data_target/sftp_file.rb
remi-0.2.21 lib/remi/data_target/sftp_file.rb
remi-0.2.20 lib/remi/data_target/sftp_file.rb
remi-0.2.19 lib/remi/data_target/sftp_file.rb
remi-0.2.18 lib/remi/data_target/sftp_file.rb
remi-0.2.17 lib/remi/data_target/sftp_file.rb
remi-0.2.16 lib/remi/data_target/sftp_file.rb