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::PACKET_SIZE_REGEXP, 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
:skewed_by(Integer|nil):: Amount of skew already applied to expires_at in seconds
: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



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/right_agent/packets.rb', line 315

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
  @skewed_by  = opts[:skewed_by] || 0
  @tags       = opts[:tags] || []
  @tries      = opts[:tries] || []
  @version    = version
  @size       = size
end

Instance Attribute Details

#expires_atObject

Returns the value of attribute expires_at.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def expires_at
  @expires_at
end

#fromObject

Returns the value of attribute from.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def from
  @from
end

#payloadObject

Returns the value of attribute payload.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def payload
  @payload
end

#persistentObject

Returns the value of attribute persistent.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def persistent
  @persistent
end

#reply_toObject

Returns the value of attribute reply_to.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def reply_to
  @reply_to
end

#scopeObject

Returns the value of attribute scope.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def scope
  @scope
end

#selectorObject

Returns the value of attribute selector.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def selector
  @selector
end

#skewed_byObject

Returns the value of attribute skewed_by.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def skewed_by
  @skewed_by
end

#tagsObject

Returns the value of attribute tags.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def tags
  @tags
end

#targetObject

Returns the value of attribute target.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def target
  @target
end

#tokenObject

Returns the value of attribute token.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def token
  @token
end

#triesObject

Returns the value of attribute tries.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def tries
  @tries
end

#typeObject

Returns the value of attribute type.



287
288
289
# File 'lib/right_agent/packets.rb', line 287

def type
  @type
end

Class Method Details

.create(o) ⇒ Object

Create packet from unmarshalled data

Parameters

o(Hash)

Unmarshalled data

Return

(Request)

New packet



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/right_agent/packets.rb', line 350

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,                 :skewed_by  => i['skewed_by'],
                                 :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)


339
340
341
# File 'lib/right_agent/packets.rb', line 339

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



411
412
413
# File 'lib/right_agent/packets.rb', line 411

def one_way
  false
end

#target_for_encryptionObject

Get target to be used for encrypting the packet

Return

(String)

Target



403
404
405
# File 'lib/right_agent/packets.rb', line 403

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



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/right_agent/packets.rb', line 375

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



395
396
397
# File 'lib/right_agent/packets.rb', line 395

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