Class: GooglePubsubEnhancer::Middleware::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/google_pubsub_enhancer/middleware/publisher.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Publisher

Returns a new instance of Publisher.



3
4
5
6
7
8
9
10
# File 'lib/google_pubsub_enhancer/middleware/publisher.rb', line 3

def initialize(app, opts={})
  @app = app
  @short_topic_name = opts[:short_topic_name] || raise
  @full_topic_name = GooglePubsubEnhancer.name_by('topics',@short_topic_name)
  @messages_key = opts[:messages] || raise
  @logger = opts[:logger] || Logger.new(STDOUT)
  @google_cloud_pubsub ||= Google::Cloud::Pubsub.new
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/google_pubsub_enhancer/middleware/publisher.rb', line 12

def call(env)
  @logger.debug("#{env[@messages_key].length} messages published")
  @google_cloud_pubsub.publish(@full_topic_name) do |publisher|
    [*env[@messages_key]].each do |m|
      publisher.publish(m)
    end
  end
rescue => ex
  @logger.error("Retry publisher: #{ex}")
  retry
  @app.call(env)
end