google-cloud-pubsub 0.20.0 → 2.6.1

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 (49) hide show
  1. checksums.yaml +5 -5
  2. data/.yardopts +18 -0
  3. data/AUTHENTICATION.md +178 -0
  4. data/CHANGELOG.md +659 -0
  5. data/CODE_OF_CONDUCT.md +40 -0
  6. data/CONTRIBUTING.md +187 -0
  7. data/EMULATOR.md +37 -0
  8. data/LICENSE +201 -0
  9. data/LOGGING.md +32 -0
  10. data/OVERVIEW.md +528 -0
  11. data/TROUBLESHOOTING.md +31 -0
  12. data/lib/google/cloud/pubsub/async_publisher/batch.rb +310 -0
  13. data/lib/google/cloud/pubsub/async_publisher.rb +402 -0
  14. data/lib/google/cloud/pubsub/batch_publisher.rb +100 -0
  15. data/lib/google/cloud/pubsub/convert.rb +91 -0
  16. data/lib/google/cloud/pubsub/credentials.rb +26 -10
  17. data/lib/google/cloud/pubsub/errors.rb +85 -0
  18. data/lib/google/cloud/pubsub/message.rb +82 -20
  19. data/lib/google/cloud/pubsub/policy.rb +40 -61
  20. data/lib/google/cloud/pubsub/project.rb +405 -265
  21. data/lib/google/cloud/pubsub/publish_result.rb +103 -0
  22. data/lib/google/cloud/pubsub/received_message.rb +165 -30
  23. data/lib/google/cloud/pubsub/retry_policy.rb +88 -0
  24. data/lib/google/cloud/pubsub/schema/list.rb +180 -0
  25. data/lib/google/cloud/pubsub/schema.rb +310 -0
  26. data/lib/google/cloud/pubsub/service.rb +304 -162
  27. data/lib/google/cloud/pubsub/snapshot/list.rb +178 -0
  28. data/lib/google/cloud/pubsub/snapshot.rb +205 -0
  29. data/lib/google/cloud/pubsub/subscriber/enumerator_queue.rb +54 -0
  30. data/lib/google/cloud/pubsub/subscriber/inventory.rb +173 -0
  31. data/lib/google/cloud/pubsub/subscriber/sequencer.rb +115 -0
  32. data/lib/google/cloud/pubsub/subscriber/stream.rb +400 -0
  33. data/lib/google/cloud/pubsub/subscriber/timed_unary_buffer.rb +230 -0
  34. data/lib/google/cloud/pubsub/subscriber.rb +417 -0
  35. data/lib/google/cloud/pubsub/subscription/list.rb +38 -43
  36. data/lib/google/cloud/pubsub/subscription/push_config.rb +268 -0
  37. data/lib/google/cloud/pubsub/subscription.rb +1040 -210
  38. data/lib/google/cloud/pubsub/topic/list.rb +32 -37
  39. data/lib/google/cloud/pubsub/topic.rb +726 -177
  40. data/lib/google/cloud/pubsub/version.rb +6 -4
  41. data/lib/google/cloud/pubsub.rb +138 -413
  42. data/lib/google-cloud-pubsub.rb +60 -42
  43. metadata +88 -39
  44. data/lib/google/cloud/pubsub/topic/publisher.rb +0 -87
  45. data/lib/google/iam/v1/iam_policy.rb +0 -33
  46. data/lib/google/iam/v1/iam_policy_services.rb +0 -30
  47. data/lib/google/iam/v1/policy.rb +0 -25
  48. data/lib/google/pubsub/v1/pubsub_pb.rb +0 -129
  49. data/lib/google/pubsub/v1/pubsub_services_pb.rb +0 -117
@@ -0,0 +1,100 @@
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
+
18
+ module Google
19
+ module Cloud
20
+ module PubSub
21
+ ##
22
+ # Topic Batch Publisher object used to publish multiple messages at
23
+ # once.
24
+ #
25
+ # @example
26
+ # require "google/cloud/pubsub"
27
+ #
28
+ # pubsub = Google::Cloud::PubSub.new
29
+ #
30
+ # topic = pubsub.topic "my-topic"
31
+ # msgs = topic.publish do |batch_publisher|
32
+ # batch_publisher.publish "task 1 completed", foo: :bar
33
+ # batch_publisher.publish "task 2 completed", foo: :baz
34
+ # batch_publisher.publish "task 3 completed", foo: :bif
35
+ # end
36
+ #
37
+ class BatchPublisher
38
+ ##
39
+ # @private The messages to publish
40
+ attr_reader :messages
41
+
42
+ ##
43
+ # @private Create a new instance of the object.
44
+ def initialize data, attributes, ordering_key, extra_attrs
45
+ @messages = []
46
+ @mode = :batch
47
+ return if data.nil?
48
+ @mode = :single
49
+ publish data, attributes, ordering_key: ordering_key, **extra_attrs
50
+ end
51
+
52
+ ##
53
+ # Add a message to the batch to be published to the topic.
54
+ # All messages added to the batch will be published at once.
55
+ # See {Google::Cloud::PubSub::Topic#publish}
56
+ #
57
+ # @param [String, File] data The message payload. This will be converted
58
+ # to bytes encoded as ASCII-8BIT.
59
+ # @param [Hash] attributes Optional attributes for the message.
60
+ # @param [String] ordering_key Identifies related messages for which
61
+ # publish order should be respected.
62
+ #
63
+ # @example Multiple messages can be sent at the same time using a block:
64
+ # require "google/cloud/pubsub"
65
+ #
66
+ # pubsub = Google::Cloud::PubSub.new
67
+ #
68
+ # topic = pubsub.topic "my-topic"
69
+ # msgs = topic.publish do |batch_publisher|
70
+ # batch_publisher.publish "task 1 completed", foo: :bar
71
+ # batch_publisher.publish "task 2 completed", foo: :baz
72
+ # batch_publisher.publish "task 3 completed", foo: :bif
73
+ # end
74
+ #
75
+ def publish data, attributes = nil, ordering_key: nil, **extra_attrs
76
+ msg = Convert.pubsub_message data, attributes, ordering_key, extra_attrs
77
+ @messages << msg
78
+ end
79
+
80
+ ##
81
+ # @private Create Message objects with message ids.
82
+ def to_gcloud_messages message_ids
83
+ msgs = @messages.zip(Array(message_ids)).map do |msg, id|
84
+ msg.message_id = id
85
+ Message.from_grpc msg
86
+ end
87
+ # Return just one Message if a single publish,
88
+ # otherwise return the array of Messages.
89
+ if @mode == :single && msgs.count <= 1
90
+ msgs.first
91
+ else
92
+ msgs
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ Pubsub = PubSub unless const_defined? :Pubsub
99
+ end
100
+ end
@@ -0,0 +1,91 @@
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
+ require "time"
17
+
18
+ module Google
19
+ module Cloud
20
+ module PubSub
21
+ ##
22
+ # @private Helper module for converting Pub/Sub values.
23
+ module Convert
24
+ module ClassMethods
25
+ def time_to_timestamp time
26
+ return nil if time.nil?
27
+
28
+ # Force the object to be a Time object.
29
+ time = time.to_time
30
+
31
+ Google::Protobuf::Timestamp.new seconds: time.to_i, nanos: time.nsec
32
+ end
33
+
34
+ def timestamp_to_time timestamp
35
+ return nil if timestamp.nil?
36
+
37
+ Time.at timestamp.seconds, Rational(timestamp.nanos, 1000)
38
+ end
39
+
40
+ def number_to_duration number
41
+ return nil if number.nil?
42
+
43
+ Google::Protobuf::Duration.new seconds: number.to_i, nanos: (number.remainder(1) * 1_000_000_000).round
44
+ end
45
+
46
+ def duration_to_number duration
47
+ return nil if duration.nil?
48
+
49
+ return duration.seconds if duration.nanos.zero?
50
+
51
+ duration.seconds + (duration.nanos / 1_000_000_000.0)
52
+ end
53
+
54
+ def pubsub_message data, attributes, ordering_key, extra_attrs
55
+ # TODO: allow data to be a Message object,
56
+ # then ensure attributes and ordering_key are nil
57
+ if data.is_a?(::Hash) && (attributes.nil? || attributes.empty?)
58
+ attributes = data.merge extra_attrs
59
+ data = nil
60
+ else
61
+ attributes = Hash(attributes).merge extra_attrs
62
+ end
63
+ # Convert IO-ish objects to strings
64
+ if data.respond_to?(:read) && data.respond_to?(:rewind)
65
+ data.rewind
66
+ data = data.read
67
+ end
68
+ # Convert data to encoded byte array to match the protobuf defn
69
+ data_bytes = String(data).dup.force_encoding(Encoding::ASCII_8BIT).freeze
70
+
71
+ # Convert attributes to strings to match the protobuf definition
72
+ attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]
73
+
74
+ # Ordering Key must always be a string
75
+ ordering_key = String(ordering_key).freeze
76
+
77
+ Google::Cloud::PubSub::V1::PubsubMessage.new(
78
+ data: data_bytes,
79
+ attributes: attributes,
80
+ ordering_key: ordering_key
81
+ )
82
+ end
83
+ end
84
+
85
+ extend ClassMethods
86
+ end
87
+ end
88
+
89
+ Pubsub = PubSub unless const_defined? :Pubsub
90
+ end
91
+ end
@@ -1,10 +1,10 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
1
+ # Copyright 2015 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,19 +13,35 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require "google/cloud/credentials"
16
+ require "googleauth"
17
+ require "google/cloud/pubsub/v1/publisher/credentials"
17
18
 
18
19
  module Google
19
20
  module Cloud
20
- module Pubsub
21
+ module PubSub
21
22
  ##
22
- # @private Represents the OAuth 2.0 signing logic for Pub/Sub.
23
- class Credentials < Google::Cloud::Credentials
24
- SCOPE = ["https://www.googleapis.com/auth/pubsub"]
25
- PATH_ENV_VARS = %w(PUBSUB_KEYFILE GOOGLE_CLOUD_KEYFILE GCLOUD_KEYFILE)
26
- JSON_ENV_VARS = %w(PUBSUB_KEYFILE_JSON GOOGLE_CLOUD_KEYFILE_JSON
27
- GCLOUD_KEYFILE_JSON)
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::Publisher::Credentials
28
42
  end
29
43
  end
44
+
45
+ Pubsub = PubSub unless const_defined? :Pubsub
30
46
  end
31
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
@@ -1,10 +1,10 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
1
+ # Copyright 2015 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,11 +13,12 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
+ require "google/cloud/pubsub/convert"
16
17
  require "google/cloud/errors"
17
18
 
18
19
  module Google
19
20
  module Cloud
20
- module Pubsub
21
+ module PubSub
21
22
  ##
22
23
  # # Message
23
24
  #
@@ -29,24 +30,31 @@ module Google
29
30
  # delayed.
30
31
  #
31
32
  # @example
32
- # require "google/cloud"
33
+ # require "google/cloud/pubsub"
33
34
  #
34
- # gcloud = Google::Cloud.new
35
- # pubsub = gcloud.pubsub
35
+ # pubsub = Google::Cloud::PubSub.new
36
36
  #
37
37
  # # Publish a message
38
38
  # topic = pubsub.topic "my-topic"
39
- # message = topic.publish "new-message"
40
- # puts message.data #=> "new-message"
39
+ # message = topic.publish "task completed"
40
+ # message.data #=> "task completed"
41
41
  #
42
- # # Pull a message
42
+ # # Listen for messages
43
43
  # sub = pubsub.subscription "my-topic-sub"
44
- # received_message = sub.pull.first
45
- # puts received_message.message.data #=> "new-message"
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!
46
54
  #
47
55
  class Message
48
56
  ##
49
- # @private The gRPC Google::Pubsub::V1::PubsubMessage object.
57
+ # @private The gRPC Google::Cloud::PubSub::V1::PubsubMessage object.
50
58
  attr_accessor :grpc
51
59
 
52
60
  ##
@@ -56,19 +64,21 @@ module Google
56
64
  # Convert attributes to strings to match the protobuf definition
57
65
  attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]
58
66
 
59
- @grpc = Google::Pubsub::V1::PubsubMessage.new(
60
- data: String(data).encode("ASCII-8BIT"),
61
- attributes: attributes)
67
+ @grpc = Google::Cloud::PubSub::V1::PubsubMessage.new(
68
+ data: String(data).dup.force_encoding(Encoding::ASCII_8BIT),
69
+ attributes: attributes
70
+ )
62
71
  end
63
72
 
64
73
  ##
65
- # The received data.
74
+ # The message payload. This data is a list of bytes encoded as
75
+ # ASCII-8BIT.
66
76
  def data
67
77
  @grpc.data
68
78
  end
69
79
 
70
80
  ##
71
- # The received attributes.
81
+ # Optional attributes for the message.
72
82
  def attributes
73
83
  return @grpc.attributes.to_h if @grpc.attributes.respond_to? :to_h
74
84
  # Enumerable doesn't have to_h on Ruby 2.0, so fallback to this
@@ -81,16 +91,68 @@ module Google
81
91
  def message_id
82
92
  @grpc.message_id
83
93
  end
84
- alias_method :msg_id, :message_id
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
85
102
 
86
103
  ##
87
- # @private New Message from a Google::Pubsub::V1::PubsubMessage object.
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.
88
148
  def self.from_grpc grpc
89
149
  new.tap do |m|
90
- m.instance_variable_set "@grpc", grpc
150
+ m.instance_variable_set :@grpc, grpc
91
151
  end
92
152
  end
93
153
  end
94
154
  end
155
+
156
+ Pubsub = PubSub unless const_defined? :Pubsub
95
157
  end
96
158
  end