<commit_before>require 'exception_notification'
require 'redis'

module ExceptionNotifier
  class << self
    alias_method :original_notify_exception, :notify_exception

    def notify_exception(exception, options={})
      ex_key = exception_key(exception)
      begin
        val = redis.get(ex_key)
      rescue Redis::CannotConnectError
      end
      if val && val.to_i + 60 > Time.now.to_i
        # Write log and return.
        ExceptionNotificationModerate.logger.info(exception)
        return false
      end

      begin
        redis.set(ex_key, Time.now.to_i)
      rescue Redis::CannotConnectError
      end
      original_notify_exception(exception, options)
    end

    private

    def exception_key(exception)
      "#{exception.to_s}_#{exception.message}"
    end

    def redis
      if ExceptionNotificationModerate.redis_host
        Redis.new(host: ExceptionNotificationModerate.redis_host)
      elsif ExceptionNotificationModerate.redis_url
        Redis.new(host: ExceptionNotificationModerate.redis_url)
      end
    end
  end
end<commit_msg>Rescue redis socket error.<commit_after>require 'exception_notification'
require 'redis'

module ExceptionNotifier
  class << self
    alias_method :original_notify_exception, :notify_exception

    def notify_exception(exception, options={})
      ex_key = exception_key(exception)

      val = execute_with_rescue_error { redis.get(ex_key) }
      if val && val.to_i + 60 > Time.now.to_i
        # Write log and return.
        ExceptionNotificationModerate.logger.info(exception)
        return false
      end

      execute_with_rescue_error { redis.set(ex_key, Time.now.to_i) }
      original_notify_exception(exception, options)
    end

    private

    def exception_key(exception)
      "#{exception.to_s}_#{exception.message}"
    end

    def execute_with_rescue_error
      yield
    rescue Redis::CannotConnectError, SocketError
    end

    def redis
      if ExceptionNotificationModerate.redis_host
        Redis.new(host: ExceptionNotificationModerate.redis_host)
      elsif ExceptionNotificationModerate.redis_url
        Redis.new(host: ExceptionNotificationModerate.redis_url)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07003183265120509, 'fraction_numerical': 0.0018190086402910413, 'mean_word_length': 3.1666666666666665, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6957805', 'n_tokens_mistral': 663, 'n_tokens_neox': 642, 'n_words': 129}