<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 = {})
      @options = options
      set_up_amqp_connection
      if http_api_use_enabled?
        logger.info "HTTP API use is enabled"
        set_up_api_connection
      else
        logger.info "HTTP API use is disabled"
      end

      if tracing_enabled?
        logger.info "tracing is enabled using #{@config[:tracer]}"
      else
        logger.info "tracing is disabled"
      end

      if block_given?
        begin
          yield
        ensure
          disconnect
        end
      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!
      if @config[:uri] && !@config[:uri].empty?
        u = URI.parse(@config[:uri])

        @config[:mq_host]     = u.host
        @config[:mq_port]     = u.port
        @config[:mq_vhost]    = u.path.sub(/^\//, "")
        @config[:mq_username] = u.user
        @config[:mq_password] = <PASSWORD>
      end

      host               = @config[:mq_host]
      port               = @config[:mq_port]
      vhost              = if @config[:mq_vhost] && "" != @config[:mq_vhost]<commit_msg>If uri starts with amqps:// then enable TLS<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 = {})
      @options = options
      set_up_amqp_connection
      if http_api_use_enabled?
        logger.info "HTTP API use is enabled"
        set_up_api_connection
      else
        logger.info "HTTP API use is disabled"
      end

      if tracing_enabled?
        logger.info "tracing is enabled using #{@config[:tracer]}"
      else
        logger.info "tracing is disabled"
      end

      if block_given?
        begin
          yield
        ensure
          disconnect
        end
      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!
      if @config[:uri] && !@config[:uri].empty?
        u = URI.parse(@config[:uri])

        @config[:mq_host]     = u.host
        @config[:mq_port]     = u.port
        @config[:mq_vhost]    = u.path.sub(/^\//, "")
        @config[:mq_username] = u.user
        @config[:mq_password] = <PASSWORD>
        @config[:mq_tls]      = u.scheme == "amqps"
      end

      host               = @config[:mq_host]
      port               = @config[:mq_port]
      vhost              = if @config[:mq_vhost] && "" != @config[:mq_vhost]
{'fraction_non_alphanumeric': 0.09909264565425024, 'fraction_numerical': 0.0, 'mean_word_length': 2.244771494965143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6394915', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1229, 'n_words': 339}