class Aesop::Aesop

Public Instance Methods

catch_exception(exception) click to toggle source
# File lib/aesop/aesop.rb, line 29
def catch_exception(exception)
  log_exception(exception)
  store_exception_occurrence(exception)
  if should_dispatch?(exception)
    dispatch_exception(exception)
  end
end
catch_exceptions( exceptions ) click to toggle source
# File lib/aesop/aesop.rb, line 21
def catch_exceptions( exceptions )
  if exceptions.is_a?(Array)
    exceptions.each{ |e| catch_exception(e) }
  else
    raise IllegalArgumentException.new("#catch_exceptions should be called with an Array as argument, maybe use #catch_exception instead?")
  end
end
dispatch_exception(exception) click to toggle source
# File lib/aesop/aesop.rb, line 68
def dispatch_exception(exception)
  record_exception_dispatch(exception)
  Aesop::Dispatcher.instance.dispatch_exception(exception)
end
exception_already_dispatched?(exception) click to toggle source
# File lib/aesop/aesop.rb, line 73
def exception_already_dispatched?(exception)
  res = !redis.get( "#{exception_prefix}:#{exception.class.to_s}:dispatched" ).nil?
  Aesop::Logger.debug("#{exception.class.to_s} has #{res ? "already" : "not yet"} been dispatched")
  res
end
exception_count_threshold(exception) click to toggle source
# File lib/aesop/aesop.rb, line 60
def exception_count_threshold(exception)
  configuration.exception_count_threshold
end
exception_prefix() click to toggle source
# File lib/aesop/aesop.rb, line 85
def exception_prefix
  configuration.exception_prefix
end
exception_time_threshold( exception ) click to toggle source
# File lib/aesop/aesop.rb, line 109
def exception_time_threshold( exception )
  configuration.exception_time_threshold
end
init() click to toggle source
# File lib/aesop/aesop.rb, line 6
def init
  load_configuration
  Aesop::Bootloader.new.boot
end
internal_exception?(exception) click to toggle source
# File lib/aesop/aesop.rb, line 53
def internal_exception?(exception)
  parts = exception.class.name.split("::")
  res = (parts.size > 1 && parts.first == "Aesop")
  Aesop::Logger.debug("#{exception.class.to_s} is #{res ? "": "not "}an internal exception and will #{res ? "" : "not "}be dispatched right away")
  res
end
is_excluded?( exception ) click to toggle source
# File lib/aesop/aesop.rb, line 99
def is_excluded?( exception )
  res = if (exceptions = configuration.excluded_exceptions)
    exceptions.include?( exception.class )
  else
    false
  end
  Aesop::Logger.debug( "#{exception.class.to_s} is#{res ? " " : " not "}excluded")
  res
end
load_configuration() click to toggle source
# File lib/aesop/aesop.rb, line 11
def load_configuration
  config_file = if File.exist?("config/aesop.rb")
    File.expand_path("config/aesop.rb")
  else
    File.expand_path(File.join( File.dirname(__FILE__), '..', '..', 'config', 'init.rb'))
  end
  load config_file
  Aesop::Logger.debug("Loaded config in #{config_file}")
end
log_exception(exception) click to toggle source
# File lib/aesop/aesop.rb, line 37
def log_exception(exception)
  Aesop::Logger.debug(exception.message)
  if trace = exception.backtrace
    trace.each do |line|
      Aesop::Logger.debug(line)
    end
  end
end
record_exception_dispatch(exception) click to toggle source
# File lib/aesop/aesop.rb, line 64
def record_exception_dispatch(exception)
  redis.set( "#{exception_prefix}:#{exception.class.to_s}:dispatched", Time.now.to_i )
end
redis() click to toggle source
# File lib/aesop/aesop.rb, line 141
def redis
  if @redis.nil? || (@redis.client && !@redis.client.connected?)
    begin
      @redis = Redis.new(redis_options)
      @redis.select( configuration.redis.database )
    rescue => e
      raise RedisConnectionException.new( e )
    end
  end
  @redis
end
redis_host_or_socket_options() click to toggle source
# File lib/aesop/aesop.rb, line 130
def redis_host_or_socket_options
  if configuration.redis.path
    { path: configuration.redis.path }
  else
    {
      host: configuration.redis.host,
      port: configuration.redis.port,
    }
  end
end
redis_options() click to toggle source
# File lib/aesop/aesop.rb, line 122
def redis_options
  options = redis_host_or_socket_options
  if (password = configuration.redis.password) && !password.empty?
    options.merge!(:password => password)
  end
  options
end
retrieve_deployment_time() click to toggle source
# File lib/aesop/aesop.rb, line 113
def retrieve_deployment_time
  timestamp = redis.get( configuration.deployment_key ).to_i
  Time.at(timestamp)
end
retrieve_exception_count(exception) click to toggle source
# File lib/aesop/aesop.rb, line 79
def retrieve_exception_count(exception)
  res = redis.get( "#{exception_prefix}:#{exception.class.to_s}:count" ).to_i
  Aesop::Logger.debug("This is occurrence number #{res} of #{exception.class.to_s}")
  res
end
should_dispatch?(exception) click to toggle source
# File lib/aesop/aesop.rb, line 46
def should_dispatch?(exception)
  return false if is_excluded?(exception) || exception_already_dispatched?(exception)
  return true if internal_exception?(exception)
  current_amount = retrieve_exception_count(exception)
  within_window?(exception) && (current_amount >= exception_count_threshold(exception))
end
store_exception_occurrence(exception) click to toggle source
# File lib/aesop/aesop.rb, line 118
def store_exception_occurrence(exception)
  redis.incr( "#{exception_prefix}:#{exception.class.to_s}:count" )
end
within_window?( exception ) click to toggle source
# File lib/aesop/aesop.rb, line 89
def within_window?( exception )
  res = if deployed_time = retrieve_deployment_time
    (Time.now - deployed_time) < exception_time_threshold(exception)
  else
    false
  end
  Aesop::Logger.debug("#{exception.class.to_s} is#{res ? " " : " not "}within the window")
  res
end