Sha256: cbda52f6a6bfb2fc351366fb2637df5f5976cdd4330db664a155d0b25e08b599

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

require 'json'
require 'middleware'
require 'google/cloud/pubsub'
require 'logger'

class GooglePubsubEnhancer

  require 'google_pubsub_enhancer/constants'
  require 'google_pubsub_enhancer/middleware'
  require 'google_pubsub_enhancer/spec'

  class << self
    def name_by(type, name)
      raise unless %w(topics subscriptions).include?(type)
      "projects/#{pubsub_config['project_id']}/#{type}/#{name}"
    end

    def pubsub_config
      key = ::Google::Cloud::Pubsub::Credentials::JSON_ENV_VARS.find { |n| !ENV[n].nil? }
      @pubsub_config ||= JSON.parse(ENV[key])
    rescue => ex
      raise Exception, 'Environment not setted properly'
    end
  end

  def initialize(logger: Logger.new(STDOUT),&block)
    @logger = logger
    @stack = ::Middleware::Builder.new(&block).__send__(:to_app)
  end

  def run(subscription_short_name, opts={})
    configurated_options = configurate_options(opts)
    subscription = create_subscription(subscription_short_name)
    work(subscription, configurated_options)
  rescue => ex
    @logger.error "Retry: #{ex} "
    retry
  end

  private

  def work(subscription, opts)
    while received_messages = subscription.pull(:max => GooglePubsubEnhancer::Constants::MAX_PULL_SIZE)
      break if opts[:shutdown].call || received_messages == nil
      next if received_messages.empty?
      @stack.call({received_messages: received_messages})
      subscription.acknowledge(received_messages)
    end
  end

  def create_subscription(subscription_short_name)
    Google::Cloud::Pubsub.new.subscription(self.class.name_by('subscriptions', subscription_short_name))
  rescue => ex
    raise Exception, 'Environment not setted properly'
  end

  def configurate_options(opts)
    raise unless opts.is_a?(Hash)
    opts[:shutdown] ||= proc { }
    opts
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
google-pubsub-enhancer-0.4.0 lib/google_pubsub_enhancer.rb