Class: Aspera::Plugins::Transferd::Agent

Inherits:
Agent::Base show all
Defined in:
lib/aspera/plugins/transferd/agent.rb

Overview

Transfers with a Transfer Daemon (transferd): an external one, or one started here (Product). Calls to a running daemon: Api.

Instance Attribute Summary collapse

Attributes inherited from Agent::Base

#transfer_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Agent::Base

#in_process?, #last_job_id, #local_destination?, #wait_for_completion

Constructor Details

#initialize(url: AUTO_LOCAL_TCP_PORT, start: true, stop: true, api_key: nil, ca_file: nil, **base) ⇒ Agent

Connect to the daemon of url, and start it if it does not answer (unless start is false)

Parameters:

  • url (String) (defaults to: AUTO_LOCAL_TCP_PORT) —

    URL of the transfer manager daemon, e.g. 127.0.0.1:55002, or only its port (address 127.0.0.1)

  • start (Boolean) (defaults to: true) —

    If false, expect that an external daemon is already running

  • stop (Boolean) (defaults to: true) —

    If false, do not shutdown daemon on exit

  • api_key (String, nil) (defaults to: nil) —

    API key of the daemon: of an external daemon with authentication enabled, or of the daemon started here (else a random one)

  • ca_file (String, nil) (defaults to: nil) —

    Certificate authority (PEM file) of an external daemon with tls enabled

  • base (Hash) —

    Base class options

Raises:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/aspera/plugins/transferd/agent.rb', line 100

def initialize(
  url:     AUTO_LOCAL_TCP_PORT,
  start:   true,
  stop:    true,
  api_key: nil,
  ca_file: nil,
  **base
)
  super(**base)
  @transfer_id = nil
  @stop = stop
  is_local_auto_port = url.eql?(AUTO_LOCAL_TCP_PORT)
  Aspera.assert(!(is_local_auto_port && (!@stop || !start)), type: ParameterError) { 'Cannot set options `stop` or `start` to false with port zero' }
  # daemon started here, kept for optional shutdown
  @daemon = nil
  # no address: local address
  @daemon_endpoint = Api.endpoint(url)
  Log.dump(:daemon_endpoint, @daemon_endpoint)
  begin
    connect(api_key, ca_file)
    Log.log.warn('Attached to existing daemon') if start && @stop
    # `shutdown` is registered with `at_exit` only when the daemon is started here:
    # a long-running process (e.g. Orchestrator) that attaches to an external daemon for each transfer would accumulate them
  rescue GRPC::Unavailable => e
    # if transferd is external: do not start it, or other error
    raise if !start || !e.message.include?('failed to connect')
    start_daemon(api_key, ca_file, auto_port: is_local_auto_port)
  end
end

Instance Attribute Details

#daemon_endpoint ⇒ Object (readonly)

Actual endpoint the daemon is listening on (resolved after connect, e.g. when port 0 was used)



131
132
133
# File 'lib/aspera/plugins/transferd/agent.rb', line 131

def daemon_endpoint
  @daemon_endpoint
end

#transfer_client ⇒ Object (readonly)

gRPC client of the daemon



133
134
135
# File 'lib/aspera/plugins/transferd/agent.rb', line 133

def transfer_client
  @transfer_client
end

Class Method Details

.empty_message(name) ⇒ Google::Protobuf::AbstractMessage

Returns empty gRPC message.

Parameters:

  • name (Symbol) —

    see Client.empty_message

Returns:

  • (Google::Protobuf::AbstractMessage) —

    empty gRPC message



70
71
72
# File 'lib/aspera/plugins/transferd/agent.rb', line 70

def empty_message(name)
  Client.empty_message(name)
end

.remote_daemon?(url) ⇒ Boolean

Returns true if the daemon is on a remote host.

Parameters:

  • url (String, nil) —

    daemon endpoint <address>:<port> (or :<port>, or absent: a local daemon)

Returns:

  • (Boolean) —

    true if the daemon is on a remote host



55
56
57
58
59
60
# File 'lib/aspera/plugins/transferd/agent.rb', line 55

def remote_daemon?(url)
  return false if url.nil?
  host = url.to_s.sub(%r{\A\w+://}, '')
  host = host[0...host.rindex(':')] if host.include?(':')
  !host.empty? && !LOCAL_DAEMON_HOSTS.include?(host.downcase)
end

.required_security_level(options) ⇒ Symbol?

A daemon started here, or attached to on the local host, runs on the local host: refused at level high (see Cli::Security). A daemon on a remote host is provided and controlled by the user: allowed at any level.

Parameters:

  • options (Hash) —

    agent parameters (url is the daemon endpoint, default: a local daemon)

Returns:

  • (Symbol, nil) —

    :medium for a local daemon, nil (any level) for a remote one



49
50
51
# File 'lib/aspera/plugins/transferd/agent.rb', line 49

def required_security_level(options)
  remote_daemon?(options[:url]) ? nil : :medium
end

.transfer_cancel(transfer_id, agent_params) ⇒ Boolean

Stop a transfer: see Api.transfer_cancel

Parameters:

  • transfer_id (String) —

    Identifier of transfer, given by the daemon

  • agent_params (Hash) —

    url: endpoint of the daemon; optional timeout: seconds, api_key, ca_file

Returns:

  • (Boolean) —

    true: stopped

Raises:



87
88
89
# File 'lib/aspera/plugins/transferd/agent.rb', line 87

def transfer_cancel(transfer_id, agent_params)
  Api.transfer_cancel(transfer_id, agent_params)
end

.transfer_status(transfer_id, agent_params) ⇒ Hash

Re-query a previously started transfer: see Api.transfer_status

Parameters:

  • transfer_id (String) —

    Identifier of transfer, given by the daemon (transfer_id)

  • agent_params (Hash) —

    url: endpoint of the daemon; optional timeout: seconds, api_key, ca_file

Returns:

  • (Hash) —

    Normalized status



78
79
80
# File 'lib/aspera/plugins/transferd/agent.rb', line 78

def transfer_status(transfer_id, agent_params)
  Api.transfer_status(transfer_id, agent_params)
end

.wrap_client(stub) ⇒ Client

Returns Calls to this daemon with plain Ruby arguments.

Parameters:

Returns:

  • (Client) —

    Calls to this daemon with plain Ruby arguments



64
65
66
# File 'lib/aspera/plugins/transferd/agent.rb', line 64

def wrap_client(stub)
  Client.new(stub)
end

Instance Method Details

#async_params ⇒ Hash

Returns url: actual endpoint of the daemon, for transfer_status and transfer_cancel (transfer_id is given by the daemon).

Returns:

  • (Hash) —

    url: actual endpoint of the daemon, for transfer_status and transfer_cancel (transfer_id is given by the daemon)



141
142
143
# File 'lib/aspera/plugins/transferd/agent.rb', line 141

def async_params
  {'url' => @daemon_endpoint}
end

#client ⇒ Client

Returns Calls to the daemon with plain Ruby arguments.

Returns:

  • (Client) —

    Calls to the daemon with plain Ruby arguments



136
137
138
# File 'lib/aspera/plugins/transferd/agent.rb', line 136

def client
  @client ||= Client.new(@transfer_client)
end

#shutdown ⇒ Object



199
200
201
# File 'lib/aspera/plugins/transferd/agent.rb', line 199

def shutdown
  @daemon&.stop if @stop
end

#start_transfer(transfer_spec, token_regenerator: nil) ⇒ Object

:reek:UnusedParameters token_regenerator



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/aspera/plugins/transferd/agent.rb', line 146

def start_transfer(transfer_spec, token_regenerator: nil)
  Aspera::Transfer::Spec.fix_transferd_resume_policy(transfer_spec)
  # create a transfer request
  transfer_request = ::Transferd::Api::TransferRequest.new(
    transferType: ::Transferd::Api::TransferType::FILE_REGULAR, # transfer type (file/stream)
    config: ::Transferd::Api::TransferConfig.new, # transfer configuration
    transferSpec: transfer_spec.to_json
  ) # transfer definition
  # send start transfer request to the transfer manager daemon
  start_response = @transfer_client.start_transfer(transfer_request)
  Aspera.assert(!start_response.status.eql?(:FAILED), type: Aspera::Transfer::Error) { start_response.error.description }
  Log.log.debug { "start transfer response #{start_response}" }
  @transfer_id = start_response.transferId
  Log.log.debug { "transfer started with id #{@transfer_id}" }
end

#wait_for_transfers_completion ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/aspera/plugins/transferd/agent.rb', line 162

def wait_for_transfers_completion
  # set to true when we know the total size of the transfer
  session_started = false
  bytes_expected = nil
  # monitor transfer status
  @transfer_client.monitor_transfers(::Transferd::Api::RegistrationRequest.new(transferId: [@transfer_id])) do |response|
    Log.dump(:response, response.to_h)
    # Log.log.debug{"#{response.sessionInfo.preTransferBytes} #{response.transferInfo.bytesTransferred}"}
    case response.status
    when :RUNNING
      if !session_started
        notify_progress(:session_start, session_id: @transfer_id)
        session_started = true
      end
      if bytes_expected.nil? &&
          !response.sessionInfo.preTransferBytes.eql?(0)
        bytes_expected = response.sessionInfo.preTransferBytes
        notify_progress(:session_size, session_id: @transfer_id, info: bytes_expected)
      end
      notify_progress(:transfer, session_id: @transfer_id, info: response.transferInfo.bytesTransferred)
    when :COMPLETED
      notify_progress(:transfer, session_id: @transfer_id, info: bytes_expected) if bytes_expected
      notify_progress(:session_end, session_id: @transfer_id)
      break
    when :FAILED, :CANCELED
      notify_progress(:session_end, session_id: @transfer_id)
      raise Aspera::Transfer::Error, JSON.parse(response.message)['Description']
    when :QUEUED, :UNKNOWN_STATUS, :PAUSED, :ORPHANED
      notify_progress(:sessions_init, info: response.status.to_s.downcase)
    else
      Log.log.error { "unknown status#{response.status}" }
    end
  end
  # TODO: return status
  return []
end