Class: Gapic::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/gapic/operation.rb,
lib/gapic/operation/retry_policy.rb

Overview

A class used to wrap Google::Longrunning::Operation objects. This class provides helper methods to check the status of an Operation

Examples:

Checking Operation status

# this example assumes both api_client and operations_client
# already exist.
require "gapic/operation"

op = Gapic::Operation.new(
  api_client.method_that_returns_longrunning_operation(),
  operations_client,
  Google::Example::ResultType,
  Google::Example::MetadataType
)

op.done? # => false
op.reload! # => operation completed

if op.done?
  results = op.results
  handle_error(results) if op.error?
  # Handle results.
end

Working with callbacks

# this example assumes both api_client and operations_client
# already exist.
require "gapic/operation"

op = Gapic::Operation.new(
  api_client.method_that_returns_longrunning_operation(),
  operations_client,
  Google::Example::ResultType,
  Google::Example::MetadataType
)

# Register a callback to be run when an operation is done.
op.on_done do |operation|
  raise operation.results.message if operation.error?
  # process(operation.results)
  # process(operation.metadata)
end

# Reload the operation running callbacks if operation completed.
op.reload!

# Or block until the operation completes, passing a block to be called
# on completion.
op.wait_until_done! do |operation|
  raise operation.results.message if operation.error?
  # process(operation.results)
  # process(operation.rmetadata)
end

Defined Under Namespace

Classes: RetryPolicy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grpc_op, client, result_type: nil, metadata_type: nil, options: {}) ⇒ Operation

Returns a new instance of Operation.

Parameters:

  • grpc_op (Google::Longrunning::Operation)

    The inital longrunning operation.

  • client (Google::Longrunning::OperationsClient)

    The client that handles the grpc operations.

  • result_type (Class) (defaults to: nil)

    The class type to be unpacked from the result. If not provided the class type will be looked up. Optional.

  • metadata_type (Class) (defaults to: nil)

    The class type to be unpacked from the metadata. If not provided the class type will be looked up. Optional.

  • options (Gapic::CallOptions) (defaults to: {})

    call options for this operation



88
89
90
91
92
93
94
95
# File 'lib/gapic/operation.rb', line 88

def initialize grpc_op, client, result_type: nil, metadata_type: nil, options: {}
  @grpc_op = grpc_op
  @client = client
  @result_type = result_type
  @metadata_type = 
  @on_done_callbacks = []
  @options = options
end

Instance Attribute Details

#grpc_opGoogle::Longrunning::Operation (readonly)

Returns The wrapped grpc operation object.

Returns:

  • (Google::Longrunning::Operation)

    The wrapped grpc operation object.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
# File 'lib/gapic/operation.rb', line 76

class Operation
  attr_reader :grpc_op

  ##
  # @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 options [Gapic::CallOptions] call options for this operation
  #
  def initialize grpc_op, client, result_type: nil, metadata_type: nil, options: {}
    @grpc_op = grpc_op
    @client = client
    @result_type = result_type
    @metadata_type = 
    @on_done_callbacks = []
    @options = options
  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?
    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.
  #
  # @param options [Gapic::CallOptions, Hash] The options for making the RPC call. A Hash can be provided to customize
  #   the options object, using keys that match the arguments for {Gapic::CallOptions.new}.
  #
  def cancel options: nil
    # Converts hash and nil to an options object
    options = Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h

    @client.cancel_operation({ name: @grpc_op.name }, options)
  end

  ##
  # Deletes the operation.
  #
  # @param options [Gapic::CallOptions, Hash] The options for making the RPC call. A Hash can be provided to customize
  #   the options object, using keys that match the arguments for {Gapic::CallOptions.new}.
  #
  def delete options: nil
    # Converts hash and nil to an options object
    options = Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h

    @client.delete_operation({ name: @grpc_op.name }, options)
  end

  ##
  # Reloads the operation object.
  #
  # @param options [Gapic::CallOptions, Hash] The options for making the RPC call. A Hash can be provided to customize
  #   the options object, using keys that match the arguments for {Gapic::CallOptions.new}.
  #
  # @return [Gapic::Operation] Since this method changes internal state, it returns itself.
  #
  def reload! options: nil
    options = if options.respond_to? :to_h
                options.to_h.merge @options.to_h
              else
                @options.to_h
              end
    options = Gapic::CallOptions.new(**options)
    gax_op = @client.get_operation({ name: @grpc_op.name }, options)
    @grpc_op = gax_op.grpc_op

    if done?
      @on_done_callbacks.each { |proc| proc.call self }
      @on_done_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 retry_policy [RetryPolicy, Hash, Proc] The policy for retry. A custom proc that takes the error as an
  #   argument and blocks can also be provided.
  #
  # @yieldparam operation [Gapic::Operation] Yields the finished Operation.
  #
  def wait_until_done! retry_policy: nil
    retry_policy = RetryPolicy.new(**retry_policy) if retry_policy.is_a? Hash
    retry_policy ||= RetryPolicy.new

    until done?
      reload!
      break unless retry_policy.call
    end

    yield self if block_given?

    self
  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.
  #
  # @yieldparam operation [Gapic::Operation] Yields the finished Operation.
  #
  def on_done &block
    if done?
      yield self
    else
      @on_done_callbacks.push block
    end
  end
end

Instance Method Details

#cancel(options: nil) ⇒ Object

Cancels the operation.

Parameters:

  • options (Gapic::CallOptions, Hash) (defaults to: nil)

    The options for making the RPC call. A Hash can be provided to customize the options object, using keys that match the arguments for CallOptions.new.



199
200
201
202
203
204
# File 'lib/gapic/operation.rb', line 199

def cancel options: nil
  # Converts hash and nil to an options object
  options = Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h

  @client.cancel_operation({ name: @grpc_op.name }, options)
end

#delete(options: nil) ⇒ Object

Deletes the operation.

Parameters:

  • options (Gapic::CallOptions, Hash) (defaults to: nil)

    The options for making the RPC call. A Hash can be provided to customize the options object, using keys that match the arguments for CallOptions.new.



212
213
214
215
216
217
# File 'lib/gapic/operation.rb', line 212

def delete options: nil
  # Converts hash and nil to an options object
  options = Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h

  @client.delete_operation({ name: @grpc_op.name }, options)
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.

Returns:

  • (Boolean)

    Whether the operation is done.



144
145
146
# File 'lib/gapic/operation.rb', line 144

def done?
  @grpc_op.done
end

#errorGoogle::Rpc::Status?

If the operation response is an error, the error will be returned, otherwise returns nil.

Returns:

  • (Google::Rpc::Status, nil)

    The error object.



189
190
191
# File 'lib/gapic/operation.rb', line 189

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.

Returns:

  • (Boolean)

    Whether an error has been returned.



180
181
182
# File 'lib/gapic/operation.rb', line 180

def error?
  done? ? @grpc_op.result == :error : false
end

#metadataObject?

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.

Returns:

  • (Object, nil)

    The metadata of the operation. Can be nil.



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gapic/operation.rb', line 126

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

#nameString

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.

Returns:

  • (String)

    The name of the operation.



114
115
116
# File 'lib/gapic/operation.rb', line 114

def name
  @grpc_op.name
end

#on_done {|operation| ... } ⇒ 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.

Yield Parameters:



275
276
277
278
279
280
281
# File 'lib/gapic/operation.rb', line 275

def on_done &block
  if done?
    yield self
  else
    @on_done_callbacks.push block
  end
end

#reload!(options: nil) ⇒ Gapic::Operation Also known as: refresh!

Reloads the operation object.

Parameters:

  • options (Gapic::CallOptions, Hash) (defaults to: nil)

    The options for making the RPC call. A Hash can be provided to customize the options object, using keys that match the arguments for CallOptions.new.

Returns:

  • (Gapic::Operation)

    Since this method changes internal state, it returns itself.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/gapic/operation.rb', line 227

def reload! options: nil
  options = if options.respond_to? :to_h
              options.to_h.merge @options.to_h
            else
              @options.to_h
            end
  options = Gapic::CallOptions.new(**options)
  gax_op = @client.get_operation({ name: @grpc_op.name }, options)
  @grpc_op = gax_op.grpc_op

  if done?
    @on_done_callbacks.each { |proc| proc.call self }
    @on_done_callbacks.clear
  end

  self
end

#responseObject?

If the operation is done, returns the response, otherwise returns nil.

Returns:

  • (Object, nil)

    The response of the operation.



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/gapic/operation.rb', line 162

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.

Returns:

  • (Boolean)

    Whether a response has been returned.



154
155
156
# File 'lib/gapic/operation.rb', line 154

def response?
  done? ? @grpc_op.result == :response : false
end

#resultsObject, ...

If the operation is done, returns the response. If the operation response is an error, the error will be returned. Otherwise returns nil.

Returns:

  • (Object, Google::Rpc::Status, nil)

    The result of the operation. If it is an error a Google::Rpc::Status will be returned.



103
104
105
106
# File 'lib/gapic/operation.rb', line 103

def results
  return error if error?
  response if response?
end

#wait_until_done!(retry_policy: nil) {|operation| ... } ⇒ 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.

Parameters:

  • retry_policy (RetryPolicy, Hash, Proc) (defaults to: nil)

    The policy for retry. A custom proc that takes the error as an argument and blocks can also be provided.

Yield Parameters:



255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/gapic/operation.rb', line 255

def wait_until_done! retry_policy: nil
  retry_policy = RetryPolicy.new(**retry_policy) if retry_policy.is_a? Hash
  retry_policy ||= RetryPolicy.new

  until done?
    reload!
    break unless retry_policy.call
  end

  yield self if block_given?

  self
end