Class: BBK::App::Dispatcher::QueueStreamStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/bbk/app/dispatcher/queue_stream_strategy.rb

Instance Method Summary collapse

Constructor Details

#initialize(pool, logger:) ⇒ QueueStreamStrategy

Returns a new instance of QueueStreamStrategy.



8
9
10
11
# File 'lib/bbk/app/dispatcher/queue_stream_strategy.rb', line 8

def initialize(pool, logger:)
  @pool = pool
  @logger = logger
end

Instance Method Details

#push(*args) ⇒ Object



35
36
37
# File 'lib/bbk/app/dispatcher/queue_stream_strategy.rb', line 35

def push(*args)
  @stream.push(*args)
end

#run(consumers, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bbk/app/dispatcher/queue_stream_strategy.rb', line 13

def run(consumers, &block)
  @unblocker = Queue.new
  @stream = BBK::App::Dispatcher::MessageStream.new(size: 10)

  consumers.each {|cons| cons.run(@stream) }
  @stream.each do |msg|
    @logger.debug "[#{self.class}] Consumed message #{msg.headers}"
    @pool.post(msg) do |m|
      block.call(m)
    end
  end

  begin
    @pool.shutdown
  rescue StandardError
    nil
  end
  @pool.kill unless @pool.wait_for_termination(@stop_queue_timeout)
ensure
  @unblocker.push(:ok)
end

#stop(timeout = 5) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/bbk/app/dispatcher/queue_stream_strategy.rb', line 39

def stop(timeout = 5)
  @stop_queue_timeout = timeout

  begin
    @stream.close
  rescue StandardError
    nil
  end
  @unblocker.pop
end