/
subscriptions.rb
316 lines (267 loc) · 12.2 KB
/
subscriptions.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright 2018 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "google/cloud/pubsub"
def update_push_configuration project_id:, subscription_name:, new_endpoint:
# [START pubsub_update_push_configuration]
# project_id = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
# new_endpoint = "Endpoint where your app receives messages""
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscription = pubsub.subscription subscription_name
subscription.endpoint = new_endpoint
puts "Push endpoint updated."
# [END pubsub_update_push_configuration]
end
def list_subscriptions project_id:
# [START pubsub_list_subscriptions]
# project_id = Your Google Cloud Project ID
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscriptions = pubsub.list_subscriptions
puts "Subscriptions:"
subscriptions.each do |subscription|
puts subscription.name
end
# [END pubsub_list_subscriptions]
end
def delete_subscription project_id:, subscription_name:
# [START pubsub_delete_subscription]
# project_id = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscription = pubsub.subscription subscription_name
subscription.delete
puts "Subscription #{subscription_name} deleted."
# [END pubsub_delete_subscription]
end
def get_subscription_policy project_id:, subscription_name:
# [START pubsub_get_subscription_policy]
# project_id = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscription = pubsub.subscription subscription_name
policy = subscription.policy
puts "Subscription policy:"
puts policy.roles
# [END pubsub_get_subscription_policy]
end
def set_subscription_policy project_id:, subscription_name:
# [START pubsub_set_subscription_policy]
# project_id = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscription = pubsub.subscription subscription_name
subscription.policy do |policy|
policy.add "roles/pubsub.subscriber",
"serviceAccount:account-name@project-name.iam.gserviceaccount.com"
end
# [END pubsub_set_subscription_policy]
end
def test_subscription_permissions project_id:, subscription_name:
# [START pubsub_test_subscription_permissions]
# project_id = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscription = pubsub.subscription subscription_name
permissions = subscription.test_permissions "pubsub.subscriptions.consume",
"pubsub.subscriptions.update"
puts "Permission to consume" if permissions.include? "pubsub.subscriptions.consume"
puts "Permission to update" if permissions.include? "pubsub.subscriptions.update"
# [END pubsub_test_subscription_permissions]
end
def listen_for_messages project_id:, subscription_name:
# [START pubsub_quickstart_subscriber]
# project_id = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscription = pubsub.subscription subscription_name
subscriber = subscription.listen do |received_message|
puts "Received message: #{received_message.data}"
received_message.acknowledge!
end
subscriber.start
# Let the main thread sleep for 60 seconds so the thread for listening
# messages does not quit
sleep 60
subscriber.stop.wait!
# [END pubsub_quickstart_subscriber]
end
def listen_for_messages_with_custom_attributes project_id:, subscription_name:
# [START pubsub_subscriber_sync_pull_custom_attributes]
# project_id = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscription = pubsub.subscription subscription_name
subscriber = subscription.listen do |received_message|
puts "Received message: #{received_message.data}"
unless received_message.attributes.empty?
puts "Attributes:"
received_message.attributes.each do |key, value|
puts "#{key}: #{value}"
end
end
received_message.acknowledge!
end
subscriber.start
# Let the main thread sleep for 60 seconds so the thread for listening
# messages does not quit
sleep 60
subscriber.stop.wait!
# [END pubsub_subscriber_sync_pull_custom_attributes]
end
def pull_messages project_id:, subscription_name:
# [START pubsub_subscriber_sync_pull]
# project_id = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscription = pubsub.subscription subscription_name
subscription.pull.each do |message|
puts "Message pulled: #{message.data}"
message.acknowledge!
end
# [END pubsub_subscriber_sync_pull]
end
def listen_for_messages_with_error_handler project_id:, subscription_name:
# [START pubsub_subscriber_error_listener]
# project_id = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscription = pubsub.subscription subscription_name
subscriber = subscription.listen do |received_message|
puts "Received message: #{received_message.data}"
received_message.acknowledge!
end
# Propagate expection from child threads to the main thread as soon as it is
# raised. Exceptions happened in the callback thread are collected in the
# callback thread pool and do not propagate to the main thread
Thread.abort_on_exception = true
begin
subscriber.start
# Let the main thread sleep for 60 seconds so the thread for listening
# messages does not quit
sleep 60
subscriber.stop.wait!
rescue Exception => ex
puts "Exception #{ex.inspect}: #{ex.message}"
raise "Stopped listening for messages."
end
# [END pubsub_subscriber_error_listener]
end
def listen_for_messages_with_flow_control project_id:, subscription_name:
# [START pubsub_subscriber_flow_settings]
# project_id = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscription = pubsub.subscription subscription_name
subscriber = subscription.listen inventory: 10 do |received_message|
puts "Received message: #{received_message.data}"
received_message.acknowledge!
end
subscriber.start
# Let the main thread sleep for 60 seconds so the thread for listening
# messages does not quit
sleep 60
subscriber.stop.wait!
# [END pubsub_subscriber_flow_settings]
end
def listen_for_messages_with_concurrency_control project_id:, subscription_name:
# [START pubsub_subscriber_concurrency_control]
# project_id = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
require "google/cloud/pubsub"
pubsub = Google::Cloud::Pubsub.new project: project_id
subscription = pubsub.subscription subscription_name
# Use 2 threads for streaming, 4 threads for executing callbacks and 2 threads
# for sending acknowledgements and/or delays
subscriber = subscription.listen streams: 2, threads: {
callback: 4,
push: 2
} do |received_message|
puts "Received message: #{received_message.data}"
received_message.acknowledge!
end
subscriber.start
# Let the main thread sleep for 60 seconds so the thread for listening
# messages does not quit
sleep 60
subscriber.stop.wait!
# [END pubsub_subscriber_concurrency_control]
end
if $PROGRAM_NAME == __FILE__
case ARGV.shift
when "update_push_configuration"
update_push_configuration project_id: ARGV.shift,
subscription_name: ARGV.shift,
new_endpoint: ARGV.shift
when "list_subscriptions"
list_subscriptions project_id: ARGV.shift
when "delete_subscription"
delete_subscription project_id: ARGV.shift,
subscription_name: ARGV.shift
when "get_subscription_policy"
get_subscription_policy project_id: ARGV.shift,
subscription_name: ARGV.shift
when "set_subscription_policy"
set_subscription_policy project_id: ARGV.shift,
subscription_name: ARGV.shift
when "test_subscription_permissions"
test_subscription_permissions project_id: ARGV.shift,
subscription_name: ARGV.shift
when "listen_for_messages"
listen_for_messages project_id: ARGV.shift,
subscription_name: ARGV.shift
when "listen_for_messages_with_custom_attributes"
listen_for_messages_with_custom_attributes project_id: ARGV.shift,
subscription_name: ARGV.shift
when "pull_messages"
pull_messages project_id: ARGV.shift,
subscription_name: ARGV.shift
when "listen_for_messages_with_error_handler"
listen_for_messages_with_error_handler project_id: ARGV.shift,
subscription_name: ARGV.shift
when "listen_for_messages_with_flow_control"
listen_for_messages_with_flow_control project_id: ARGV.shift,
subscription_name: ARGV.shift
when "listen_for_messages_with_concurrency_control"
listen_for_messages_with_concurrency_control project_id: ARGV.shift,
subscription_name: ARGV.shift
else
puts <<~USAGE
Usage: bundle exec ruby subscriptions.rb [command] [arguments]
Commands:
update_push_configuration <project_id> <subscription_name> <endpoint> Update the endpoint of a push subscription
list_subscriptions <project_id> List subscriptions of a project
delete_subscription <project_id> <subscription_name> Delete a subscription
get_subscription_policy <project_id> <subscription_name> Get policies of a subscription
set_subscription_policy <project_id> <subscription_name> Set policies of a subscription
test_subscription_policy <project_id> <subscription_name> Test policies of a subscription
listen_for_messages <project_id> <subscription_name> Listen for messages
listen_for_messages_with_custom_attributes <project_id> <subscription_name> Listen for messages with custom attributes
pull_messages <project_id> <subscription_name> Pull messages
listen_for_messages_with_error_handler <project_id> <subscription_name> Listen for messages with an error handler
listen_for_messages_with_flow_control <project_id> <subscription_name> Listen for messages with flow control
listen_for_messages_with_concurrency_control <project_id> <subscription_name> Listen for messages with concurrency control
USAGE
end
end