Class: Aspera::Fasp::Node

Inherits:
Manager
  • Object
show all
Defined in:
lib/aspera/fasp/node.rb

Overview

this singleton class is used by the CLI to provide a common interface to start a transfer before using it, the use must set the ‘node_api` member.

Constant Summary

Constants inherited from Manager

Manager::LISTENER_SESSION_ID_B, Manager::LISTENER_SESSION_ID_S

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Manager

#add_listener, validate_status_list

Constructor Details

#initialize(node_api) ⇒ Node

Returns a new instance of Node.



10
11
12
13
14
15
# File 'lib/aspera/fasp/node.rb', line 10

def initialize(node_api)
  super()
  @node_api=node_api
  # TODO: currently only supports one transfer. This is bad shortcut. but ok for CLI.
  @transfer_id=nil
end

Instance Attribute Details

#node_apiObject

use this to read the node_api end point.



23
24
25
# File 'lib/aspera/fasp/node.rb', line 23

def node_api
  @node_api
end

Instance Method Details

#node_api_Object

used internally to ensure node api is set before using.

Raises:

  • (StandardError)


18
19
20
21
# File 'lib/aspera/fasp/node.rb', line 18

def node_api_
  raise StandardError,'Before using this object, set the node_api attribute to a Aspera::Rest object' if @node_api.nil?
  return @node_api
end

#start_transfer(transfer_spec, options = nil) ⇒ Object

generic method



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/aspera/fasp/node.rb', line 34

def start_transfer(transfer_spec,options=nil)
  if transfer_spec['tags'].is_a?(Hash) and transfer_spec['tags']['aspera'].is_a?(Hash)
    transfer_spec['tags']['aspera']['xfer_retry']||=150
  end
  # optimisation in case of sending to the same node
  if transfer_spec['remote_host'].eql?(URI.parse(node_api_.params[:base_url]).host)
    transfer_spec['remote_host']='localhost'
  end
  resp=node_api_.create('ops/transfers',transfer_spec)[:data]
  @transfer_id=resp['id']
  Log.log.debug("tr_id=#{@transfer_id}")
  return @transfer_id
end

#wait_for_transfers_completionObject

generic method



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
78
79
80
81
82
83
84
85
# File 'lib/aspera/fasp/node.rb', line 49

def wait_for_transfers_completion
  started=false
  spinner=nil
  # lets emulate management events to display progress bar
  loop do
    # status is empty sometimes with status 200...
    trdata=node_api_.read("ops/transfers/#{@transfer_id}")[:data] || {"status"=>"unknown"} rescue {"status"=>"waiting(read error)"}
    case trdata['status']
    when 'completed'
      notify_listeners('emulated',{Manager::LISTENER_SESSION_ID_B=>@transfer_id,'Type'=>'DONE'})
      break
    when 'waiting','partially_completed','unknown','waiting(read error)'
      if spinner.nil?
        spinner = TTY::Spinner.new("[:spinner] :title", format: :classic)
        spinner.start
      end
      spinner.update(title: trdata['status'])
      spinner.spin
      #puts trdata
    when 'running'
      #puts "running: sessions:#{trdata["sessions"].length}, #{trdata["sessions"].map{|i| i['bytes_transferred']}.join(',')}"
      if !started and trdata['precalc'].is_a?(Hash) and
      trdata['precalc']['status'].eql?('ready')
        notify_listeners('emulated',{Manager::LISTENER_SESSION_ID_B=>@transfer_id,'Type'=>'NOTIFICATION','PreTransferBytes'=>trdata['precalc']['bytes_expected']})
        started=true
      else
        notify_listeners('emulated',{Manager::LISTENER_SESSION_ID_B=>@transfer_id,'Type'=>'STATS','Bytescont'=>trdata['bytes_transferred']})
      end
    else
      Log.log.warn("trdata -> #{trdata}")
      raise Fasp::Error.new("#{trdata['status']}: #{trdata['error_desc']}")
    end
    sleep 1
  end
  #TODO get status of sessions
  return [] 
end