<commit_before>module Gundog
  class ApplicationWorker
    include Celluloid

    def self.disable_transaction!
      define_method :in_transaction? do
        false
      end
    end

    attr_reader :json

    def work(args = "{}", metadata, delivery_info, channel)
      log_start(args)

      begin
        parse!(args)
        ActiveRecord::Base.connection_pool.with_connection do
          setup

          if in_transaction?
            ActiveRecord::Base.transaction do
              call
            end
          else
            call
          end
        end
      rescue Exception => ex
        # 'Should all unacknowledged messages up to this be acknowledged as well?'
        # = false
        channel.acknowledge(delivery_info.delivery_tag, false)
        log_exception(ex)
        queue_name = delivery_info.routing_key + "_retry"
        if metadata[:headers]
          Gundog::Publisher.new
            .publish(args, to_queue: queue_name, headers: metadata[:headers])
        else
          Gundog::Publisher.new.publish(args, to_queue: queue_name,
                                        headers: {retry_count: 1})
        end
        self.terminate
        return
      end

      channel.acknowledge(delivery_info.delivery_tag, false)
      log_success
      self.terminate
    end


    private

    def log_exception(ex)
      puts "#{Time.zone.now.to_s}  *** EXCEPTION #{self.class.name} ***"
      puts('=' * 80)
      puts ex.message
      puts ex.backtrace
    end

    def log_start(args)
      puts "#{Time.zone.now.to_s}  *** START #{self.class.name} with #{args} ***"
    end

    def log_success
      puts "#{Time.zone.now.to_s}  *** SUCCESS #{self.class.name} ***"
    end

    def parse!(args)
      @json = ActiveSupport::JSON.decode(args)
    end

    def setup
    end

    def call
    end

    def publish_to(queue, data)
      Gundog::Publisher.new.publish(data.to_json, to_queue: queue)
    end

    def in_transaction?
      true
    end
  end
end
<commit_msg>header-handling for messages published by RabbitMQ-UI<commit_after>module Gundog
  class ApplicationWorker
    include Celluloid

    def self.disable_transaction!
      define_method :in_transaction? do
        false
      end
    end

    attr_reader :json

    def work(args = "{}", metadata, delivery_info, channel)
      log_start(args)

      begin
        parse!(args)
        ActiveRecord::Base.connection_pool.with_connection do
          setup

          if in_transaction?
            ActiveRecord::Base.transaction do
              call
            end
          else
            call
          end
        end
      rescue Exception => ex
        # 'Should all unacknowledged messages up to this be acknowledged as well?'
        # = false
        channel.acknowledge(delivery_info.delivery_tag, false)
        log_exception(ex)
        queue_name = delivery_info.routing_key + "_retry"
        # messages sent by RabbitMQ-UI will add metadata =
        # {:headers=>{}, :delivery_mode=>1}
        if metadata[:headers]&.any?
          Gundog::Publisher.new
            .publish(args, to_queue: queue_name, headers: metadata[:headers])
        else
          Gundog::Publisher.new.publish(args, to_queue: queue_name,
                                        headers: {retry_count: 1})
        end
        self.terminate
        return
      end

      channel.acknowledge(delivery_info.delivery_tag, false)
      log_success
      self.terminate
    end


    private

    def log_exception(ex)
      puts "#{Time.zone.now.to_s}  *** EXCEPTION #{self.class.name} ***"
      puts('=' * 80)
      puts ex.message
      puts ex.backtrace
    end

    def log_start(args)
      puts "#{Time.zone.now.to_s}  *** START #{self.class.name} with #{args} ***"
    end

    def log_success
      puts "#{Time.zone.now.to_s}  *** SUCCESS #{self.class.name} ***"
    end

    def parse!(args)
      @json = ActiveSupport::JSON.decode(args)
    end

    def setup
    end

    def call
    end

    def publish_to(queue, data)
      Gundog::Publisher.new.publish(data.to_json, to_queue: queue)
    end

    def in_transaction?
      true
    end
  end
end

{'fraction_non_alphanumeric': 0.0923298869920654, 'fraction_numerical': 0.001683096898292859, 'mean_word_length': 1.9545454545454546, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5694551', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1221, 'n_words': 286}