<commit_before>require 'mongo'

module Qu
  module Backend
    class Mongo < Base

      # Number of times to retry connection on connection failure (default: 5)
      attr_accessor :max_retries

      # Seconds to wait before try to reconnect after connection failure (default: 1)
      attr_accessor :retry_frequency

      # Seconds to wait before looking for more jobs when the queue is empty (default: 5)
      attr_accessor :poll_frequency

      def initialize
        self.max_retries     = 5
        self.retry_frequency = 1
        self.poll_frequency  = 5
      end

      def connection
        @connection ||= begin
          host_uri = (ENV['MONGOHQ_URL'] || ENV['MONGOLAB_URI']).to_s
          if host_uri && !host_uri.empty?
            uri = URI.parse(host_uri)
            database = uri.path.empty? ? 'qu' : uri.path[1..-1]
            ::Mongo::MongoClient.from_uri(host_uri).db(database)
          else
            ::Mongo::MongoClient.new.db('qu')
          end
        end
      end
      alias_method :database, :connection

      def clear(queue = nil)
        queue ||= queues + ['failed']
        logger.info { "Clearing queues: #{queue.inspect}" }
        Array(queue).each do |q|
          logger.debug "Clearing queue #{q}"
          jobs(q).drop
          self[:queues].remove({:name => q})
        end
      end

      def queues
        self[:queues].find.map {|doc| doc['name'] }
      end

      def length(queue = 'default')
        jobs(queue).count
      end

      def enqueue(payload)<commit_msg>Make database name defaulting in mongo more robust.<commit_after>require 'mongo'

module Qu
  module Backend
    class Mongo < Base

      # Number of times to retry connection on connection failure (default: 5)
      attr_accessor :max_retries

      # Seconds to wait before try to reconnect after connection failure (default: 1)
      attr_accessor :retry_frequency

      # Seconds to wait before looking for more jobs when the queue is empty (default: 5)
      attr_accessor :poll_frequency

      def initialize
        self.max_retries     = 5
        self.retry_frequency = 1
        self.poll_frequency  = 5
      end

      def connection
        @connection ||= begin
          host_uri = (ENV['MONGOHQ_URL'] || ENV['MONGOLAB_URI']).to_s
          if host_uri && !host_uri.empty?
            uri = URI.parse(host_uri)

            # path can come in as nil, "", "/", or "/something";
            # this normalizes to empty string or "something"
            path = uri.path.to_s[1..-1].to_s
            database = path.empty? ? 'qu' : path
            uri.path = "/#{database}"
            ::Mongo::MongoClient.from_uri(host_uri).db(database)
          else
            ::Mongo::MongoClient.new.db('qu')
          end
        end
      end
      alias_method :database, :connection

      def clear(queue = nil)
        queue ||= queues + ['failed']
        logger.info { "Clearing queues: #{queue.inspect}" }
        Array(queue).each do |q|
          logger.debug "Clearing queue #{q}"
          jobs(q).drop
          self[:queues].remove({:name => q})
        end
      end

      def queues
        self[:queues].find.map {|doc| doc['name'] }
      end

      def length(queue = 'default')
        jobs(queue).count
      end

      def enqueue(payload)
{'fraction_non_alphanumeric': 0.10505600968816228, 'fraction_numerical': 0.004844081138359067, 'mean_word_length': 2.0145985401459856, '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': '5488987', 'n_tokens_mistral': 1001, 'n_tokens_neox': 965, 'n_words': 269}