Class: Gapic::GenericLRO::Operation
- Inherits:
-
BaseOperation
- Object
- BaseOperation
- Gapic::GenericLRO::Operation
- Defined in:
- lib/gapic/generic_lro/operation.rb
Overview
A class used to wrap the longrunning operation objects, including the nonstandard ones
(nonstandard
meaning not conforming to the AIP-151).
It provides helper methods to poll and check for status of these operations.
Defined Under Namespace
Classes: GenericError
Instance Attribute Summary
Attributes inherited from BaseOperation
Instance Method Summary collapse
-
#done? ⇒ Boolean
Checks if the operation is done.
-
#error ⇒ Object?
If the operation response is an error, the error will be returned, otherwise returns nil.
-
#error? ⇒ Boolean
Checks if the operation is done and the result is an error.
-
#initialize(operation, client:, polling_method_name:, operation_status_field:, request_values: {}, operation_name_field: nil, operation_err_field: nil, operation_err_code_field: nil, operation_err_msg_field: nil, operation_copy_fields: {}, options: {}) ⇒ Operation
constructor
A new instance of Operation.
-
#name ⇒ String?
Returns the name of the operation, if specified.
-
#on_done(&block) ⇒ Object
Registers a callback to be run when a refreshed operation is marked as done.
-
#on_reload(&block) ⇒ Object
Registers a callback to be run when an operation is being reloaded.
-
#reload!(options: nil) ⇒ Gapic::GenericLRO::Operation
(also: #refresh!)
Reloads the operation object.
-
#response ⇒ Object?
If the operation is completed successfully, returns the underlying operation object, otherwise returns nil.
-
#response? ⇒ Boolean
Checks if the operation is done and the result is not an error.
-
#results ⇒ Object?
If the operation is done, returns the response.
-
#wait_until_done!(retry_policy: nil) {|_self| ... } ⇒ Object
Blocking method to wait until the operation has completed or the maximum timeout has been reached.
Constructor Details
#initialize(operation, client:, polling_method_name:, operation_status_field:, request_values: {}, operation_name_field: nil, operation_err_field: nil, operation_err_code_field: nil, operation_err_msg_field: nil, operation_copy_fields: {}, options: {}) ⇒ Operation
Returns a new instance of Operation.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gapic/generic_lro/operation.rb', line 61 def initialize operation, client:, polling_method_name:, operation_status_field:, request_values: {}, operation_name_field: nil, operation_err_field: nil, operation_err_code_field: nil, operation_err_msg_field: nil, operation_copy_fields: {}, options: {} @client = client @polling_method_name = polling_method_name @operation_status_field = operation_status_field @request_values = request_values || {} @operation_name_field = operation_name_field @operation_err_field = operation_err_field @operation_err_code_field = operation_err_code_field @operation_err_msg_field = operation_err_msg_field @operation_copy_fields = operation_copy_fields || {} @on_done_callbacks = [] @on_reload_callbacks = [] @options = || {} super operation end |
Instance Method Details
#done? ⇒ Boolean
Checks if the operation is done. This does not send a new api call, but checks the result of the previous api call to see if done.
112 113 114 115 116 |
# File 'lib/gapic/generic_lro/operation.rb', line 112 def done? return status if [true, false].include? status status == :DONE end |
#error ⇒ Object?
If the operation response is an error, the error will be returned, otherwise returns nil.
151 152 153 154 |
# File 'lib/gapic/generic_lro/operation.rb', line 151 def error return unless error? err || GenericError.new(error_code, error_msg) end |
#error? ⇒ Boolean
Checks if the operation is done and the result is an error. If the operation is not finished then this will return false.
142 143 144 |
# File 'lib/gapic/generic_lro/operation.rb', line 142 def error? done? && (!err.nil? || !error_code.nil?) end |
#name ⇒ String?
Returns the name of the operation, if specified.
101 102 103 104 |
# File 'lib/gapic/generic_lro/operation.rb', line 101 def name return nil if @operation_name_field.nil? operation.send @operation_name_field if operation.respond_to? @operation_name_field end |
#on_done(&block) ⇒ Object
Registers a callback to be run when a refreshed operation is marked as done. If the operation has completed prior to a call to this function the callback will be called instead of registered.
231 232 233 234 235 236 237 |
# File 'lib/gapic/generic_lro/operation.rb', line 231 def on_done &block if done? yield self else @on_done_callbacks.push block end end |
#on_reload(&block) ⇒ Object
Registers a callback to be run when an operation is being reloaded. If the operation has completed prior to a call to this function the callback will NOT be called or registered.
220 221 222 223 |
# File 'lib/gapic/generic_lro/operation.rb', line 220 def on_reload &block return if done? @on_reload_callbacks.push block end |
#reload!(options: nil) ⇒ Gapic::GenericLRO::Operation Also known as: refresh!
Reloads the operation object.
to customize the options object, using keys that match the arguments for CallOptions.new.
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 |
# File 'lib/gapic/generic_lro/operation.rb', line 164 def reload! options: nil return self if done? @on_reload_callbacks.each { |proc| proc.call self } request_hash = @request_values.transform_keys(&:to_sym) @operation_copy_fields.each do |field_from, field_to| request_hash[field_to.to_sym] = operation.send field_from.to_s if operation.respond_to? field_from.to_s end = , @options ops = @client.send @polling_method_name, request_hash, ops = ops.operation if ops.is_a? Gapic::GenericLRO::BaseOperation self.operation = ops if done? @on_reload_callbacks.clear @on_done_callbacks.each { |proc| proc.call self } @on_done_callbacks.clear end self end |
#response ⇒ Object?
If the operation is completed successfully, returns the underlying operation object, otherwise returns nil.
132 133 134 |
# File 'lib/gapic/generic_lro/operation.rb', line 132 def response operation if response? end |
#response? ⇒ Boolean
Checks if the operation is done and the result is not an error. If the operation is not finished then this will return false.
124 125 126 |
# File 'lib/gapic/generic_lro/operation.rb', line 124 def response? done? && !error? end |
#results ⇒ Object?
If the operation is done, returns the response. If the operation response is an error, the error will be returned. Otherwise returns nil.
91 92 93 94 |
# File 'lib/gapic/generic_lro/operation.rb', line 91 def results return error if error? return response if response? end |
#wait_until_done!(retry_policy: nil) {|_self| ... } ⇒ Object
Blocking method to wait until the operation has completed or the maximum timeout has been reached. Upon completion, registered callbacks will be called, then - if a block is given - the block will be called.
200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/gapic/generic_lro/operation.rb', line 200 def wait_until_done! retry_policy: nil retry_policy = ::Gapic::Operation::RetryPolicy.new retry_policy if retry_policy.is_a? Hash retry_policy ||= ::Gapic::Operation::RetryPolicy.new until done? reload! break unless retry_policy.call end yield self if block_given? self end |