google-cloud-pubsub 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
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,47 @@
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 "googleauth"
17
+ require "google/cloud/pubsub/v1/credentials.rb"
18
+
19
+ module Google
20
+ module Cloud
21
+ module PubSub
22
+ ##
23
+ # # Credentials
24
+ #
25
+ # Represents the authentication and authorization used to connect to the
26
+ # Pub/Sub API.
27
+ #
28
+ # @example
29
+ # require "google/cloud/pubsub"
30
+ #
31
+ # keyfile = "/path/to/keyfile.json"
32
+ # creds = Google::Cloud::PubSub::Credentials.new keyfile
33
+ #
34
+ # pubsub = Google::Cloud::PubSub.new(
35
+ # project_id: "my-project",
36
+ # credentials: creds
37
+ # )
38
+ #
39
+ # pubsub.project_id #=> "my-project"
40
+ #
41
+ class Credentials < Google::Cloud::PubSub::V1::Credentials
42
+ end
43
+ end
44
+
45
+ Pubsub = PubSub unless const_defined? :Pubsub
46
+ end
47
+ end
@@ -0,0 +1,85 @@
1
+ # Copyright 2019 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
+
18
+ module Google
19
+ module Cloud
20
+ module PubSub
21
+ ##
22
+ # Indicates that the {AsyncPublisher} has been stopped and cannot accept
23
+ # messages to publish.
24
+ #
25
+ class AsyncPublisherStopped < Google::Cloud::Error
26
+ def initialize message = "Can't publish when stopped."
27
+ super message
28
+ end
29
+ end
30
+
31
+ ##
32
+ # Indicates that the {AsyncPublisher} has not been enabled to publish
33
+ # messages with an ordering key. Use
34
+ # {AsyncPublisher#enable_message_ordering!} to enable publishing ordered
35
+ # messages.
36
+ #
37
+ class OrderedMessagesDisabled < Google::Cloud::Error
38
+ def initialize message = "Ordered messages are disabled."
39
+ super message
40
+ end
41
+ end
42
+
43
+ ##
44
+ # Indicates that the {Subscriber} for a {Subscription} with message
45
+ # ordering enabled has observed that a message has been delivered out of
46
+ # order.
47
+ #
48
+ class OrderedMessageDeliveryError < Google::Cloud::Error
49
+ attr_reader :ordered_message
50
+
51
+ def initialize ordered_message
52
+ @ordered_message = ordered_message
53
+
54
+ super "Ordered message delivered out of order."
55
+ end
56
+ end
57
+
58
+ ##
59
+ # Indicates that messages using the {#ordering_key} are not being
60
+ # published due to error. Future calls to {Topic#publish_async} with the
61
+ # {#ordering_key} will fail with this error.
62
+ #
63
+ # To allow future messages with the {#ordering_key} to be published, the
64
+ # {#ordering_key} must be passed to {Topic#resume_publish}.
65
+ #
66
+ # If this error is retrieved from {PublishResult#error}, inspect `cause`
67
+ # for the error raised while publishing.
68
+ #
69
+ # @!attribute [r] ordering_key
70
+ # @return [String] The ordering key that is in a failed state.
71
+ #
72
+ class OrderingKeyError < Google::Cloud::Error
73
+ attr_reader :ordering_key
74
+
75
+ def initialize ordering_key
76
+ @ordering_key = ordering_key
77
+
78
+ super "Can't publish message using #{ordering_key}."
79
+ end
80
+ end
81
+ end
82
+
83
+ Pubsub = PubSub unless const_defined? :Pubsub
84
+ end
85
+ end
@@ -0,0 +1,158 @@
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/pubsub/convert"
17
+ require "google/cloud/errors"
18
+
19
+ module Google
20
+ module Cloud
21
+ module PubSub
22
+ ##
23
+ # # Message
24
+ #
25
+ # Represents a Pub/Sub Message.
26
+ #
27
+ # Message objects are created by {Topic#publish}. {Subscription#pull}
28
+ # returns an array of {ReceivedMessage} objects, each of which contains a
29
+ # Message object. Each {ReceivedMessage} object can be acknowledged and/or
30
+ # delayed.
31
+ #
32
+ # @example
33
+ # require "google/cloud/pubsub"
34
+ #
35
+ # pubsub = Google::Cloud::PubSub.new
36
+ #
37
+ # # Publish a message
38
+ # topic = pubsub.topic "my-topic"
39
+ # message = topic.publish "task completed"
40
+ # message.data #=> "task completed"
41
+ #
42
+ # # Listen for messages
43
+ # sub = pubsub.subscription "my-topic-sub"
44
+ # subscriber = sub.listen do |received_message|
45
+ # # process message
46
+ # received_message.acknowledge!
47
+ # end
48
+ #
49
+ # # Start background threads that will call the block passed to listen.
50
+ # subscriber.start
51
+ #
52
+ # # Shut down the subscriber when ready to stop receiving messages.
53
+ # subscriber.stop.wait!
54
+ #
55
+ class Message
56
+ ##
57
+ # @private The gRPC Google::Cloud::PubSub::V1::PubsubMessage object.
58
+ attr_accessor :grpc
59
+
60
+ ##
61
+ # Create an empty Message object.
62
+ # This can be used to publish several messages in bulk.
63
+ def initialize data = nil, attributes = {}
64
+ # Convert attributes to strings to match the protobuf definition
65
+ attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]
66
+
67
+ @grpc = Google::Cloud::PubSub::V1::PubsubMessage.new(
68
+ data: String(data).dup.force_encoding(Encoding::ASCII_8BIT),
69
+ attributes: attributes
70
+ )
71
+ end
72
+
73
+ ##
74
+ # The message payload. This data is a list of bytes encoded as
75
+ # ASCII-8BIT.
76
+ def data
77
+ @grpc.data
78
+ end
79
+
80
+ ##
81
+ # Optional attributes for the message.
82
+ def attributes
83
+ return @grpc.attributes.to_h if @grpc.attributes.respond_to? :to_h
84
+ # Enumerable doesn't have to_h on Ruby 2.0, so fallback to this
85
+ Hash[@grpc.attributes.to_a]
86
+ end
87
+
88
+ ##
89
+ # The ID of this message, assigned by the server at publication time.
90
+ # Guaranteed to be unique within the topic.
91
+ def message_id
92
+ @grpc.message_id
93
+ end
94
+ alias msg_id message_id
95
+
96
+ ##
97
+ # The time at which the message was published.
98
+ def published_at
99
+ Convert.timestamp_to_time @grpc.publish_time
100
+ end
101
+ alias publish_time published_at
102
+
103
+ ##
104
+ # Identifies related messages for which publish order should be
105
+ # respected.
106
+ #
107
+ # Google Cloud Pub/Sub ordering keys provide the ability to ensure
108
+ # related messages are sent to subscribers in the order in which they
109
+ # were published. Messages can be tagged with an ordering key, a string
110
+ # that identifies related messages for which publish order should be
111
+ # respected. The service guarantees that, for a given ordering key and
112
+ # publisher, messages are sent to subscribers in the order in which they
113
+ # were published. Ordering does not require sacrificing high throughput
114
+ # or scalability, as the service automatically distributes messages for
115
+ # different ordering keys across subscribers.
116
+ #
117
+ # See {Topic#publish_async} and {Subscription#listen}.
118
+ #
119
+ # @return [String]
120
+ #
121
+ def ordering_key
122
+ @grpc.ordering_key
123
+ end
124
+
125
+ # @private
126
+ def hash
127
+ @grpc.hash
128
+ end
129
+
130
+ # @private
131
+ def eql? other
132
+ return false unless other.is_a? self.class
133
+ @grpc.hash == other.hash
134
+ end
135
+ # @private
136
+ alias == eql?
137
+
138
+ # @private
139
+ def <=> other
140
+ return nil unless other.is_a? self.class
141
+ other_grpc = other.instance_variable_get :@grpc
142
+ @grpc <=> other_grpc
143
+ end
144
+
145
+ ##
146
+ # @private New Message from a Google::Cloud::PubSub::V1::PubsubMessage
147
+ # object.
148
+ def self.from_grpc grpc
149
+ new.tap do |m|
150
+ m.instance_variable_set :@grpc, grpc
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ Pubsub = PubSub unless const_defined? :Pubsub
157
+ end
158
+ end
@@ -0,0 +1,187 @@
1
+ # Copyright 2016 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
+
18
+ module Google
19
+ module Cloud
20
+ module PubSub
21
+ ##
22
+ # # Policy
23
+ #
24
+ # Represents a Cloud IAM Policy for the Pub/Sub service.
25
+ #
26
+ # A common pattern for updating a resource's metadata, such as its Policy,
27
+ # is to read the current data from the service, update the data locally,
28
+ # and then send the modified data for writing. This pattern may result in
29
+ # a conflict if two or more processes attempt the sequence simultaneously.
30
+ # IAM solves this problem with the {Google::Cloud::PubSub::Policy#etag}
31
+ # property, which is used to verify whether the policy has changed since
32
+ # the last request. When you make a request to with an `etag` value, Cloud
33
+ # IAM compares the `etag` value in the request with the existing `etag`
34
+ # value associated with the policy. It writes the policy only if the
35
+ # `etag` values match.
36
+ #
37
+ # When you update a policy, first read the policy (and its current `etag`)
38
+ # from the service, then modify the policy locally, and then write the
39
+ # modified policy to the service. See
40
+ # {Google::Cloud::PubSub::Topic#policy} and
41
+ # {Google::Cloud::PubSub::Topic#policy=}.
42
+ #
43
+ # @see https://cloud.google.com/iam/docs/managing-policies Managing
44
+ # policies
45
+ # @see https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#iampolicy
46
+ # google.iam.v1.IAMPolicy
47
+ #
48
+ # @attr [String] etag Used to verify whether the policy has changed since
49
+ # the last request. The policy will be written only if the `etag` values
50
+ # match.
51
+ # @attr [Hash{String => Array<String>}] roles The bindings that associate
52
+ # roles with an array of members. See [Understanding
53
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
54
+ # listing of primitive and curated roles.
55
+ # See [Binding](https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#binding)
56
+ # for a listing of values and patterns for members.
57
+ #
58
+ # @example
59
+ # require "google/cloud/pubsub"
60
+ #
61
+ # pubsub = Google::Cloud::PubSub.new
62
+ # topic = pubsub.topic "my-topic"
63
+ #
64
+ # topic.policy do |p|
65
+ # p.remove "roles/owner", "user:owner@example.com"
66
+ # p.add "roles/owner", "user:newowner@example.com"
67
+ # p.roles["roles/viewer"] = ["allUsers"]
68
+ # end
69
+ #
70
+ class Policy
71
+ attr_reader :etag, :roles
72
+
73
+ ##
74
+ # @private Creates a Policy object.
75
+ def initialize etag, roles
76
+ @etag = etag
77
+ @roles = roles
78
+ end
79
+
80
+ ##
81
+ # Convenience method for adding a member to a binding on this policy.
82
+ # See [Understanding
83
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
84
+ # listing of primitive and curated roles.
85
+ # See [Binding](https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#binding)
86
+ # for a listing of values and patterns for members.
87
+ #
88
+ # @param [String] role_name A Cloud IAM role, such as
89
+ # `"roles/pubsub.admin"`.
90
+ # @param [String] member A Cloud IAM identity, such as
91
+ # `"user:owner@example.com"`.
92
+ #
93
+ # @example
94
+ # require "google/cloud/pubsub"
95
+ #
96
+ # pubsub = Google::Cloud::PubSub.new
97
+ # topic = pubsub.topic "my-topic"
98
+ #
99
+ # topic.policy do |p|
100
+ # p.add "roles/owner", "user:newowner@example.com"
101
+ # end
102
+ #
103
+ def add role_name, member
104
+ role(role_name) << member
105
+ end
106
+
107
+ ##
108
+ # Convenience method for removing a member from a binding on this
109
+ # policy. See [Understanding
110
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
111
+ # listing of primitive and curated roles. See
112
+ # [Binding](https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#binding)
113
+ # for a listing of values and patterns for members.
114
+ #
115
+ # @param [String] role_name A Cloud IAM role, such as
116
+ # `"roles/pubsub.admin"`.
117
+ # @param [String] member A Cloud IAM identity, such as
118
+ # `"user:owner@example.com"`.
119
+ #
120
+ # @example
121
+ # require "google/cloud/pubsub"
122
+ #
123
+ # pubsub = Google::Cloud::PubSub.new
124
+ # topic = pubsub.topic "my-topic"
125
+ #
126
+ # topic.policy do |p|
127
+ # p.remove "roles/owner", "user:owner@example.com"
128
+ # end
129
+ #
130
+ def remove role_name, member
131
+ role(role_name).delete member
132
+ end
133
+
134
+ ##
135
+ # Convenience method returning the array of members bound to a role in
136
+ # this policy, or an empty array if no value is present for the role in
137
+ # {#roles}. See [Understanding
138
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
139
+ # listing of primitive and curated roles. See
140
+ # [Binding](https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#binding)
141
+ # for a listing of values and patterns for members.
142
+ #
143
+ # @return [Array<String>] The members strings, or an empty array.
144
+ #
145
+ # @example
146
+ # require "google/cloud/pubsub"
147
+ #
148
+ # pubsub = Google::Cloud::PubSub.new
149
+ # topic = pubsub.topic "my-topic"
150
+ #
151
+ # topic.policy do |p|
152
+ # p.role("roles/viewer") << "user:viewer@example.com"
153
+ # end
154
+ #
155
+ def role role_name
156
+ roles[role_name] ||= []
157
+ end
158
+
159
+ ##
160
+ # @private Convert the Policy to a Google::Iam::V1::Policy object.
161
+ def to_grpc
162
+ Google::Iam::V1::Policy.new(
163
+ etag: etag,
164
+ bindings: roles.keys.map do |role_name|
165
+ next if roles[role_name].empty?
166
+ Google::Iam::V1::Binding.new(
167
+ role: role_name,
168
+ members: roles[role_name]
169
+ )
170
+ end
171
+ )
172
+ end
173
+
174
+ ##
175
+ # @private New Policy from a Google::Iam::V1::Policy object.
176
+ def self.from_grpc grpc
177
+ roles = grpc.bindings.each_with_object({}) do |binding, memo|
178
+ memo[binding.role] = binding.members.to_a
179
+ end
180
+ new grpc.etag, roles
181
+ end
182
+ end
183
+ end
184
+
185
+ Pubsub = PubSub unless const_defined? :Pubsub
186
+ end
187
+ end