class Pheromone::Messaging::MessageDispatcher

Public Class Methods

new(message_parameters:, dispatch_method:) click to toggle source
# File lib/pheromone/messaging/message_dispatcher.rb, line 14
def initialize(message_parameters:, dispatch_method:)
  @message_parameters = message_parameters
  @dispatch_method = dispatch_method
end

Public Instance Methods

dispatch() click to toggle source
# File lib/pheromone/messaging/message_dispatcher.rb, line 19
def dispatch
  return unless Pheromone.enabled?
  if @dispatch_method == :sync
    Message.new(
      message_body.merge!(
        encoder: @message_parameters[:encoder],
        message_format: @message_parameters[:message_format]
      )
    ).send!
  elsif @dispatch_method == :async
    send_message_asynchronously
  end
end

Private Instance Methods

background_processor() click to toggle source
# File lib/pheromone/messaging/message_dispatcher.rb, line 60
def background_processor
  Pheromone.config.background_processor
end
background_processor_klass() click to toggle source
# File lib/pheromone/messaging/message_dispatcher.rb, line 64
def background_processor_klass
  @klass ||= background_processor.klass.constantize
end
message_body() click to toggle source
# File lib/pheromone/messaging/message_dispatcher.rb, line 50
def message_body
  {
    topic: @message_parameters[:topic],
    blob: @message_parameters[:blob],
    metadata: @message_parameters[:metadata],
    options: @message_parameters[:producer_options] || {},
    embed_blob: @message_parameters[:embed_blob]
  }
end
send_message_asynchronously() click to toggle source

Allows sending messages via resque or sidekiq. WaterDrop::Message object is passed and calling `send!` on the object will trigger producing messages to Kafka

# File lib/pheromone/messaging/message_dispatcher.rb, line 38
def send_message_asynchronously
  if background_processor.name == :resque
    Resque.enqueue(background_processor_klass, message_body)
  elsif background_processor.name == :sidekiq
    background_processor_klass.perform_async(message_body)
  elsif background_processor.name == :custom
    background_processor.custom_processor.call(background_processor_klass, message_body)
  else
    raise NoSpecifiedProcessor
  end
end