Sha256: 3017a3b159bff697fe870868df3717b33cbe1bb07ffa175f41839ec5480194f5
Contents?: true
Size: 1.48 KB
Versions: 1
Compression:
Stored size: 1.48 KB
Contents
require 'json' require 'middleware' require 'google/cloud/pubsub' class GooglePubsubEnhancer require_relative 'google_pubsub_enhancer/constants' require_relative 'google_pubsub_enhancer/middleware' 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]) end end def initialize(&block) @stack = ::Middleware::Builder.new(&block) end def run(subscription_short_name, opts={}) configurated_options = configurate_options(opts) subscription = create_subscription(subscription_short_name) work(subscription, configurated_options) rescue 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)) 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.1.5 | lib/google_pubsub_enhancer.rb |