<commit_before>require 'time'

module BmcDaemonLib
  class MqConsumerError              < StandardError; end
  class MqConsumerException          < StandardError; end
  class MqConsumerTopicNotFound      < StandardError; end

  class MqConsumer < MqEndpoint
    include LoggerHelper

    def subscribe_to_queue name, context = nil
      log_debug "subscribe_to_queue [#{name}]"

      # Ensure a channel has been successfully opened first
      unless @channel
        error = "subscribe_to_queue: no active channel (call subscribe_to_queue beforehand)"
        log_error error
        raise MqConsumerException, error
      end

      # Queue for this rule
      @queue = @channel.queue(name, auto_delete: false, durable: true)

      # Create consumer on this queue
      @queue.subscribe(manual_ack: AMQP_MANUAL_ACK, on_cancellation: :consumer_cancelled) do |delivery_info, metadata, payload|
        handle_receive context, delivery_info, metadata, payload
      end
    end

    def listen_to topic, rkey
      log_info "listen_to [#{topic}] [#{rkey}] > [#{@queue.name}]"

      # Ensure a queue has been successfully subscribed first
      unless @queue
        error = "listen_to: no active queue (call subscribe_to_queue beforehand)"
        log_error error
        raise MqConsumerException, error
      end

      # Bind on topic exchange, if exists
      exchange = @channel.topic(topic, durable: true)

      # puts "listen_to(#{topic},#{rkey})"
      @queue.bind topic, routing_key: rkey

      # Handle errors
      rescue Bunny::NotFound => e
        log_debug "missing exchange [#{topic}]"
        #@channel.topic topic, durable: false, auto_delete: true
        raise MqConsumerTopicNotFound, e.message

      rescue StandardError => e
        log_error "UNEXPECTED: #{e.inspect}"
        raise MqConsumerException, e.message

      else<commit_msg>consumer: don't try to create exchange if missing (revert behaviour change)<commit_after>require 'time'

module BmcDaemonLib
  class MqConsumerError              < StandardError; end
  class MqConsumerException          < StandardError; end
  class MqConsumerTopicNotFound      < StandardError; end

  class MqConsumer < MqEndpoint
    include LoggerHelper

    def subscribe_to_queue name, context = nil
      log_debug "subscribe_to_queue [#{name}]"

      # Ensure a channel has been successfully opened first
      unless @channel
        error = "subscribe_to_queue: no active channel (call subscribe_to_queue beforehand)"
        log_error error
        raise MqConsumerException, error
      end

      # Queue for this rule
      @queue = @channel.queue(name, auto_delete: false, durable: true)

      # Create consumer on this queue
      @queue.subscribe(manual_ack: AMQP_MANUAL_ACK, on_cancellation: :consumer_cancelled) do |delivery_info, metadata, payload|
        handle_receive context, delivery_info, metadata, payload
      end
    end

    def listen_to topic, rkey
      log_info "listen_to [#{topic}] [#{rkey}] > [#{@queue.name}]"

      # Ensure a queue has been successfully subscribed first
      unless @queue
        error = "listen_to: no active queue (call subscribe_to_queue beforehand)"
        log_error error
        raise MqConsumerException, error
      end

      # Bind on topic exchange, if exists
      @queue.bind topic, routing_key: rkey

      # Handle errors
      rescue Bunny::NotFound => e
        log_debug "missing exchange [#{topic}]"
        #<EMAIL>.topic topic, durable: false, auto_delete: true
        raise MqConsumerTopicNotFound, e.message

      rescue StandardError => e
        log_error "UNEXPECTED: #{e.inspect}"
        raise MqConsumerException, e.message

      else
{'fraction_non_alphanumeric': 0.07415426251691475, 'fraction_numerical': 0.0, 'mean_word_length': 2.9529411764705884, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 10, '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': '2742860', 'n_tokens_mistral': 1042, 'n_tokens_neox': 1006, 'n_words': 323}