Created
December 14, 2018 17:41
-
-
Save Oshiumi/4569ab766d771a30e0a77ae4817aa452 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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