Class: Google::Gax::Operation
- Inherits:
-
Object
- Object
- Google::Gax::Operation
- Defined in:
- lib/google/gax/operation.rb
Overview
A class used to wrap Google::Longrunning::Operation objects. This class provides helper methods to check the status of an Operation
Instance Attribute Summary collapse
-
#call_options ⇒ Google::Gax::CallOptions
The call options used when reloading the operation.
-
#grpc_op ⇒ Google::Longrunning::Operation
readonly
The wrapped grpc operation object.
Instance Method Summary collapse
-
#cancel ⇒ Object
Cancels the operation.
-
#delete ⇒ Object
Deletes the operation.
-
#done? ⇒ Boolean
Checks if the operation is done.
-
#error ⇒ Google::Rpc::Status?
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(grpc_op, client, result_type = nil, metadata_type = nil, call_options: nil) ⇒ Operation
constructor
A new instance of Operation.
-
#metadata ⇒ Object?
Returns the metadata of an operation.
-
#name ⇒ String
Returns the server-assigned name of the operation, which is only unique within the same service that originally returns it.
-
#on_done(&block) ⇒ Object
Registers a callback to be run when a refreshed operation is marked as done.
-
#reload! ⇒ Google::Gax::Operation
(also: #refresh!)
Reloads the operation object.
-
#response ⇒ Object?
If the operation is done, returns the response, otherwise returns nil.
-
#response? ⇒ Boolean
Checks if the operation is done and the result is a response.
-
#results ⇒ Object, ...
If the operation is done, returns the response.
-
#wait_until_done!(backoff_settings: nil) {|_self| ... } ⇒ Object
Blocking method to wait until the operation has completed or the maximum timeout has been reached.
Constructor Details
#initialize(grpc_op, client, result_type = nil, metadata_type = nil, call_options: nil) ⇒ Operation
Returns a new instance of Operation.
114 115 116 117 118 119 120 121 122 |
# File 'lib/google/gax/operation.rb', line 114 def initialize(grpc_op, client, result_type = nil, = nil, call_options: nil) @grpc_op = grpc_op @client = client @call_options = @result_type = result_type @metadata_type = @callbacks = [] end |
Instance Attribute Details
#call_options ⇒ Google::Gax::CallOptions
Returns The call options used when reloading the operation.
99 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/google/gax/operation.rb', line 99 class Operation attr_reader :grpc_op attr_accessor :call_options # @param grpc_op [Google::Longrunning::Operation] # The inital longrunning operation. # @param client [Google::Longrunning::OperationsClient] # The client that handles the grpc operations. # @param result_type [Class] The class type to be unpacked from the # result. If not provided the class type will be looked up. Optional. # @param metadata_type [Class] The class type to be unpacked from the # metadata. If not provided the class type will be looked up. Optional. # @param call_options [Google::Gax::CallOptions] # The call options that are used when reloading the operation. Optional. def initialize(grpc_op, client, result_type = nil, = nil, call_options: nil) @grpc_op = grpc_op @client = client @call_options = @result_type = result_type @metadata_type = @callbacks = [] end # If the operation is done, returns the response. # If the operation response is an error, the error will be returned. # Otherwise returns nil. # # @return [Object, Google::Rpc::Status, nil] # The result of the operation. If it is an error a Google::Rpc::Status # will be returned. def results return error if error? return response if response? end # Returns the server-assigned name of the operation, which is only unique # within the same service that originally returns it. If you use the # default HTTP mapping, the name should have the format of # operations/some/unique/name. # # @return [String] # The name of the operation. def name @grpc_op.name end # Returns the metadata of an operation. If a type is provided, # the metadata will be unpacked using the type provided; returning nil # if the metadata is not of the type provided. # If the type is not of provided, the metadata will be unpacked using # the metadata's type_url if the type_url is found in the # Google::Protobuf::DescriptorPool.generated_pool. # If the type cannot be found the raw metadata is retuned. # # @return [Object, nil] # The metadata of the operation. Can be nil. def return if @grpc_op..nil? return @grpc_op..unpack(@metadata_type) if @metadata_type descriptor = Google::Protobuf::DescriptorPool.generated_pool.lookup( @grpc_op..type_name ) return @grpc_op..unpack(descriptor.msgclass) if descriptor @grpc_op. end # 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. # # @return [Boolean] Whether the operation is done. def done? @grpc_op.done end # Checks if the operation is done and the result is a response. # If the operation is not finished then this will return false. # # @return [Boolean] Whether a response has been returned. def response? done? ? @grpc_op.result == :response : false end # If the operation is done, returns the response, otherwise returns nil. # # @return [Object, nil] # The response of the operation. def response return unless response? return @grpc_op.response.unpack(@result_type) if @result_type descriptor = Google::Protobuf::DescriptorPool.generated_pool.lookup( @grpc_op.response.type_name ) return @grpc_op.response.unpack(descriptor.msgclass) if descriptor @grpc_op.response end # Checks if the operation is done and the result is an error. # If the operation is not finished then this will return false. # # @return [Boolean] Whether an error has been returned. def error? done? ? @grpc_op.result == :error : false end # If the operation response is an error, the error will be returned, # otherwise returns nil. # # @return [Google::Rpc::Status, nil] # The error object. def error @grpc_op.error if error? end # Cancels the operation. def cancel @client.cancel_operation(@grpc_op.name) end # Deletes the operation. def delete @client.delete_operation(@grpc_op.name) end # Reloads the operation object. # # @return [Google::Gax::Operation] # Since this method changes internal state, it returns itself. def reload! @grpc_op = @client.get_operation(@grpc_op.name, options: @call_options) if done? @callbacks.each { |proc| proc.call(self) } @callbacks.clear end self end alias refresh! reload! # 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. # # @param backoff_settings [Google::Gax::BackoffSettings] # The backoff settings used to manipulate how this method retries # checking if the operation is done. # @yield operation [Google::Gax::Operation] Yields the finished Operation. def wait_until_done!(backoff_settings: nil) unless backoff_settings backoff_settings = BackoffSettings.new( 10 * MILLIS_PER_SECOND, 1.3, 5 * 60 * MILLIS_PER_SECOND, 0, 0, 0, 60 * 60 * MILLIS_PER_SECOND ) end delay = backoff_settings.initial_retry_delay_millis / MILLIS_PER_SECOND max_delay = backoff_settings.max_retry_delay_millis / MILLIS_PER_SECOND delay_multiplier = backoff_settings.retry_delay_multiplier total_timeout = backoff_settings.total_timeout_millis / MILLIS_PER_SECOND deadline = Time.now + total_timeout until done? sleep(delay) if Time.now >= deadline raise RetryError.new('Retry total timeout exceeded with exception') end delay = [delay * delay_multiplier, max_delay].min reload! end yield(self) if block_given? end # 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. # # @yield operation [Google::Gax::Operation] Yields the finished Operation. def on_done(&block) if done? yield(self) else @callbacks.push(block) end end end |
#grpc_op ⇒ Google::Longrunning::Operation (readonly)
Returns The wrapped grpc operation object.
99 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/google/gax/operation.rb', line 99 class Operation attr_reader :grpc_op attr_accessor :call_options # @param grpc_op [Google::Longrunning::Operation] # The inital longrunning operation. # @param client [Google::Longrunning::OperationsClient] # The client that handles the grpc operations. # @param result_type [Class] The class type to be unpacked from the # result. If not provided the class type will be looked up. Optional. # @param metadata_type [Class] The class type to be unpacked from the # metadata. If not provided the class type will be looked up. Optional. # @param call_options [Google::Gax::CallOptions] # The call options that are used when reloading the operation. Optional. def initialize(grpc_op, client, result_type = nil, = nil, call_options: nil) @grpc_op = grpc_op @client = client @call_options = @result_type = result_type @metadata_type = @callbacks = [] end # If the operation is done, returns the response. # If the operation response is an error, the error will be returned. # Otherwise returns nil. # # @return [Object, Google::Rpc::Status, nil] # The result of the operation. If it is an error a Google::Rpc::Status # will be returned. def results return error if error? return response if response? end # Returns the server-assigned name of the operation, which is only unique # within the same service that originally returns it. If you use the # default HTTP mapping, the name should have the format of # operations/some/unique/name. # # @return [String] # The name of the operation. def name @grpc_op.name end # Returns the metadata of an operation. If a type is provided, # the metadata will be unpacked using the type provided; returning nil # if the metadata is not of the type provided. # If the type is not of provided, the metadata will be unpacked using # the metadata's type_url if the type_url is found in the # Google::Protobuf::DescriptorPool.generated_pool. # If the type cannot be found the raw metadata is retuned. # # @return [Object, nil] # The metadata of the operation. Can be nil. def return if @grpc_op..nil? return @grpc_op..unpack(@metadata_type) if @metadata_type descriptor = Google::Protobuf::DescriptorPool.generated_pool.lookup( @grpc_op..type_name ) return @grpc_op..unpack(descriptor.msgclass) if descriptor @grpc_op. end # 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. # # @return [Boolean] Whether the operation is done. def done? @grpc_op.done end # Checks if the operation is done and the result is a response. # If the operation is not finished then this will return false. # # @return [Boolean] Whether a response has been returned. def response? done? ? @grpc_op.result == :response : false end # If the operation is done, returns the response, otherwise returns nil. # # @return [Object, nil] # The response of the operation. def response return unless response? return @grpc_op.response.unpack(@result_type) if @result_type descriptor = Google::Protobuf::DescriptorPool.generated_pool.lookup( @grpc_op.response.type_name ) return @grpc_op.response.unpack(descriptor.msgclass) if descriptor @grpc_op.response end # Checks if the operation is done and the result is an error. # If the operation is not finished then this will return false. # # @return [Boolean] Whether an error has been returned. def error? done? ? @grpc_op.result == :error : false end # If the operation response is an error, the error will be returned, # otherwise returns nil. # # @return [Google::Rpc::Status, nil] # The error object. def error @grpc_op.error if error? end # Cancels the operation. def cancel @client.cancel_operation(@grpc_op.name) end # Deletes the operation. def delete @client.delete_operation(@grpc_op.name) end # Reloads the operation object. # # @return [Google::Gax::Operation] # Since this method changes internal state, it returns itself. def reload! @grpc_op = @client.get_operation(@grpc_op.name, options: @call_options) if done? @callbacks.each { |proc| proc.call(self) } @callbacks.clear end self end alias refresh! reload! # 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. # # @param backoff_settings [Google::Gax::BackoffSettings] # The backoff settings used to manipulate how this method retries # checking if the operation is done. # @yield operation [Google::Gax::Operation] Yields the finished Operation. def wait_until_done!(backoff_settings: nil) unless backoff_settings backoff_settings = BackoffSettings.new( 10 * MILLIS_PER_SECOND, 1.3, 5 * 60 * MILLIS_PER_SECOND, 0, 0, 0, 60 * 60 * MILLIS_PER_SECOND ) end delay = backoff_settings.initial_retry_delay_millis / MILLIS_PER_SECOND max_delay = backoff_settings.max_retry_delay_millis / MILLIS_PER_SECOND delay_multiplier = backoff_settings.retry_delay_multiplier total_timeout = backoff_settings.total_timeout_millis / MILLIS_PER_SECOND deadline = Time.now + total_timeout until done? sleep(delay) if Time.now >= deadline raise RetryError.new('Retry total timeout exceeded with exception') end delay = [delay * delay_multiplier, max_delay].min reload! end yield(self) if block_given? end # 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. # # @yield operation [Google::Gax::Operation] Yields the finished Operation. def on_done(&block) if done? yield(self) else @callbacks.push(block) end end end |
Instance Method Details
#cancel ⇒ Object
Cancels the operation.
221 222 223 |
# File 'lib/google/gax/operation.rb', line 221 def cancel @client.cancel_operation(@grpc_op.name) end |
#delete ⇒ Object
Deletes the operation.
226 227 228 |
# File 'lib/google/gax/operation.rb', line 226 def delete @client.delete_operation(@grpc_op.name) end |
#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.
174 175 176 |
# File 'lib/google/gax/operation.rb', line 174 def done? @grpc_op.done end |
#error ⇒ Google::Rpc::Status?
If the operation response is an error, the error will be returned, otherwise returns nil.
216 217 218 |
# File 'lib/google/gax/operation.rb', line 216 def error @grpc_op.error if error? 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.
207 208 209 |
# File 'lib/google/gax/operation.rb', line 207 def error? done? ? @grpc_op.result == :error : false end |
#metadata ⇒ Object?
Returns the metadata of an operation. If a type is provided, the metadata will be unpacked using the type provided; returning nil if the metadata is not of the type provided. If the type is not of provided, the metadata will be unpacked using the metadata’s type_url if the type_url is found in the Google::Protobuf::DescriptorPool.generated_pool. If the type cannot be found the raw metadata is retuned.
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/google/gax/operation.rb', line 157 def return if @grpc_op..nil? return @grpc_op..unpack(@metadata_type) if @metadata_type descriptor = Google::Protobuf::DescriptorPool.generated_pool.lookup( @grpc_op..type_name ) return @grpc_op..unpack(descriptor.msgclass) if descriptor @grpc_op. end |
#name ⇒ String
Returns the server-assigned name of the operation, which is only unique within the same service that originally returns it. If you use the default HTTP mapping, the name should have the format of operations/some/unique/name.
143 144 145 |
# File 'lib/google/gax/operation.rb', line 143 def name @grpc_op.name 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.
287 288 289 290 291 292 293 |
# File 'lib/google/gax/operation.rb', line 287 def on_done(&block) if done? yield(self) else @callbacks.push(block) end end |
#reload! ⇒ Google::Gax::Operation Also known as: refresh!
Reloads the operation object.
234 235 236 237 238 239 240 241 |
# File 'lib/google/gax/operation.rb', line 234 def reload! @grpc_op = @client.get_operation(@grpc_op.name, options: @call_options) if done? @callbacks.each { |proc| proc.call(self) } @callbacks.clear end self end |
#response ⇒ Object?
If the operation is done, returns the response, otherwise returns nil.
190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/google/gax/operation.rb', line 190 def response return unless response? return @grpc_op.response.unpack(@result_type) if @result_type descriptor = Google::Protobuf::DescriptorPool.generated_pool.lookup( @grpc_op.response.type_name ) return @grpc_op.response.unpack(descriptor.msgclass) if descriptor @grpc_op.response end |
#response? ⇒ Boolean
Checks if the operation is done and the result is a response. If the operation is not finished then this will return false.
182 183 184 |
# File 'lib/google/gax/operation.rb', line 182 def response? done? ? @grpc_op.result == :response : false 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.
131 132 133 134 |
# File 'lib/google/gax/operation.rb', line 131 def results return error if error? return response if response? end |
#wait_until_done!(backoff_settings: 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.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/google/gax/operation.rb', line 252 def wait_until_done!(backoff_settings: nil) unless backoff_settings backoff_settings = BackoffSettings.new( 10 * MILLIS_PER_SECOND, 1.3, 5 * 60 * MILLIS_PER_SECOND, 0, 0, 0, 60 * 60 * MILLIS_PER_SECOND ) end delay = backoff_settings.initial_retry_delay_millis / MILLIS_PER_SECOND max_delay = backoff_settings.max_retry_delay_millis / MILLIS_PER_SECOND delay_multiplier = backoff_settings.retry_delay_multiplier total_timeout = backoff_settings.total_timeout_millis / MILLIS_PER_SECOND deadline = Time.now + total_timeout until done? sleep(delay) if Time.now >= deadline raise RetryError.new('Retry total timeout exceeded with exception') end delay = [delay * delay_multiplier, max_delay].min reload! end yield(self) if block_given? end |