forked from googleapis/gax-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.rb
295 lines (263 loc) · 10.6 KB
/
operation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
283
284
285
286
287
288
289
290
291
292
293
294
295
# Copyright 2016, Google LLC
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google LLC nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require "google/gax/operation/retry_policy"
require "google/protobuf/well_known_types"
module Google
module Gax
# A class used to wrap Google::Longrunning::Operation objects. This class provides helper methods to check the
# status of an Operation
#
# @example Checking Operation status
# # this example assumes both api_client and operations_client
# # already exist.
# require "google/gax/operation"
#
# op = Google::Gax::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
#
# @example Working with callbacks
# # this example assumes both api_client and operations_client
# # already exist.
# require "google/gax/operation"
#
# op = Google::Gax::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
#
# @attribute [r] grpc_op
# @return [Google::Longrunning::Operation] The wrapped grpc
# operation object.
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.
#
def initialize grpc_op, client, result_type: nil, metadata_type: nil
@grpc_op = grpc_op
@client = client
@result_type = result_type
@metadata_type = metadata_type
@on_done_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 metadata
return if @grpc_op.metadata.nil?
return @grpc_op.metadata.unpack @metadata_type if @metadata_type
descriptor = Google::Protobuf::DescriptorPool.generated_pool.lookup @grpc_op.metadata.type_name
return @grpc_op.metadata.unpack descriptor.msgclass if descriptor
@grpc_op.metadata
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 [ApiCall::Options, Hash] The options for making the API call. A Hash can be provided to customize
# the options object, using keys that match the arguments for {ApiCall::Options.new}.
#
def cancel options: nil
# Converts hash and nil to an options object
options = ApiCall::Options.new options.to_h if options.respond_to? :to_h
@client.cancel_operation({ name: @grpc_op.name }, options)
end
##
# Deletes the operation.
#
# @param options [ApiCall::Options, Hash] The options for making the API call. A Hash can be provided to customize
# the options object, using keys that match the arguments for {ApiCall::Options.new}.
#
def delete options: nil
# Converts hash and nil to an options object
options = ApiCall::Options.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 [ApiCall::Options, Hash] The options for making the API call. A Hash can be provided to customize
# the options object, using keys that match the arguments for {ApiCall::Options.new}.
#
# @return [Google::Gax::Operation] Since this method changes internal state, it returns itself.
#
def reload! options: nil
# Converts hash and nil to an options object
options = ApiCall::Options.new options.to_h if options.respond_to? :to_h
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.
#
# @yield operation [Google::Gax::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.
#
# @yield operation [Google::Gax::Operation] Yields the finished Operation.
#
def on_done &block
if done?
yield self
else
@on_done_callbacks.push block
end
end
end
end
end