Skip to content

Instantly share code, notes, and snippets.

@Oshiumi
Created Dec 14, 2018
Embed
What would you like to do?
require 'google/cloud/pubsub'
require 'json'
pubsub = Google::Cloud::Pubsub.new(project_id: ENV['GCP_PROJECT_ID'])
count = {}
data = []
sub = pubsub.subscription ENV['PUBSUB_SUBSCRIPTION']
retry_count = 0
1.step do |i|
messages = sub.pull(max: 1000)
messages.each do |m|
data << { 'id' => m.data.to_i, 'index' => i }
count[m.data] ||= 0
count[m.data] += 1
end
sub.acknowledge messages
puts messages.length
if messages.empty?
retry_count += 1
# retry 3 times
break if retry_count > 3
end
end
File.open('count.json', 'w') do |f|
JSON.dump(count, f)
end
File.open('data.json', 'w') do |f|
JSON.dump(data, f)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment