Sha256: 9b574808156d76a9bf7297db853b86619338e9c4414a6161bcf9e45dfa924c6d

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require "tainbox"

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

class Rabbit::Receiving::Receive
  autoload :Job, "rabbit/receiving/job"

  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

1 entries across 1 versions & 1 rubygems

Version Path
rabbit_messaging-0.8.1 lib/rabbit/receiving/receive.rb