Class: Etna::Clients::Metis::MetisDownloadWorkflow

Inherits:
Struct
  • Object
show all
Defined in:
lib/etna/clients/metis/workflows/metis_download_workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ MetisDownloadWorkflow

Returns a new instance of MetisDownloadWorkflow.



11
12
13
# File 'lib/etna/clients/metis/workflows/metis_download_workflow.rb', line 11

def initialize(args)
  super({max_attempts: 3}.update(args))
end

Instance Attribute Details

#bucket_nameObject

Returns the value of attribute bucket_name

Returns:

  • (Object)

    the current value of bucket_name



9
10
11
# File 'lib/etna/clients/metis/workflows/metis_download_workflow.rb', line 9

def bucket_name
  @bucket_name
end

#max_attemptsObject

Returns the value of attribute max_attempts

Returns:

  • (Object)

    the current value of max_attempts



9
10
11
# File 'lib/etna/clients/metis/workflows/metis_download_workflow.rb', line 9

def max_attempts
  @max_attempts
end

#metis_clientObject

Returns the value of attribute metis_client

Returns:

  • (Object)

    the current value of metis_client



9
10
11
# File 'lib/etna/clients/metis/workflows/metis_download_workflow.rb', line 9

def metis_client
  @metis_client
end

#project_nameObject

Returns the value of attribute project_name

Returns:

  • (Object)

    the current value of project_name



9
10
11
# File 'lib/etna/clients/metis/workflows/metis_download_workflow.rb', line 9

def project_name
  @project_name
end

Instance Method Details

#do_download(dest_file_or_io, metis_file, &block) ⇒ Object

TODO: Might be possible to use range headers to select and resume downloads on failure in the future.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/etna/clients/metis/workflows/metis_download_workflow.rb', line 16

def do_download(dest_file_or_io, metis_file, &block)
  size = metis_file.size
  completed = 0.0
  start = Time.now

  unless dest_file_or_io.is_a?(IO) || dest_file_or_io.is_a?(StringIO)
    ::File.open(dest_file_or_io, 'w') do |io|
      return do_download(dest_file_or_io, metis_file, &block)
    end
  end

  metis_client.download_file(metis_file) do |chunk|
    dest_file_or_io.write chunk
    completed += chunk.size

    block.call([
        :progress,
        size == 0 ? 1 : completed / size,
        (completed / (Time.now - start)).round(2),
    ]) unless block.nil?
  end
end