<commit_before>require 'bunny'
require 'carrot-top'
require 'securerandom'
require 'hutch/logging'
require 'hutch/exceptions'

module Hutch
  class Broker
    include Logging

    attr_accessor :connection, :channel, :exchange, :api_client

    def initialize(config = nil)
      @config = config || Hutch::Config
    end

    def connect(options = {})
      set_up_amqp_connection
      set_up_api_connection if options.fetch(:enable_http_api_use, true)

      if block_given?
        yield
        disconnect
      end
    end

    def disconnect
      @channel.close    if @channel
      @connection.close if @connection
      @channel, @connection, @exchange, @api_client = nil, nil, nil, nil
    end

    # Connect to RabbitMQ via AMQP. This sets up the main connection and
    # channel we use for talking to RabbitMQ. It also ensures the existance of
    # the exchange we'll be using.
    def set_up_amqp_connection
      open_connection!
      open_channel!

      exchange_name = @config[:mq_exchange]
      logger.info "using topic exchange '#{exchange_name}'"

      with_bunny_precondition_handler('exchange') do
        @exchange = @channel.topic(exchange_name, durable: true)
      end
    end

    def open_connection!
      host     = @config[:mq_host]
      port     = @config[:mq_port]
      vhost    = @config[:mq_vhost]
      username = @config[:mq_username]
      password = @config[:mq_password]
      tls      = @config[:mq_tls]
      tls_key  = @config[:mq_tls_cert]
      tls_cert = @config[:mq_tls_key]
      protocol = tls ? "amqps://" : "amqp://"
      sanitized_uri = "#{protocol}#{username}@#{host}:#{port}/#{vhost.sub(/^\//, '')}"
      logger.info "connecting to rabbitmq (#{sanitized_uri})"<commit_msg>fixed the swapping of tls_key and tls_cert<commit_after>require 'bunny'
require 'carrot-top'
require 'securerandom'
require 'hutch/logging'
require 'hutch/exceptions'

module Hutch
  class Broker
    include Logging

    attr_accessor :connection, :channel, :exchange, :api_client

    def initialize(config = nil)
      @config = config || Hutch::Config
    end

    def connect(options = {})
      set_up_amqp_connection
      set_up_api_connection if options.fetch(:enable_http_api_use, true)

      if block_given?
        yield
        disconnect
      end
    end

    def disconnect
      @channel.close    if @channel
      @connection.close if @connection
      @channel, @connection, @exchange, @api_client = nil, nil, nil, nil
    end

    # Connect to RabbitMQ via AMQP. This sets up the main connection and
    # channel we use for talking to RabbitMQ. It also ensures the existance of
    # the exchange we'll be using.
    def set_up_amqp_connection
      open_connection!
      open_channel!

      exchange_name = @config[:mq_exchange]
      logger.info "using topic exchange '#{exchange_name}'"

      with_bunny_precondition_handler('exchange') do
        @exchange = @channel.topic(exchange_name, durable: true)
      end
    end

    def open_connection!
      host     = @config[:mq_host]
      port     = @config[:mq_port]
      vhost    = @config[:mq_vhost]
      username = @config[:mq_username]
      password = @config[:mq_password]
      tls      = @config[:mq_tls]
      tls_key  = @config[:mq_tls_key]
      tls_cert = @config[:mq_tls_cert]
      protocol = tls ? "amqps://" : "amqp://"
      sanitized_uri = "#{protocol}#{username}@#{host}:#{port}/#{vhost.sub(/^\//, '')}"
      logger.info "connecting to rabbitmq (#{sanitized_uri})"
{'fraction_non_alphanumeric': 0.1073978863181948, 'fraction_numerical': 0.0, 'mean_word_length': 2.921612541993281, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6394914', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1087, 'n_words': 277}