Class: Gcpc::Publisher::Engine::BatchEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/gcpc/publisher/engine/batch_engine.rb

Instance Method Summary collapse

Constructor Details

#initialize(topic:, interceptor:) ⇒ BatchEngine

Returns a new instance of BatchEngine.

Parameters:



7
8
9
10
11
# File 'lib/gcpc/publisher/engine/batch_engine.rb', line 7

def initialize(topic:, interceptor:)
  @topic       = topic
  @interceptor = interceptor
  @messages    = []  # Container of data and attributes
end

Instance Method Details

#flushObject

Flush all enqueued messages



27
28
29
30
31
32
33
# File 'lib/gcpc/publisher/engine/batch_engine.rb', line 27

def flush
  @topic.publish do |t|
    @messages.each do |(data, attributes)|
      t.publish data, attributes
    end
  end
end

#publish(data, attributes = {}) ⇒ Object

Enqueue a message

Parameters:

  • data (String)
  • attributes (Hash) (defaults to: {})


17
18
19
20
21
22
23
24
# File 'lib/gcpc/publisher/engine/batch_engine.rb', line 17

def publish(data, attributes = {})
  d = data.dup
  a = attributes.dup

  @interceptor.intercept!(d, a) do |dd, aa|
    @messages << [dd, aa]
  end
end