lib/whirled_peas/frame/producer.rb in whirled_peas-0.4.1 vs lib/whirled_peas/frame/producer.rb in whirled_peas-0.5.0
- old
+ new
@@ -1,31 +1,37 @@
require 'socket'
require 'json'
-require_relative '../null_logger'
+require 'whirled_peas/null_logger'
module WhirledPeas
module Frame
+ # A Producer is the object given to the driver as the interface that allows
+ # the driver to emit frame events. The recommended way of creating a Producer
+ # is by invoking `Producer.produce` as it handles the lifecycle methods of
+ # the consumer.
class Producer
LOGGER_ID = 'PRODUCER'
- # Manages the EventLoop lifecycle and yields a Producer to send frames to the
- # EventLoop
+ # Manages the consumer lifecycle and yields a Producer to send frames to the
+ # consumer
+ #
+ # @param consumer [Consumer] instance that consumes frame events through
+ # `#enqueue`
def self.produce(consumer, logger=NullLogger.new)
producer = new(consumer, logger)
consumer_thread = Thread.new do
Thread.current.report_on_exception = false
consumer.start
end
yield producer
- producer.send_frame(Frame::EOF)
producer.flush
rescue => e
- consumer.stop if consumer
logger.warn(LOGGER_ID) { 'Exited with error' }
logger.error(LOGGER_ID) { e }
raise
ensure
+ consumer.stop
consumer_thread.join if consumer_thread
end
def initialize(consumer, logger=NullLogger.new)
@consumer = consumer