Skip to content

Instantly share code, notes, and snippets.

@Oshiumi
Created December 14, 2018 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oshiumi/4569ab766d771a30e0a77ae4817aa452 to your computer and use it in GitHub Desktop.
Save Oshiumi/4569ab766d771a30e0a77ae4817aa452 to your computer and use it in GitHub Desktop.
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