Class: Backdat::Application
- Inherits:
-
Object
- Object
- Backdat::Application
- Includes:
- Mixlib::CLI
- Defined in:
- lib/backdat/application.rb
Overview
The backdat application class for both server and worker.
Direct Known Subclasses
Defined Under Namespace
Class Method Summary collapse
-
.debug_stacktrace(e) ⇒ Object
Present a debug stracktrace upon an error.
-
.exit!(msg, err = -1)) ⇒ Object
Log a fatal error message to both STDERR and the Logger, exit the application with a debug message.
-
.fatal!(msg, err = -1)) ⇒ Object
Log a fatal error message to both STDERR and the Logger, exit the application with a fatal message.
Instance Method Summary collapse
-
#configure_backdat ⇒ Object
Configure the application throwing a warning when there is no config file.
-
#configure_logging ⇒ Object
Configures the logging in a relatively sane fashion.
-
#initialize ⇒ Application
constructor
Initialize the application, setting up default handlers.
-
#reconfigure ⇒ Object
Reconfigure the application and logging.
-
#run ⇒ Object
Run the application itself.
-
#run_application ⇒ Object
Placeholder for run_application, intended to be overridden.
-
#setup_application ⇒ Object
Placeholder for setup_application, intended to be overridden.
Constructor Details
#initialize ⇒ Application
Initialize the application, setting up default handlers.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/backdat/application.rb', line 11 def initialize super trap("TERM") do Backdat::Application.fatal!("SIGTERM received, stopping", 1) end trap("INT") do Backdat::Application.fatal!("SIGINT received, stopping", 2) end trap("QUIT") do Backdat::Log.info("SIGQUIT received, call stack:\n ", caller.join("\n ")) end trap("HUP") do Backdat::Log.info("SIGHUP received, reconfiguring") reconfigure end end |
Class Method Details
.debug_stacktrace(e) ⇒ Object
Present a debug stracktrace upon an error. Gives a readable backtrace with a timestamp.
106 107 108 109 110 111 112 |
# File 'lib/backdat/application.rb', line 106 def debug_stacktrace(e) = "#{e.class}: #{e}\n#{e.backtrace.join("\n")}" stacktrace_out = "Generated at #{Time.now.to_s}\n" stacktrace_out += Backdat::Log.debug() end |
.exit!(msg, err = -1)) ⇒ Object
Log a fatal error message to both STDERR and the Logger, exit the application with a debug message.
129 130 131 132 |
# File 'lib/backdat/application.rb', line 129 def exit!(msg, err = -1) Backdat::Log.debug(msg) Process.exit err end |
.fatal!(msg, err = -1)) ⇒ Object
Log a fatal error message to both STDERR and the Logger, exit the application with a fatal message.
119 120 121 122 |
# File 'lib/backdat/application.rb', line 119 def fatal!(msg, err = -1) Backdat::Log.fatal(msg) Process.exit err end |
Instance Method Details
#configure_backdat ⇒ Object
Configure the application throwing a warning when there is no config file.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/backdat/application.rb', line 39 def configure_backdat 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." Backdat::Log.warn "*****************************************" Backdat::Log.warn msg Backdat::Log.warn "*****************************************" end end |
#configure_logging ⇒ Object
Configures the logging in a relatively sane fashion. Only prints to STDOUT given a valid tty. Does not write to STDOUT when daemonizing.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/backdat/application.rb', line 56 def configure_logging Backdat::Log.init(Backdat::Config[:log_location]) if ( Backdat::Config[:log_location] != STDOUT ) && STDOUT.tty? && ( !Backdat::Config[:daemonize] ) stdout_loger = Logger.new(STDOUT) STDOUT.sync = true stdout_logger = Backdat::Log.logger.formatter Backdat::Log.loggers << stdout_logger end Backdat::Log.level = Backdat::Config[:log_level] end |
#reconfigure ⇒ Object
Reconfigure the application and logging.
33 34 35 36 |
# File 'lib/backdat/application.rb', line 33 def reconfigure configure_backdat configure_logging end |
#run ⇒ Object
Run the application itself. Configure, setup, and then run.
69 70 71 72 73 |
# File 'lib/backdat/application.rb', line 69 def run reconfigure setup_application run_application end |
#run_application ⇒ Object
Placeholder for run_application, intended to be overridden.
86 87 88 89 |
# File 'lib/backdat/application.rb', line 86 def run_application error_msg = "#{self.to_s}: you must override run_application" raise Backdat::Exceptions::Application, error_msg end |
#setup_application ⇒ Object
Placeholder for setup_application, intended to be overridden.
78 79 80 81 |
# File 'lib/backdat/application.rb', line 78 def setup_application error_msg = "#{self.to_s}: you must override setup_application" raise Backdat::Exceptions::Application, error_msg end |