Sha256: 2b46b1369c6035fc6fa3e27b31d8fa8a117d9cc8c6e751c1b6dfebe56f205780

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'google/cloud/pubsub'
module PubSubModelSync
  class Connector
    attr_accessor :service, :topic, :subscription, :config, :subscriber

    def initialize
      @config = PubSubModelSync::Config
      @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 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
      attrs = message.attributes.symbolize_keys
      return unless attrs[:service_model_sync]

      data = JSON.parse(message.data).symbolize_keys
      args = [data, attrs[:klass], attrs[:action], attrs]
      PubSubModelSync::MessageProcessor.new(*args).process
    rescue => e
      log("Error processing message: #{[received_message, e.message]}")
    ensure
      received_message.acknowledge!
    end

    def log(msg)
      config.log(msg)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pub_sub_model_sync-0.1.0 lib/pub_sub_model_sync/connector.rb