<commit_before>class QueuePublisher
  def initialize(options = {})
    @noop = options[:noop]
    @options = options

    setup_exchange unless @noop
  end

  attr_reader :exchange, :channel

  def send_message(item)
    return if @noop
    routing_key = "#{item.format}.#{item.update_type}"
    exchange.publish(
      item_json(item),
      routing_key: routing_key,
      content_type: 'application/json',
      persistent: true
    )
    success = channel.wait_for_confirms
    if !success
      Airbrake.notify_or_ignore(
        Exception.new("Publishing message failed"),
        parameters: {
          routing_key: routing_key,
          message_body: hash,
        },
      )
    end
  end

  private

  def setup_exchange
    connection = Bunny.new(@options)
    connection.start
    @channel = connection.create_channel
    channel.confirm_select

    # passive parameter ensures we don't create the exchange.
    @exchange = channel.topic(@options.fetch(:exchange), passive: true)
  end

  def item_json(item)
    PrivateContentItemPresenter.new(item).to_json
  end
end
<commit_msg>Use a custom exception for publish failure<commit_after>class QueuePublisher
  def initialize(options = {})
    @noop = options[:noop]
    @options = options

    setup_exchange unless @noop
  end

  attr_reader :exchange, :channel

  class PublishFailedError < StandardError
  end

  def send_message(item)
    return if @noop
    routing_key = "#{item.format}.#{item.update_type}"
    exchange.publish(
      item_json(item),
      routing_key: routing_key,
      content_type: 'application/json',
      persistent: true
    )
    success = channel.wait_for_confirms
    if !success
      Airbrake.notify_or_ignore(
        PublishFailedError.new("Publishing message failed"),
        parameters: {
          routing_key: routing_key,
          message_body: hash,
        },
      )
    end
  end

  private

  def setup_exchange
    connection = Bunny.new(@options)
    connection.start
    @channel = connection.create_channel

    # Enable publisher confirms, so we get acks back after publishes.
    channel.confirm_select

    # passive parameter ensures we don't create the exchange.
    @exchange = channel.topic(@options.fetch(:exchange), passive: true)
  end

  def item_json(item)
    PrivateContentItemPresenter.new(item).to_json
  end
end

{'fraction_non_alphanumeric': 0.08599404001702853, 'fraction_numerical': 0.0, 'mean_word_length': 2.983050847457627, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2061316', 'n_tokens_mistral': 725, 'n_tokens_neox': 681, 'n_words': 164}