Skip to content

Instantly share code, notes, and snippets.

@ianks
Last active June 6, 2018 14:38
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 ianks/65b45a3d24f26992dafea2f0e7b09fd6 to your computer and use it in GitHub Desktop.
Save ianks/65b45a3d24f26992dafea2f0e7b09fd6 to your computer and use it in GitHub Desktop.
Threads in google-cloud-pubsub
---
version: '3.6'
services:
pubsub:
image: adhawk/google-pubsub-emulator
ports:
- 8085:8085
require 'google/cloud/pubsub'
ENV['PUBSUB_EMULATOR_HOST'] ||= 'localhost:8085'
QUEUE = Queue.new
pubsub = Google::Cloud::Pubsub.new(
project_id: 'my-project'
)
# Retrieve a topic
topic = pubsub.topic 'my-topic'
msg = topic.publish 'new-message'
require 'google/cloud/pubsub'
ENV['PUBSUB_EMULATOR_HOST'] ||= 'localhost:8085'
QUEUE = Queue.new
pubsub = Google::Cloud::Pubsub.new(
project_id: 'my-project'
)
# Retrieve a topic
topic = pubsub.find_topic('my-topic') || pubsub.create_topic('my-topic')
# Retrieve a subscription
sub = topic.find_subscription('my-topic-sub') || topic.create_subscription('my-topic-sub')
# Create a subscriber to listen for available messages
subscriber = sub.listen do |received_message|
QUEUE << 'msg received'
received_message.acknowledge!
end
subscriber.start
# Generate a backtrace string for given exception.
# Generated string is a series of lines, each beginning with a tab and "at ".
def pretty_backtrace(thread)
"\tat #{thread.backtrace.join("\n\tat ")}"
end
# Generate a string containing exception message followed by backtrace.
def pretty_thread(thread)
"#{thread.inspect}: #{thread.status}\n#{pretty_backtrace(thread)}"
end
begin
loop do
puts
puts '==========='
puts '=== Backtraces ==='
puts Thread.list.map { |thr| pretty_thread(thr) }.join("\n\n")
puts '=== Info ==='
puts 'Thread count:' + Thread.list.count.to_s
puts '==========='
puts QUEUE.pop
end
rescue Interrupt
subscriber.stop.wait!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment