Module: DeliveryBoy

Defined in:
lib/delivery_boy.rb,
lib/delivery_boy/fake.rb,
lib/delivery_boy/config.rb,
lib/delivery_boy/railtie.rb,
lib/delivery_boy/version.rb,
lib/delivery_boy/instance.rb,
lib/delivery_boy/config_error.rb,
lib/generators/delivery_boy/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: Config, Fake, Instance, Railtie

Constant Summary collapse

VERSION =
"1.0.1"
ConfigError =
Class.new(StandardError)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerLogger

The logger used by DeliveryBoy.

Returns:

  • (Logger)


100
101
102
103
104
105
106
# File 'lib/delivery_boy.rb', line 100

def logger
  @logger ||= Logger.new($stdout).tap do |logger|
    if config.log_level
      logger.level = Object.const_get("Logger::#{config.log_level.upcase}")
    end
  end
end

Class Method Details

.clear_bufferObject

Clear any buffered messages generated by produce or produce! methods.



84
85
86
# File 'lib/delivery_boy.rb', line 84

def clear_buffer
  instance.clear_buffer
end

.configDeliveryBoy::Config

The configuration used by DeliveryBoy.

Returns:



113
114
115
116
117
# File 'lib/delivery_boy.rb', line 113

def config
  @config ||= DeliveryBoy::Config.new(env: ENV)
rescue KingKonf::ConfigError => e
  raise ConfigError, e.message
end

.configure {|DeliveryBoy::Config| ... } ⇒ nil

Configure DeliveryBoy in a block.

DeliveryBoy.configure do |config|
  config.client_id = "yolo"
end

Yields:

Returns:

  • (nil)


127
128
129
# File 'lib/delivery_boy.rb', line 127

def configure
  yield config
end

.deliver(value, topic:, **options) ⇒ nil

Write a message to a specified Kafka topic synchronously.

Keep in mind that the client will block until the message has been delivered.

Parameters:

  • value (String)

    the message value.

  • topic (String)

    the topic that the message should be written to.

  • key (String, nil)

    the message key.

  • partition (Integer, nil)

    the topic partition that the message should be written to.

  • partition_key (String, nil)

    a key used to deterministically assign a partition to the message.

Returns:

  • (nil)

Raises:

  • (Kafka::BufferOverflow)

    if the producer’s buffer is full.

  • (Kafka::DeliveryFailed)

    if delivery failed for some reason.



27
28
29
# File 'lib/delivery_boy.rb', line 27

def deliver(value, topic:, **options)
  instance.deliver(value, topic: topic, **options)
end

.deliver_async(value, topic:, **options) ⇒ nil

Like deliver_async!, but handles Kafka::BufferOverflow errors by logging them and just going on with normal business.

Returns:

  • (nil)


35
36
37
38
39
# File 'lib/delivery_boy.rb', line 35

def deliver_async(value, topic:, **options)
  deliver_async!(value, topic: topic, **options)
rescue Kafka::BufferOverflow
  logger.error "Message for `#{topic}` dropped due to buffer overflow"
end

.deliver_async!(value, topic:, **options) ⇒ nil

Like deliver, but returns immediately.

The actual delivery takes place in a background thread.

Returns:

  • (nil)


46
47
48
# File 'lib/delivery_boy.rb', line 46

def deliver_async!(value, topic:, **options)
  instance.deliver_async!(value, topic: topic, **options)
end

.deliver_messagesnil

Delivers the items currently in the producer buffer.

Returns:

  • (nil)

Raises:

  • (Kafka::DeliveryFailed)

    if delivery failed for some reason.



79
80
81
# File 'lib/delivery_boy.rb', line 79

def deliver_messages
  instance.deliver_messages
end

.produce(value, topic:, **options) ⇒ nil

Like produce!, but handles Kafka::BufferOverflow errors by logging them and just going on with normal business.

Returns:

  • (nil)


54
55
56
57
58
# File 'lib/delivery_boy.rb', line 54

def produce(value, topic:, **options)
  produce!(value, topic: topic, **options)
rescue Kafka::BufferOverflow
  logger.error "Message for `#{topic}` dropped due to buffer overflow"
end

.produce!(value, topic:, **options) ⇒ nil

Appends the given message to the producer buffer but does not send it until deliver_messages is called.

Parameters:

  • value (String)

    the message value.

  • topic (String)

    the topic that the message should be written to.

  • key (String, nil)

    the message key.

  • partition (Integer, nil)

    the topic partition that the message should be written to.

  • partition_key (String, nil)

    a key used to deterministically assign a partition to the message.

Returns:

  • (nil)

Raises:

  • (Kafka::BufferOverflow)

    if the producer’s buffer is full.



71
72
73
# File 'lib/delivery_boy.rb', line 71

def produce!(value, topic:, **options)
  instance.produce(value, topic: topic, **options)
end

.shutdownnil

Shut down DeliveryBoy.

Automatically called when the process exits.

Returns:

  • (nil)


93
94
95
# File 'lib/delivery_boy.rb', line 93

def shutdown
  instance.shutdown
end

.test_mode!Object



131
132
133
# File 'lib/delivery_boy.rb', line 131

def test_mode!
  @instance = testing
end

.testingObject



135
136
137
# File 'lib/delivery_boy.rb', line 135

def testing
  @testing ||= Fake.new
end