google-cloud-pubsub 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +18 -0
  3. data/AUTHENTICATION.md +177 -0
  4. data/CHANGELOG.md +538 -0
  5. data/CODE_OF_CONDUCT.md +40 -0
  6. data/CONTRIBUTING.md +188 -0
  7. data/EMULATOR.md +37 -0
  8. data/LICENSE +201 -0
  9. data/LOGGING.md +32 -0
  10. data/OVERVIEW.md +557 -0
  11. data/TROUBLESHOOTING.md +31 -0
  12. data/lib/google-cloud-pubsub.rb +139 -0
  13. data/lib/google/cloud/pubsub.rb +173 -0
  14. data/lib/google/cloud/pubsub/async_publisher.rb +399 -0
  15. data/lib/google/cloud/pubsub/async_publisher/batch.rb +309 -0
  16. data/lib/google/cloud/pubsub/batch_publisher.rb +99 -0
  17. data/lib/google/cloud/pubsub/convert.rb +91 -0
  18. data/lib/google/cloud/pubsub/credentials.rb +47 -0
  19. data/lib/google/cloud/pubsub/errors.rb +85 -0
  20. data/lib/google/cloud/pubsub/message.rb +158 -0
  21. data/lib/google/cloud/pubsub/policy.rb +187 -0
  22. data/lib/google/cloud/pubsub/project.rb +393 -0
  23. data/lib/google/cloud/pubsub/publish_result.rb +103 -0
  24. data/lib/google/cloud/pubsub/received_message.rb +297 -0
  25. data/lib/google/cloud/pubsub/retry_policy.rb +90 -0
  26. data/lib/google/cloud/pubsub/service.rb +514 -0
  27. data/lib/google/cloud/pubsub/snapshot.rb +202 -0
  28. data/lib/google/cloud/pubsub/snapshot/list.rb +178 -0
  29. data/lib/google/cloud/pubsub/subscriber.rb +399 -0
  30. data/lib/google/cloud/pubsub/subscriber/enumerator_queue.rb +54 -0
  31. data/lib/google/cloud/pubsub/subscriber/inventory.rb +166 -0
  32. data/lib/google/cloud/pubsub/subscriber/sequencer.rb +115 -0
  33. data/lib/google/cloud/pubsub/subscriber/stream.rb +401 -0
  34. data/lib/google/cloud/pubsub/subscriber/timed_unary_buffer.rb +231 -0
  35. data/lib/google/cloud/pubsub/subscription.rb +1279 -0
  36. data/lib/google/cloud/pubsub/subscription/list.rb +205 -0
  37. data/lib/google/cloud/pubsub/subscription/push_config.rb +244 -0
  38. data/lib/google/cloud/pubsub/topic.rb +934 -0
  39. data/lib/google/cloud/pubsub/topic/list.rb +171 -0
  40. data/lib/google/cloud/pubsub/v1.rb +17 -0
  41. data/lib/google/cloud/pubsub/v1/credentials.rb +41 -0
  42. data/lib/google/cloud/pubsub/v1/doc/google/iam/v1/iam_policy.rb +21 -0
  43. data/lib/google/cloud/pubsub/v1/doc/google/iam/v1/options.rb +21 -0
  44. data/lib/google/cloud/pubsub/v1/doc/google/iam/v1/policy.rb +21 -0
  45. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/duration.rb +91 -0
  46. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/empty.rb +29 -0
  47. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/field_mask.rb +222 -0
  48. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/timestamp.rb +113 -0
  49. data/lib/google/cloud/pubsub/v1/doc/google/pubsub/v1/pubsub.rb +833 -0
  50. data/lib/google/cloud/pubsub/v1/doc/google/type/expr.rb +19 -0
  51. data/lib/google/cloud/pubsub/v1/publisher_client.rb +928 -0
  52. data/lib/google/cloud/pubsub/v1/publisher_client_config.json +120 -0
  53. data/lib/google/cloud/pubsub/v1/subscriber_client.rb +1466 -0
  54. data/lib/google/cloud/pubsub/v1/subscriber_client_config.json +153 -0
  55. data/lib/google/cloud/pubsub/version.rb +24 -0
  56. data/lib/google/pubsub/v1/pubsub_pb.rb +269 -0
  57. data/lib/google/pubsub/v1/pubsub_services_pb.rb +215 -0
  58. metadata +337 -0
@@ -0,0 +1,393 @@
1
+ # Copyright 2015 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "google/cloud/errors"
17
+ require "google/cloud/pubsub/service"
18
+ require "google/cloud/pubsub/credentials"
19
+ require "google/cloud/pubsub/topic"
20
+ require "google/cloud/pubsub/batch_publisher"
21
+ require "google/cloud/pubsub/snapshot"
22
+
23
+ module Google
24
+ module Cloud
25
+ module PubSub
26
+ ##
27
+ # # Project
28
+ #
29
+ # Represents the project that pubsub messages are pushed to and pulled
30
+ # from. {Topic} is a named resource to which messages are sent by
31
+ # publishers. {Subscription} is a named resource representing the stream
32
+ # of messages from a single, specific topic, to be delivered to the
33
+ # subscribing application. {Message} is a combination of data and
34
+ # attributes that a publisher sends to a topic and is eventually delivered
35
+ # to subscribers.
36
+ #
37
+ # See {Google::Cloud#pubsub}
38
+ #
39
+ # @example
40
+ # require "google/cloud/pubsub"
41
+ #
42
+ # pubsub = Google::Cloud::PubSub.new
43
+ #
44
+ # topic = pubsub.topic "my-topic"
45
+ # topic.publish "task completed"
46
+ #
47
+ class Project
48
+ ##
49
+ # @private The Service object.
50
+ attr_accessor :service
51
+
52
+ ##
53
+ # @private Creates a new Pub/Sub Project instance.
54
+ def initialize service
55
+ @service = service
56
+ end
57
+
58
+ # The Pub/Sub project connected to.
59
+ #
60
+ # @example
61
+ # require "google/cloud/pubsub"
62
+ #
63
+ # pubsub = Google::Cloud::PubSub.new(
64
+ # project_id: "my-project",
65
+ # credentials: "/path/to/keyfile.json"
66
+ # )
67
+ #
68
+ # pubsub.project_id #=> "my-project"
69
+ #
70
+ def project_id
71
+ service.project
72
+ end
73
+ alias project project_id
74
+
75
+ ##
76
+ # Retrieves topic by name.
77
+ #
78
+ # @param [String] topic_name Name of a topic.
79
+ # @param [String] project If the topic belongs to a project other than
80
+ # the one currently connected to, the alternate project ID can be
81
+ # specified here. Optional.
82
+ # @param [Boolean] skip_lookup Optionally create a {Topic} object
83
+ # without verifying the topic resource exists on the Pub/Sub service.
84
+ # Calls made on this object will raise errors if the topic resource
85
+ # does not exist. Default is `false`. Optional.
86
+ # @param [Hash] async A hash of values to configure the topic's
87
+ # {AsyncPublisher} that is created when {Topic#publish_async}
88
+ # is called. Optional.
89
+ #
90
+ # Hash keys and values may include the following:
91
+ #
92
+ # * `:max_bytes` (Integer) The maximum size of messages to be collected before the batch is published. Default
93
+ # is 1,000,000 (1MB).
94
+ # * `:max_messages` (Integer) The maximum number of messages to be collected before the batch is published.
95
+ # Default is 100.
96
+ # * `:interval` (Numeric) The number of seconds to collect messages before the batch is published. Default is
97
+ # 0.01.
98
+ # * `:threads` (Hash) The number of threads to create to handle concurrent calls by the publisher:
99
+ # * `:publish` (Integer) The number of threads used to publish messages. Default is 2.
100
+ # * `:callback` (Integer) The number of threads to handle the published messages' callbacks. Default is 4.
101
+ #
102
+ # @return [Google::Cloud::PubSub::Topic, nil] Returns `nil` if topic
103
+ # does not exist.
104
+ #
105
+ # @example
106
+ # require "google/cloud/pubsub"
107
+ #
108
+ # pubsub = Google::Cloud::PubSub.new
109
+ # topic = pubsub.topic "existing-topic"
110
+ #
111
+ # @example By default `nil` will be returned if topic does not exist.
112
+ # require "google/cloud/pubsub"
113
+ #
114
+ # pubsub = Google::Cloud::PubSub.new
115
+ # topic = pubsub.topic "non-existing-topic" # nil
116
+ #
117
+ # @example Create topic in a different project with the `project` flag.
118
+ # require "google/cloud/pubsub"
119
+ #
120
+ # pubsub = Google::Cloud::PubSub.new
121
+ # topic = pubsub.topic "another-topic", project: "another-project"
122
+ #
123
+ # @example Skip the lookup against the service with `skip_lookup`:
124
+ # require "google/cloud/pubsub"
125
+ #
126
+ # pubsub = Google::Cloud::PubSub.new
127
+ # topic = pubsub.topic "another-topic", skip_lookup: true
128
+ #
129
+ # @example Configuring AsyncPublisher to increase concurrent callbacks:
130
+ # require "google/cloud/pubsub"
131
+ #
132
+ # pubsub = Google::Cloud::PubSub.new
133
+ # topic = pubsub.topic "my-topic",
134
+ # async: { threads: { callback: 16 } }
135
+ #
136
+ # topic.publish_async "task completed" do |result|
137
+ # if result.succeeded?
138
+ # log_publish_success result.data
139
+ # else
140
+ # log_publish_failure result.data, result.error
141
+ # end
142
+ # end
143
+ #
144
+ # topic.async_publisher.stop.wait!
145
+ #
146
+ def topic topic_name, project: nil, skip_lookup: nil, async: nil
147
+ ensure_service!
148
+ options = { project: project }
149
+ return Topic.from_name topic_name, service, options if skip_lookup
150
+ grpc = service.get_topic topic_name
151
+ Topic.from_grpc grpc, service, async: async
152
+ rescue Google::Cloud::NotFoundError
153
+ nil
154
+ end
155
+ alias get_topic topic
156
+ alias find_topic topic
157
+
158
+ ##
159
+ # Creates a new topic.
160
+ #
161
+ # @param [String] topic_name Name of a topic.
162
+ # @param [Hash] labels A hash of user-provided labels associated with
163
+ # the topic. You can use these to organize and group your topics.
164
+ # Label keys and values can be no longer than 63 characters, can only
165
+ # contain lowercase letters, numeric characters, underscores and
166
+ # dashes. International characters are allowed. Label values are
167
+ # optional. Label keys must start with a letter and each label in the
168
+ # list must have a different key. See [Creating and Managing
169
+ # Labels](https://cloud.google.com/pubsub/docs/labels).
170
+ # @param [String] kms_key The Cloud KMS encryption key that will be used
171
+ # to protect access to messages published on this topic. Optional.
172
+ # For example: `projects/a/locations/b/keyRings/c/cryptoKeys/d`
173
+ # @param [Array<String>] persistence_regions The list of GCP region IDs
174
+ # where messages that are published to the topic may be persisted in
175
+ # storage. Optional.
176
+ # @param [Hash] async A hash of values to configure the topic's
177
+ # {AsyncPublisher} that is created when {Topic#publish_async}
178
+ # is called. Optional.
179
+ #
180
+ # Hash keys and values may include the following:
181
+ #
182
+ # * `:max_bytes` (Integer) The maximum size of messages to be collected before the batch is published. Default
183
+ # is 1,000,000 (1MB).
184
+ # * `:max_messages` (Integer) The maximum number of messages to be collected before the batch is published.
185
+ # Default is 100.
186
+ # * `:interval` (Numeric) The number of seconds to collect messages before the batch is published. Default is
187
+ # 0.01.
188
+ # * `:threads` (Hash) The number of threads to create to handle concurrent calls by the publisher:
189
+ # * `:publish` (Integer) The number of threads used to publish messages. Default is 2.
190
+ # * `:callback` (Integer) The number of threads to handle the published messages' callbacks. Default is 4.
191
+ #
192
+ # @return [Google::Cloud::PubSub::Topic]
193
+ #
194
+ # @example
195
+ # require "google/cloud/pubsub"
196
+ #
197
+ # pubsub = Google::Cloud::PubSub.new
198
+ # topic = pubsub.create_topic "my-topic"
199
+ #
200
+ def create_topic topic_name, labels: nil, kms_key: nil, persistence_regions: nil, async: nil
201
+ ensure_service!
202
+ grpc = service.create_topic topic_name,
203
+ labels: labels,
204
+ kms_key_name: kms_key,
205
+ persistence_regions: persistence_regions
206
+ Topic.from_grpc grpc, service, async: async
207
+ end
208
+ alias new_topic create_topic
209
+
210
+ ##
211
+ # Retrieves a list of topics for the given project.
212
+ #
213
+ # @param [String] token The `token` value returned by the last call to
214
+ # `topics`; indicates that this is a continuation of a call, and that
215
+ # the system should return the next page of data.
216
+ # @param [Integer] max Maximum number of topics to return.
217
+ #
218
+ # @return [Array<Google::Cloud::PubSub::Topic>] (See
219
+ # {Google::Cloud::PubSub::Topic::List})
220
+ #
221
+ # @example
222
+ # require "google/cloud/pubsub"
223
+ #
224
+ # pubsub = Google::Cloud::PubSub.new
225
+ #
226
+ # topics = pubsub.topics
227
+ # topics.each do |topic|
228
+ # puts topic.name
229
+ # end
230
+ #
231
+ # @example Retrieve all topics: (See {Topic::List#all})
232
+ # require "google/cloud/pubsub"
233
+ #
234
+ # pubsub = Google::Cloud::PubSub.new
235
+ #
236
+ # topics = pubsub.topics
237
+ # topics.all do |topic|
238
+ # puts topic.name
239
+ # end
240
+ #
241
+ def topics token: nil, max: nil
242
+ ensure_service!
243
+ options = { token: token, max: max }
244
+ grpc = service.list_topics options
245
+ Topic::List.from_grpc grpc, service, max
246
+ end
247
+ alias find_topics topics
248
+ alias list_topics topics
249
+
250
+ ##
251
+ # Retrieves subscription by name.
252
+ #
253
+ # @param [String] subscription_name Name of a subscription.
254
+ # @param [String] project If the subscription belongs to a project other
255
+ # than the one currently connected to, the alternate project ID can be
256
+ # specified here.
257
+ # @param [Boolean] skip_lookup Optionally create a {Subscription} object
258
+ # without verifying the subscription resource exists on the Pub/Sub
259
+ # service. Calls made on this object will raise errors if the service
260
+ # resource does not exist. Default is `false`.
261
+ #
262
+ # @return [Google::Cloud::PubSub::Subscription, nil] Returns `nil` if
263
+ # the subscription does not exist
264
+ #
265
+ # @example
266
+ # require "google/cloud/pubsub"
267
+ #
268
+ # pubsub = Google::Cloud::PubSub.new
269
+ #
270
+ # sub = pubsub.subscription "my-sub"
271
+ # sub.name #=> "projects/my-project/subscriptions/my-sub"
272
+ #
273
+ # @example Skip the lookup against the service with `skip_lookup`:
274
+ # require "google/cloud/pubsub"
275
+ #
276
+ # pubsub = Google::Cloud::PubSub.new
277
+ #
278
+ # # No API call is made to retrieve the subscription information.
279
+ # sub = pubsub.subscription "my-sub", skip_lookup: true
280
+ # sub.name #=> "projects/my-project/subscriptions/my-sub"
281
+ #
282
+ def subscription subscription_name, project: nil, skip_lookup: nil
283
+ ensure_service!
284
+ options = { project: project }
285
+ return Subscription.from_name subscription_name, service, options if skip_lookup
286
+ grpc = service.get_subscription subscription_name
287
+ Subscription.from_grpc grpc, service
288
+ rescue Google::Cloud::NotFoundError
289
+ nil
290
+ end
291
+ alias get_subscription subscription
292
+ alias find_subscription subscription
293
+
294
+ ##
295
+ # Retrieves a list of subscriptions for the given project.
296
+ #
297
+ # @param [String] token A previously-returned page token representing
298
+ # part of the larger set of results to view.
299
+ # @param [Integer] max Maximum number of subscriptions to return.
300
+ #
301
+ # @return [Array<Google::Cloud::PubSub::Subscription>] (See
302
+ # {Google::Cloud::PubSub::Subscription::List})
303
+ #
304
+ # @example
305
+ # require "google/cloud/pubsub"
306
+ #
307
+ # pubsub = Google::Cloud::PubSub.new
308
+ #
309
+ # subs = pubsub.subscriptions
310
+ # subs.each do |sub|
311
+ # puts sub.name
312
+ # end
313
+ #
314
+ # @example Retrieve all subscriptions: (See {Subscription::List#all})
315
+ # require "google/cloud/pubsub"
316
+ #
317
+ # pubsub = Google::Cloud::PubSub.new
318
+ #
319
+ # subs = pubsub.subscriptions
320
+ # subs.all do |sub|
321
+ # puts sub.name
322
+ # end
323
+ #
324
+ def subscriptions token: nil, max: nil
325
+ ensure_service!
326
+ options = { token: token, max: max }
327
+ grpc = service.list_subscriptions options
328
+ Subscription::List.from_grpc grpc, service, max
329
+ end
330
+ alias find_subscriptions subscriptions
331
+ alias list_subscriptions subscriptions
332
+
333
+
334
+ ##
335
+ # Retrieves a list of snapshots for the given project.
336
+ #
337
+ # @param [String] token A previously-returned page token representing
338
+ # part of the larger set of results to view.
339
+ # @param [Integer] max Maximum number of snapshots to return.
340
+ #
341
+ # @return [Array<Google::Cloud::PubSub::Snapshot>] (See
342
+ # {Google::Cloud::PubSub::Snapshot::List})
343
+ #
344
+ # @example
345
+ # require "google/cloud/pubsub"
346
+ #
347
+ # pubsub = Google::Cloud::PubSub.new
348
+ #
349
+ # snapshots = pubsub.snapshots
350
+ # snapshots.each do |snapshot|
351
+ # puts snapshot.name
352
+ # end
353
+ #
354
+ # @example Retrieve all snapshots: (See {Snapshot::List#all})
355
+ # require "google/cloud/pubsub"
356
+ #
357
+ # pubsub = Google::Cloud::PubSub.new
358
+ #
359
+ # snapshots = pubsub.snapshots
360
+ # snapshots.all do |snapshot|
361
+ # puts snapshot.name
362
+ # end
363
+ #
364
+ def snapshots token: nil, max: nil
365
+ ensure_service!
366
+ options = { token: token, max: max }
367
+ grpc = service.list_snapshots options
368
+ Snapshot::List.from_grpc grpc, service, max
369
+ end
370
+ alias find_snapshots snapshots
371
+ alias list_snapshots snapshots
372
+
373
+ protected
374
+
375
+ ##
376
+ # @private Raise an error unless an active connection to the service is
377
+ # available.
378
+ def ensure_service!
379
+ raise "Must have active connection to service" unless service
380
+ end
381
+
382
+ ##
383
+ # Call the publish API with arrays of data data and attrs.
384
+ def publish_batch_messages topic_name, batch
385
+ grpc = service.publish topic_name, batch.messages
386
+ batch.to_gcloud_messages Array(grpc.message_ids)
387
+ end
388
+ end
389
+ end
390
+
391
+ Pubsub = PubSub unless const_defined? :Pubsub
392
+ end
393
+ end
@@ -0,0 +1,103 @@
1
+ # Copyright 2017 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ module Google
17
+ module Cloud
18
+ module PubSub
19
+ ##
20
+ # The result of a publish operation. The message object is available on
21
+ # {#message} and will have {#message_id} assigned by the API.
22
+ #
23
+ # When the publish operation was successful the result will be marked
24
+ # {#succeeded?}. Otherwise, the result will be marked {#failed?} and the
25
+ # error raised will be availabe on {#error}.
26
+ #
27
+ class PublishResult
28
+ ##
29
+ # @private Create an PublishResult object.
30
+ def initialize message, error = nil
31
+ @message = message
32
+ @error = error
33
+ end
34
+
35
+ ##
36
+ # The message.
37
+ def message
38
+ @message
39
+ end
40
+ alias msg message
41
+
42
+ ##
43
+ # The message's data.
44
+ def data
45
+ message.data
46
+ end
47
+
48
+ ##
49
+ # The message's attributes.
50
+ def attributes
51
+ message.attributes
52
+ end
53
+
54
+ ##
55
+ # The ID of the message, assigned by the server at publication
56
+ # time. Guaranteed to be unique within the topic.
57
+ def message_id
58
+ message.message_id
59
+ end
60
+ alias msg_id message_id
61
+
62
+ ##
63
+ # The time at which the message was published.
64
+ def published_at
65
+ message.published_at
66
+ end
67
+ alias publish_time published_at
68
+
69
+ ##
70
+ # The error that was raised when published, if any.
71
+ def error
72
+ @error
73
+ end
74
+
75
+ ##
76
+ # Whether the publish request was successful.
77
+ def succeeded?
78
+ error.nil?
79
+ end
80
+
81
+ # Whether the publish request failed.
82
+ def failed?
83
+ !succeeded?
84
+ end
85
+
86
+ ##
87
+ # @private Create an PublishResult object from a message protobuf.
88
+ def self.from_grpc msg_grpc
89
+ new Message.from_grpc(msg_grpc)
90
+ end
91
+
92
+ ##
93
+ # @private Create an PublishResult object from a message protobuf and an
94
+ # error.
95
+ def self.from_error msg_grpc, error
96
+ new Message.from_grpc(msg_grpc), error
97
+ end
98
+ end
99
+ end
100
+
101
+ Pubsub = PubSub unless const_defined? :Pubsub
102
+ end
103
+ end