class Souffle::Application

The souffle application class for both server and worker.

Attributes

commands[RW]

The commands that were left unparsed from parse_options.

Public Class Methods

new() click to toggle source

Initialize the application, setting up default handlers.

Calls superclass method
# File lib/souffle/application.rb, line 14
def initialize
  @commands = []
  super

  trap("TERM") do
    Souffle::Application.fatal!("SIGTERM received, stopping", 1)
  end

  trap("INT") do
    Souffle::Application.fatal!("SIGINT received, stopping", 2)
  end

  trap("QUIT") do
    Souffle::Log.info("SIGQUIT received, call stack:\n ", caller.join("\n "))
  end

  trap("HUP") do
    Souffle::Log.info("SIGHUP received, reconfiguring")
    reconfigure
  end
end

Private Class Methods

debug_stacktrace(e) click to toggle source

Present a debug stracktrace upon an error. Gives a readable backtrace with a timestamp.

@param [ Exception ] e The raised exception.

# File lib/souffle/application.rb, line 110
def debug_stacktrace(e)
  message = "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
  stacktrace_out = "Generated at #{Time.now.to_s}\n"
  stacktrace_out += message

  Souffle::Log.debug(message)
end
exit!(msg, err = -1) click to toggle source

Log a fatal error message to both STDERR and the Logger, exit the application with a debug message.

@param [ String ] msg The message to log. @param [ Fixnum ] err The exit level.

# File lib/souffle/application.rb, line 133
def exit!(msg, err = -1)
  Souffle::Log.debug(msg)
  Process.exit err
end
fatal!(msg, err = -1) click to toggle source

Log a fatal error message to both STDERR and the Logger, exit the application with a fatal message.

@param [ String ] msg The message to log. @param [ Fixnum ] err The exit level.

# File lib/souffle/application.rb, line 123
def fatal!(msg, err = -1)
  Souffle::Log.fatal(msg)
  Process.exit err
end

Public Instance Methods

configure_logging() click to toggle source

Configures the logging in a relatively sane fashion. Only prints to STDOUT given a valid tty. Does not write to STDOUT when daemonizing.

# File lib/souffle/application.rb, line 60
def configure_logging
  Souffle::Log.init(Souffle::Config[:log_location])
  if ( Souffle::Config[:log_location] != STDOUT ) && STDOUT.tty? &&
    ( !Souffle::Config[:daemonize] )
    stdout_logger = Logger.new(STDOUT)
    STDOUT.sync = true
    stdout_logger.formatter = Souffle::Log.logger.formatter
    Souffle::Log.loggers << stdout_logger
  end
  Souffle::Log.level = Souffle::Config[:log_level]
end
configure_souffle() click to toggle source

Configure the application throwing a warning when there is no config file.

# File lib/souffle/application.rb, line 43
def configure_souffle
  parse_options

  begin
    ::File.open(config[:config_file]) { |f| apply_config(f.path) }
  rescue Errno::ENOENT => error
    msg =  "Did not find the config file: #{config[:config_file]}"
    msg << ", Using command line options."
    Souffle::Log.warn "*****************************************"
    Souffle::Log.warn msg
    Souffle::Log.warn "*****************************************"
  end
end
reconfigure() click to toggle source

Reconfigure the application and logging.

# File lib/souffle/application.rb, line 37
def reconfigure
  configure_souffle
  configure_logging
end
run() click to toggle source

Run the application itself. Configure, setup, and then run.

# File lib/souffle/application.rb, line 73
def run
  reconfigure
  setup_application
  run_application
end
run_application() click to toggle source

Placeholder for run_application, intended to be overridden.

@raise Souffle::Exceptions::Application Must be overridden.

# File lib/souffle/application.rb, line 90
def run_application
  error_msg = "#{self.to_s}: you must override run_application"
  raise Souffle::Exceptions::Application, error_msg
end
setup_application() click to toggle source

Placeholder for setup_application, intended to be overridden.

@raise Souffle::Exceptions::Application Must be overridden.

# File lib/souffle/application.rb, line 82
def setup_application
  error_msg = "#{self.to_s}: you must override setup_application"
  raise Souffle::Exceptions::Application, error_msg
end

Private Instance Methods

apply_config(config_file_path) click to toggle source

Apply the configuration given a file path.

@param [ String ] config_file_path The path to the configuration file.

# File lib/souffle/application.rb, line 100
def apply_config(config_file_path)
  Souffle::Config.from_file(config_file_path)
  Souffle::Config.merge!(config)
end