Sha256: 40a3c995b7578ba949cd6f9e7e50b92986988fedffd59c076028f6507f923d21
Contents?: true
Size: 1.52 KB
Versions: 4
Compression:
Stored size: 1.52 KB
Contents
# frozen_string_literal: true begin require 'google/cloud/pubsub' rescue LoadError # rubocop:disable Lint/SuppressedException end module PubSubModelSync class ServiceGoogle < ServiceBase LISTEN_SETTINGS = { threads: { callback: 1 } }.freeze SUBSCRIPTION_SETTINGS = { message_ordering: true }.freeze 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(LISTEN_SETTINGS, &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 }.merge(PUBLISH_SETTINGS)) end def stop log('Listener stopping...') subscriber.stop! end private def subscribe_to_topic topic.subscription(config.subscription_key) || topic.subscribe(config.subscription_key, SUBSCRIPTION_SETTINGS) 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
4 entries across 4 versions & 1 rubygems