Class: Ecoportal::API::V2::S3::Files::BatchUpload

Inherits:
Object
  • Object
show all
Includes:
Common::Concerns::Benchmarkable, Common::Concerns::Threadable
Defined in:
lib/ecoportal/api/v2/s3/files/batch_upload.rb

Overview

Class service to upload multiple files in one go

Defined Under Namespace

Classes: FileResult

Constant Summary collapse

SIZE_UNITS =

KB

1_024
MIN_UNIT =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, files_api:) ⇒ BatchUpload

Returns a new instance of BatchUpload.

Parameters:

  • files (Array<String>)

    the files to be uploaded

  • files_api (API::S3::Files)

    the api object.

[View source]

35
36
37
38
# File 'lib/ecoportal/api/v2/s3/files/batch_upload.rb', line 35

def initialize(files, files_api:)
  @files     = [files].flatten.compact
  @files_api = files_api
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.


14
15
16
# File 'lib/ecoportal/api/v2/s3/files/batch_upload.rb', line 14

def files
  @files
end

#files_apiObject (readonly)

Returns the value of attribute files_api.


14
15
16
# File 'lib/ecoportal/api/v2/s3/files/batch_upload.rb', line 14

def files_api
  @files_api
end

Instance Method Details

#upload!(benchmarking: false, threads: 1, **kargs) ⇒ Object

Do the actual upload of the file

[View source]

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ecoportal/api/v2/s3/files/batch_upload.rb', line 41

def upload!(benchmarking: false, threads: 1, **kargs) # rubocop:disable Metrics/AbcSize
  bench        = benchmarking && benchmark_enabled?
  bench_mth    = "#{self.class}##{__method__}"
  thr_children = []
  benchmarking("#{bench_mth}.#{files.count}_files", print: bench) do
    with_preserved_thread_globals(report: false) do
      files.each do |file|
        new_thread(thr_children, max: threads) do
          file_results << (result = FileResult.new(file))

          s3_ref = nil

          kbytes = bench ? file_size(file) : 1

          benchmarking("#{bench_mth}.s3_upload (KB)", units: kbytes, print: bench) do
            s3_ref = result.s3_file_reference = s3_api.upload_file(file)
          end

          benchmarking("##{bench_mth}.ep_poll (KB)", units: kbytes, print: bench) do
            files_api.poll!(s3_ref, **kargs) do |poll|
              result.poll = poll
            end
          end
          yield(result) if block_given?
        rescue *rescued_errors => err # each_file
          result.error = err
          yield(result) if block_given?
        end
      end
    end
  end

  thr_children.each(&:join)
  file_results
ensure
  puts benchmark_summary(:all) if bench
end