Class: Ecoportal::API::V2::S3::Files::BatchUpload
- Inherits:
-
Object
- Object
- Ecoportal::API::V2::S3::Files::BatchUpload
- 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
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#files_api ⇒ Object
readonly
Returns the value of attribute files_api.
Instance Method Summary collapse
-
#initialize(files, files_api:) ⇒ BatchUpload
constructor
A new instance of BatchUpload.
-
#upload!(benchmarking: false, threads: 1, **kargs) ⇒ Object
Do the actual upload of the file.
Constructor Details
#initialize(files, files_api:) ⇒ BatchUpload
Returns a new instance of BatchUpload.
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
#files ⇒ Object (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_api ⇒ Object (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
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 |