<commit_before>require "net/telnet"

module CartoDB
  class Varnish
    def purge(what)
      ActiveSupport::Notifications.instrument('purge.varnish', what: what) do |payload|
        send_command("purge #{what}") do |result|
          payload[:result] = result
          result =~ /200/
        end
      end
    end

    def purge_url(path)
      ActiveSupport::Notifications.instrument('purge_url.varnish', path: path) do |payload|
        send_command(Cartodb::config[:varnish_management]["purge_command"] + " " + path.gsub('\\', '\\\\\\')) do |result|
          payload[:result] = result
          result =~ /200/
        end
      end
    end

    def send_command(command)
      retries = 0
      response = nil
      conf = Cartodb::config[:varnish_management]
      begin
        retries += 1
        connection = Net::Telnet.new(
          'Host' => conf["host"],
          'Port' => conf["port"],
          'Timeout' => conf["timeout"] || 5)

        connection.cmd('String' => command, 'Match' => /\n\n/) {|r| response = r.split("\n").first.strip}
        connection.close if connection.respond_to?(:close)
      rescue Exception => e
        if retries < conf["retries"]
          retry
        else
          if Cartodb::config[:varnish_management]["critical"] == true
            raise "Varnish error while trying to connect to #{conf[:host]}:#{conf[:port]} #{e}"
          end
        end
      end
      response
    end
  end
end
<commit_msg>Fix varnish connection error message from ruby<commit_after>require "net/telnet"

module CartoDB
  class Varnish
    def purge(what)
      ActiveSupport::Notifications.instrument('purge.varnish', what: what) do |payload|
        send_command("purge #{what}") do |result|
          payload[:result] = result
          result =~ /200/
        end
      end
    end

    def purge_url(path)
      ActiveSupport::Notifications.instrument('purge_url.varnish', path: path) do |payload|
        send_command(Cartodb::config[:varnish_management]["purge_command"] + " " + path.gsub('\\', '\\\\\\')) do |result|
          payload[:result] = result
          result =~ /200/
        end
      end
    end

    def send_command(command)
      retries = 0
      response = nil
      conf = Cartodb::config[:varnish_management]
      begin
        retries += 1
        connection = Net::Telnet.new(
          'Host' => conf["host"],
          'Port' => conf["port"],
          'Timeout' => conf["timeout"] || 5)

        connection.cmd('String' => command, 'Match' => /\n\n/) {|r| response = r.split("\n").first.strip}
        connection.close if connection.respond_to?(:close)
      rescue Exception => e
        if retries < conf["retries"]
          retry
        else
          if Cartodb::config[:varnish_management]["critical"] == true
            raise "Varnish error while trying to connect to #{conf["host"]}:#{conf["port"]} #{e}"
          end
        end
      end
      response
    end
  end
end

{'fraction_non_alphanumeric': 0.14213197969543148, 'fraction_numerical': 0.006091370558375634, 'mean_word_length': 2.244785949506037, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '4992121', 'n_tokens_mistral': 917, 'n_tokens_neox': 873, 'n_words': 206}