Class: RightScale::Request

Inherits:
Packet show all
Defined in:
lib/right_agent/packets.rb

Overview

Packet for a work request for an actor node that has an expected result

Constant Summary collapse

DEFAULT_OPTIONS =
{:selector => :any}

Constants inherited from Packet

Packet::DEFAULT_VERSION, Packet::GLOBAL, Packet::NOT_SERIALIZED, Packet::VERSION

Instance Attribute Summary collapse

Attributes inherited from Packet

#received_at, #size

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Packet

compatible, #enough_precision, #id_to_s, #ids_to_s, json_create, msgpack_create, #name, #recv_version, #send_version, #send_version=, #to_json, #to_msgpack, #trace

Constructor Details

#initialize(type, payload, opts = {}, version = [VERSION, VERSION], size = nil) ⇒ Request

Create packet

Parameters

type(String)

Dispatch route for the request

payload(Any)

Arbitrary data that is transferred to actor

opts(Hash)

Optional settings:

:from(String)

Sender identity

:scope(Hash)

Define behavior that should be used to resolve tag based routing

:token(String)

Generated request id that a router uses to identify replies

:reply_to(String)

Identity of the node that actor replies to, usually a router itself

:selector(Symbol)

Selector used to route the request: :any or :all, defaults to :any,

  :all deprecated for version 13 and above
:target(String|Array):: Target recipient(s)
:persistent(Boolean):: Indicates if this request should be saved to persistent storage
  by the AMQP broker
:expires_at(Integer|nil):: Time in seconds in Unix-epoch when this request expires and
   is to be ignored by the receiver; value 0 means never expire; defaults to 0
:tags(Array(Symbol)):: List of tags to be used for selecting target for this request
:tries(Array):: List of tokens for previous attempts to send this request
version(Array)

Protocol version of the original creator of the packet followed by the

protocol version of the packet contents to be used when sending
size(Integer)

Size of request in bytes used only for marshalling



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/right_agent/packets.rb', line 312

def initialize(type, payload, opts = {}, version = [VERSION, VERSION], size = nil)
  opts = DEFAULT_OPTIONS.merge(opts)
  @type       = type
  @payload    = payload
  @from       = opts[:from]
  @scope      = opts[:scope]
  @token      = opts[:token]
  @reply_to   = opts[:reply_to]
  @selector   = opts[:selector]
  @selector   = :any if ["least_loaded", "random"].include?(@selector.to_s)
  @target     = opts[:target]
  @persistent = opts[:persistent]
  @expires_at = opts[:expires_at] || 0
  @tags       = opts[:tags] || []
  @tries      = opts[:tries] || []
  @version    = version
  @size       = size
end

Instance Attribute Details

#expires_atObject

Returns the value of attribute expires_at.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def expires_at
  @expires_at
end

#fromObject

Returns the value of attribute from.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def from
  @from
end

#payloadObject

Returns the value of attribute payload.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def payload
  @payload
end

#persistentObject

Returns the value of attribute persistent.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def persistent
  @persistent
end

#reply_toObject

Returns the value of attribute reply_to.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def reply_to
  @reply_to
end

#scopeObject

Returns the value of attribute scope.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def scope
  @scope
end

#selectorObject

Returns the value of attribute selector.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def selector
  @selector
end

#tagsObject

Returns the value of attribute tags.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def tags
  @tags
end

#targetObject

Returns the value of attribute target.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def target
  @target
end

#tokenObject

Returns the value of attribute token.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def token
  @token
end

#triesObject

Returns the value of attribute tries.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def tries
  @tries
end

#typeObject

Returns the value of attribute type.



285
286
287
# File 'lib/right_agent/packets.rb', line 285

def type
  @type
end

Class Method Details

.create(o) ⇒ Object

Create packet from unmarshalled data

Parameters

o(Hash)

Unmarshalled data

Return

(Request)

New packet



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/right_agent/packets.rb', line 346

def self.create(o)
  i = o['data']
  expires_at = if i.has_key?('created_at')
    created_at = i['created_at'].to_i
    created_at > 0 ? created_at + (15 * 60) : 0
  else
    i['expires_at']
  end
  new(i['type'], i['payload'], { :from       => self.compatible(i['from']), :scope      => i['scope'],
                                 :token      => i['token'],                 :reply_to   => self.compatible(i['reply_to']),
                                 :selector   => i['selector'],              :target     => self.compatible(i['target']),
                                 :persistent => i['persistent'],            :tags       => i['tags'],
                                 :expires_at => expires_at,                 :tries      => i['tries'] },
      i['version'] || [DEFAULT_VERSION, DEFAULT_VERSION], o['size'])
end

Instance Method Details

#fanout?Boolean

Test whether the request is being fanned out to multiple targets

Return

(Boolean)

true if is multicast, otherwise false

Returns:

  • (Boolean)


335
336
337
# File 'lib/right_agent/packets.rb', line 335

def fanout?
  @selector.to_s == 'all'
end

#one_wayObject

Whether the packet is one that does not have an associated response

Return

false

Always return false



406
407
408
# File 'lib/right_agent/packets.rb', line 406

def one_way
  false
end

#target_for_encryptionObject

Get target to be used for encrypting the packet

Return

(String)

Target



398
399
400
# File 'lib/right_agent/packets.rb', line 398

def target_for_encryption
  @target
end

#to_s(filter = nil, version = nil) ⇒ Object

Generate log representation

Parameters

filter(Array(Symbol))

Attributes to be included in output

version(Symbol|nil)

Version to display: :recv_version, :send_version, or nil meaning none

Return

log_msg(String)

Log representation



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/right_agent/packets.rb', line 370

def to_s(filter = nil, version = nil)
  payload = PayloadFormatter.log(@type, @payload)
  log_msg = "#{super(filter, version)} #{trace} #{@type}"
  log_msg += " #{payload}" if payload
  log_msg += " from #{id_to_s(@from)}" if filter.nil? || filter.include?(:from)
  log_msg += ", target #{ids_to_s(@target)}" if @target && (filter.nil? || filter.include?(:target))
  log_msg += ", scope #{@scope.inspect}" if @scope && (filter.nil? || filter.include?(:scope))
  log_msg += ", fanout" if (filter.nil? || filter.include?(:fanout)) && fanout?
  log_msg += ", reply_to #{id_to_s(@reply_to)}" if @reply_to && (filter.nil? || filter.include?(:reply_to))
  log_msg += ", tags #{@tags.inspect}" if @tags && !@tags.empty? && (filter.nil? || filter.include?(:tags))
  log_msg += ", persistent" if @persistent && (filter.nil? || filter.include?(:persistent))
  log_msg += ", tries #{tries_to_s}" if @tries && !@tries.empty? && (filter.nil? || filter.include?(:tries))
  log_msg += ", payload #{@payload.inspect}" if filter && filter.include?(:payload)
  log_msg
end

#tries_to_sObject

Convert tries list to string representation

Return

log_msg(String)

Tries list



390
391
392
# File 'lib/right_agent/packets.rb', line 390

def tries_to_s
  @tries.map { |t| "<#{t}>" }.join(", ")
end