Sha256: dffddc8af7f5ab20be2d33022b0dc0bebe58df0d3e95434a3289210baa2a649a

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require "tainbox"

require "rabbit"
require "rabbit/receiving/queue"
require "rabbit/receiving/job"

class Rabbit::Receiving::Receive
  include Tainbox

  attribute :message
  attribute :delivery_info
  attribute :arguments

  def call
    log!
    call_hooks(before_hooks)
    process_message
    call_hooks(after_hooks)
  end

  def log!
    Rabbit.config.receive_logger.debug(
      [message, delivery_info, arguments].join(" / "),
    )
  end

  def process_message
    job_class
      .set(queue: queue)
      .perform_later(message, message_info)
  end

  def call_hooks(hooks)
    hooks.each do |hook_proc|
      hook_proc.call(message, message_info)
    end
  end

  def before_hooks
    Rabbit.config.before_receiving_hooks || []
  end

  def after_hooks
    Rabbit.config.after_receiving_hooks || []
  end

  def message_info
    arguments.merge(
      delivery_info.slice(:exchange, :routing_key),
    )
  end

  def queue
    Rabbit::Receiving::Queue.new(message, arguments).name
  end

  def job_class
    Rabbit.config.receiving_job_class_callable&.call || Rabbit::Receiving::Job
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rabbit_messaging-0.10.0 lib/rabbit/receiving/receive.rb
rabbit_messaging-0.9.0 lib/rabbit/receiving/receive.rb