Class: RightScale::OperationResult

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/right_agent/operation_result.rb

Overview

Container for status and result of an operation

Constant Summary collapse

SUCCESS =

Result status code

0
ERROR =
1
CONTINUE =
2
RETRY =
3
NON_DELIVERY =
4
MULTICAST =

Deprecated for agents at version 13 or above

5
CANCEL =
6
NON_DELIVERY_REASONS =

Non-delivery reasons

[
  NO_TARGET            = "no target",
  UNKNOWN_TARGET       = "unknown target",
  NO_ROUTE_TO_TARGET   = "no route to target",
  TARGET_NOT_CONNECTED = "target not connected",
  TTL_EXPIRATION       = "TTL expiration",
  RETRY_TIMEOUT        = "retry timeout"
]
MAX_ERROR_SIZE =

Maximum characters included in display of error

60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializable

included

Constructor Details

#initialize(*args) ⇒ OperationResult

Returns a new instance of OperationResult.



59
60
61
62
# File 'lib/right_agent/operation_result.rb', line 59

def initialize(*args)
  @status_code = args[0]
  @content     = args[1] if args.size > 1
end

Instance Attribute Details

#contentObject

(Object) Result data, if any



57
58
59
# File 'lib/right_agent/operation_result.rb', line 57

def content
  @content
end

#status_codeObject

(Integer) Status code



54
55
56
# File 'lib/right_agent/operation_result.rb', line 54

def status_code
  @status_code
end

Class Method Details

.cancel(content = nil) ⇒ Object

Cancel request and never retry

Parameters

content(Object)

Any data associated with cancel, defaults to nil

Return

(OperationResult)

Corresponding result



212
213
214
# File 'lib/right_agent/operation_result.rb', line 212

def self.cancel(content = nil)
  OperationResult.new(CANCEL, content)
end

.continue(content = nil) ⇒ Object

Create new continue status

Parameters

content(Object)

Any data associated with continue, defaults to nil

Return

(OperationResult)

Corresponding result



167
168
169
# File 'lib/right_agent/operation_result.rb', line 167

def self.continue(content = nil)
  OperationResult.new(CONTINUE, content)
end

.error(message, exception = nil, backtrace = :caller) ⇒ Object

Create new error status

Parameters

message(String)

Error description

exception(Exception|String)

Associated exception or other parenthetical error information

backtrace(Symbol)

Exception backtrace extent: :no_trace, :caller, or :trace,

defaults to :caller

Return

(OperationResult)

Corresponding result



156
157
158
# File 'lib/right_agent/operation_result.rb', line 156

def self.error(message, exception = nil, backtrace = :caller)
  OperationResult.new(ERROR, Log.format(message, exception, backtrace))
end

.from_results(result) ⇒ Object

Instantiate from request result Ignore all but first result if result is a hash

Parameters

result(Result|Hash|OperationResult|nil)

Result or the Result “results” field

Return

(RightScale::OperationResult)

Converted operation result



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/right_agent/operation_result.rb', line 110

def self.from_results(result)
  r = result.is_a?(Result) ? result.results : result
  if r && r.respond_to?(:status_code) && r.respond_to?(:content)
    new(r.status_code, r.content)
  elsif r && r.is_a?(Hash) && r.values.size > 0
    r = r.values[0]
    if r.respond_to?(:status_code) && r.respond_to?(:content)
      new(r.status_code, r.content)
    else
      error("Invalid operation result content: #{r.inspect}")
    end
  elsif r && r.is_a?(String)
    # This is not a supported return value but older RightLink versions can incorrectly
    # return a String rather than an OperationResult#error in situations where the actor
    # raises an exception when processing a request
    error(r)
  elsif r.nil?
    error("No results")
  elsif result.is_a?(Result)
    error("Invalid results in Result from #{result.from}: #{result.results.inspect}")
  else
    error("Invalid operation result type: #{result.inspect}")
  end
end

.multicast(targets) ⇒ Object

Create new multicast status Deprecated for agents at version 13 or above

Parameters

targets(Array)

Identity of targets to which request was published

Return

(OperationResult)

Corresponding result



201
202
203
# File 'lib/right_agent/operation_result.rb', line 201

def self.multicast(targets)
  OperationResult.new(MULTICAST, targets)
end

.non_delivery(reason) ⇒ Object

Create new non-delivery status

Parameters

reason(String)

Non-delivery reason from NON_DELIVERY_REASONS

Return

(OperationResult)

Corresponding result



189
190
191
# File 'lib/right_agent/operation_result.rb', line 189

def self.non_delivery(reason)
  OperationResult.new(NON_DELIVERY, reason)
end

.retry(content = nil) ⇒ Object

Create new retry status

Parameters

content(Object)

Any data associated with retry, defaults to nil

Return

(OperationResult)

Corresponding result



178
179
180
# File 'lib/right_agent/operation_result.rb', line 178

def self.retry(content = nil)
  OperationResult.new(RETRY, content)
end

.success(content = nil) ⇒ Object

Create new success status

Parameters

content(Object)

Any data associated with successful results, defaults to nil

Return

(OperationResult)

Corresponding result



142
143
144
# File 'lib/right_agent/operation_result.rb', line 142

def self.success(content = nil)
  OperationResult.new(SUCCESS, content)
end

Instance Method Details

#cancel?Boolean

Was last operation status CANCEL?

Return

true

If status is CANCEL

false

Otherwise

Returns:

  • (Boolean)


276
277
278
# File 'lib/right_agent/operation_result.rb', line 276

def cancel?
  status_code == CANCEL
end

#continue?Boolean

Was last operation status CONTINUE?

Return

true

If status is CONTINUE

false

Otherwise

Returns:

  • (Boolean)


239
240
241
# File 'lib/right_agent/operation_result.rb', line 239

def continue?
  status_code == CONTINUE
end

#error?Boolean

Was last operation unsuccessful?

Return

true

If status is ERROR or NON_DELIVERY

false

Otherwise

Returns:

  • (Boolean)


230
231
232
# File 'lib/right_agent/operation_result.rb', line 230

def error?
  status_code == ERROR || status_code == NON_DELIVERY
end

#multicast?Boolean

Was last operation status MULTICAST? Deprecated for agents at version 13 or above

Return

true

If status is MULTICAST

false

Otherwise

Returns:

  • (Boolean)


267
268
269
# File 'lib/right_agent/operation_result.rb', line 267

def multicast?
  status_code == MULTICAST
end

#non_delivery?Boolean

Was last operation status NON_DELIVERY?

Return

true

If status is NON_DELIVERY

false

Otherwise

Returns:

  • (Boolean)


257
258
259
# File 'lib/right_agent/operation_result.rb', line 257

def non_delivery?
  status_code == NON_DELIVERY
end

#retry?Boolean

Was last operation status RETRY?

Return

true

If status is RETRY

false

Otherwise

Returns:

  • (Boolean)


248
249
250
# File 'lib/right_agent/operation_result.rb', line 248

def retry?
  status_code == RETRY
end

#serialized_membersObject

Array of serialized fields given to constructor



281
282
283
# File 'lib/right_agent/operation_result.rb', line 281

def serialized_members
  [@status_code, @content]
end

#status(reason = false) ⇒ Object

User friendly result status

Parameters

reason(Boolean)

Whether to include failure reason information, default to false

Return

(String)

Name of result code



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/right_agent/operation_result.rb', line 80

def status(reason = false)
  case @status_code
  when SUCCESS      then 'success'
  when ERROR        then 'error' + (reason ? " (#{truncated_error})" : "")
  when CONTINUE     then 'continue'
  when RETRY        then 'retry' + (reason ? " (#{@content})" : "")
  when NON_DELIVERY then 'non-delivery' + (reason ? " (#{@content})" : "")
  when MULTICAST    then 'multicast'
  when CANCEL       then 'cancel' + (reason ? " (#{@content})" : "")
  end
end

#success?Boolean

Was last operation successful?

Return

true

If status is SUCCESS or CONTINUE

false

Otherwise

Returns:

  • (Boolean)


221
222
223
# File 'lib/right_agent/operation_result.rb', line 221

def success?
  status_code == SUCCESS || status_code == CONTINUE
end

#to_sObject

User friendly result Does not include content except in the case of error or non-delivery

Return

(String)

Name of result code



69
70
71
# File 'lib/right_agent/operation_result.rb', line 69

def to_s
  status(reason = true)
end

#truncated_errorObject

Limited length error string

Return

e(String)

String of no more than MAX_ERROR_SIZE characters



96
97
98
99
100
# File 'lib/right_agent/operation_result.rb', line 96

def truncated_error
  e = @content.is_a?(String) ? @content : @content.inspect
  e = e[0, MAX_ERROR_SIZE - 3] + "..." if e.size > (MAX_ERROR_SIZE - 3)
  e
end