<commit_before>require 'delegate'

module Backburner
  class Connection < SimpleDelegator
    class BadURL < RuntimeError; end

    attr_accessor :url, :beanstalk

    # If a proc is provided, it will be called (and given this connection as an
    # argument) whenever the connection is reconnected.
    # @example
    #   connection.on_reconnect = lambda { |conn| puts 'reconnected!' }
    attr_accessor :on_reconnect

    # Constructs a backburner connection
    # `url` can be a string i.e '127.0.0.1:3001' or an array of
    # addresses (however, only the first element in the array will
    # be used)
    def initialize(url, &on_reconnect)
      @url = url
      @beanstalk = nil
      @on_reconnect = on_reconnect
      connect!
    end

    # Close the connection, if it exists
    def close
      @beanstalk.close if @beanstalk
      @beanstalk = nil
      __setobj__(@beanstalk)
    end

    # Determines if the connection to Beanstalk is currently open
    def connected?
      begin
        !!(@beanstalk && @beanstalk.connection && @beanstalk.connection.connection && !@beanstalk.connection.connection.closed?) # Would be nice if beaneater provided a connected? method
      rescue => e
        false
      end
    end

    # Attempt to reconnect to Beanstalk. Note: the connection will not be watching
    # or using the tubes it was before it was reconnected (as it's actually a
    # completely new connection)
    # @raise [Beaneater::NotConnected] If beanstalk fails to connect
    def reconnect!
      close
      connect!
      @on_reconnect.call(self) if @on_reconnect.respond_to?(:call)
    end

    # Yield to a block that will be retried several times if the connection to
    # beanstalk goes down and is able to be re-established.
    #<commit_msg>assigned but unused variable - e<commit_after>require 'delegate'

module Backburner
  class Connection < SimpleDelegator
    class BadURL < RuntimeError; end

    attr_accessor :url, :beanstalk

    # If a proc is provided, it will be called (and given this connection as an
    # argument) whenever the connection is reconnected.
    # @example
    #   connection.on_reconnect = lambda { |conn| puts 'reconnected!' }
    attr_accessor :on_reconnect

    # Constructs a backburner connection
    # `url` can be a string i.e '127.0.0.1:3001' or an array of
    # addresses (however, only the first element in the array will
    # be used)
    def initialize(url, &on_reconnect)
      @url = url
      @beanstalk = nil
      @on_reconnect = on_reconnect
      connect!
    end

    # Close the connection, if it exists
    def close
      @beanstalk.close if @beanstalk
      @beanstalk = nil
      __setobj__(@beanstalk)
    end

    # Determines if the connection to Beanstalk is currently open
    def connected?
      begin
        !!(@beanstalk && @beanstalk.connection && @beanstalk.connection.connection && !@beanstalk.connection.connection.closed?) # Would be nice if beaneater provided a connected? method
      rescue
        false
      end
    end

    # Attempt to reconnect to Beanstalk. Note: the connection will not be watching
    # or using the tubes it was before it was reconnected (as it's actually a
    # completely new connection)
    # @raise [Beaneater::NotConnected] If beanstalk fails to connect
    def reconnect!
      close
      connect!
      @on_reconnect.call(self) if @on_reconnect.respond_to?(:call)
    end

    # Yield to a block that will be retried several times if the connection to
    # beanstalk goes down and is able to be re-established.
    #
{'fraction_non_alphanumeric': 0.07046603032004492, 'fraction_numerical': 0.005614823133071308, 'mean_word_length': 2.8602383531960998, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4011853', 'n_tokens_mistral': 1063, 'n_tokens_neox': 998, 'n_words': 389}