Sha256: d4be80917b992d2e1cfa0cb4f1c3812c54019a1f0220b85c5de7a17ab020348b
Contents?: true
Size: 1.34 KB
Versions: 5
Compression:
Stored size: 1.34 KB
Contents
# frozen_string_literal: true begin require 'google/cloud/pubsub' rescue LoadError # rubocop:disable Lint/SuppressedException end module PubSubModelSync class ServiceGoogle < ServiceBase attr_accessor :service, :topic, :subscription, :subscriber def initialize @service = Google::Cloud::Pubsub.new(project: config.project, credentials: config.credentials) @topic = service.topic(config.topic_name) || service.create_topic(config.topic_name) end def listen_messages @subscription = subscribe_to_topic @subscriber = subscription.listen(&method(:process_message)) log('Listener starting...') subscriber.start log('Listener started') sleep subscriber.stop.wait! log('Listener stopped') end def publish(payload) topic.publish(payload.to_json, { SERVICE_KEY => true }) end def stop log('Listener stopping...') subscriber.stop! end private def subscribe_to_topic topic.subscription(config.subscription_name) || topic.subscribe(config.subscription_name) end def process_message(received_message) message = received_message.message super(message.data) if message.attributes[SERVICE_KEY] ensure received_message.acknowledge! end end end
Version data entries
5 entries across 5 versions & 1 rubygems