Class: Aspera::Plugins::Transferd::Agent
- Inherits:
-
Agent::Base
- Object
- Agent::Base
- Aspera::Plugins::Transferd::Agent
- 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
-
#daemon_endpoint ⇒ Object
readonly
Actual endpoint the daemon is listening on (resolved after connect, e.g. when port 0 was used).
-
#transfer_client ⇒ Object
readonly
gRPC client of the daemon.
Attributes inherited from Agent::Base
Class Method Summary collapse
-
.empty_message(name) ⇒ Google::Protobuf::AbstractMessage
Empty gRPC message.
-
.remote_daemon?(url) ⇒ Boolean
trueif the daemon is on a remote host. -
.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(seeCli::Security). -
.transfer_cancel(transfer_id, agent_params) ⇒ Boolean
Stop a transfer: see
Api.transfer_cancel. -
.transfer_status(transfer_id, agent_params) ⇒ Hash
Re-query a previously started transfer: see
Api.transfer_status. -
.wrap_client(stub) ⇒ Client
Calls to this daemon with plain Ruby arguments.
Instance Method Summary collapse
-
#async_params ⇒ Hash
url: actual endpoint of the daemon, fortransfer_statusandtransfer_cancel(transfer_idis given by the daemon). -
#client ⇒ Client
Calls to the daemon with plain Ruby arguments.
-
#initialize(url: AUTO_LOCAL_TCP_PORT, start: true, stop: true, api_key: nil, ca_file: nil, **base) ⇒ Agent
constructor
Connect to the daemon of
url, and start it if it does not answer (unlessstartisfalse). - #shutdown ⇒ Object
-
#start_transfer(transfer_spec, token_regenerator: nil) ⇒ Object
:reek:UnusedParameters token_regenerator.
- #wait_for_transfers_completion ⇒ Object
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)
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..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.
70 71 72 |
# File 'lib/aspera/plugins/transferd/agent.rb', line 70 def (name) Client.(name) end |
.remote_daemon?(url) ⇒ Boolean
Returns 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.
49 50 51 |
# File 'lib/aspera/plugins/transferd/agent.rb', line 49 def required_security_level() remote_daemon?([:url]) ? nil : :medium end |
.transfer_cancel(transfer_id, agent_params) ⇒ Boolean
Stop a transfer: see Api.transfer_cancel
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
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 |
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).
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.
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.)['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 |