Class: Adhearsion::Initializer
Constant Summary
- DEFAULT_PID_FILE_NAME =
'adhearsion.pid'- InitializationFailedError =
Class.new Adhearsion::Error
Instance Attribute Summary (collapse)
-
- (Object) daemon
readonly
Returns the value of attribute daemon.
-
- (Object) path
readonly
Returns the value of attribute path.
-
- (Object) pid_file
readonly
Returns the value of attribute pid_file.
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) catch_termination_signal
- - (Object) create_pid_file
- - (Object) daemonize!
- - (Object) debugging_items
- - (Object) debugging_log
- - (Object) default_pid_path
- - (Object) init_get_logging_appenders
- - (Object) init_plugins
-
- (Initializer) initialize(options = {})
constructor
Creation of pid_files.
- - (Object) initialize_exception_logger
-
- (Object) initialize_log_paths
Creates the relative paths associated to log files i.e.
-
- (Object) join_important_threads
This method will block Thread.main() until calling join() has returned for all Threads in Adhearsion::Process.important_threads.
- - (Object) launch_console
- - (Object) load_config
-
- (Boolean) load_lib_folder
Loads files in application lib folder.
- - (Object) memoize_logging_appenders
- - (Boolean) need_console?
- - (Object) resolve_log_file_path
- - (Object) resolve_pid_file_path
- - (Object) run_plugins
- - (Object) set_ahn_proc_name
- - (Boolean) should_daemonize?
- - (Object) start
- - (Object) start_logging
- - (Object) trigger_after_initialized_hooks
- - (Object) update_rails_env_var
Constructor Details
- (Initializer) initialize(options = {})
Creation of pid_files
- You may want to have Adhearsion create a process identification
file when it boots so that a crash monitoring program such as
Monit can reboot if necessary or so the init script can kill it
for system shutdowns.
- To have Adhearsion create a pid file in the default location (i.e.
AHN_INSTALL_DIR/adhearsion.pid), supply :pid_file with 'true'. Otherwise
one is not created UNLESS it is running in daemon mode, in which
case one is created. You can force Adhearsion to not create one
even in daemon mode by supplying "false".
31 32 33 34 35 36 37 38 |
# File 'lib/adhearsion/initializer.rb', line 31 def initialize( = {}) @@started = true @path = path @mode = [:mode] @pid_file = [:pid_file].nil? ? ENV['PID_FILE'] : [:pid_file] @loaded_init_files = [:loaded_init_files] Adhearsion.ahn_root = '.' end |
Instance Attribute Details
- (Object) daemon (readonly)
Returns the value of attribute daemon
18 19 20 |
# File 'lib/adhearsion/initializer.rb', line 18 def daemon @daemon end |
- (Object) path (readonly)
Returns the value of attribute path
18 19 20 |
# File 'lib/adhearsion/initializer.rb', line 18 def path @path end |
- (Object) pid_file (readonly)
Returns the value of attribute pid_file
18 19 20 |
# File 'lib/adhearsion/initializer.rb', line 18 def pid_file @pid_file end |
Class Method Details
+ (Object) start(*args, &block)
11 12 13 |
# File 'lib/adhearsion/initializer.rb', line 11 def start(*args, &block) new(*args, &block).start end |
Instance Method Details
- (Object) catch_termination_signal
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/adhearsion/initializer.rb', line 126 def catch_termination_signal %w'INT TERM'.each do |process_signal| trap process_signal do logger.info "Received SIG#{process_signal}. Shutting down." Adhearsion::Process.shutdown end end trap 'HUP' do logger.debug "Received SIGHUP. Reopening logfiles." Adhearsion::Logging.reopen_logs end trap 'ALRM' do logger.debug "Received SIGALRM. Toggling trace logging." Adhearsion::Logging.toggle_trace! end trap 'ABRT' do logger.info "Received ABRT signal. Forcing stop." Adhearsion::Process.force_stop end end |
- (Object) create_pid_file
263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/adhearsion/initializer.rb', line 263 def create_pid_file return unless pid_file File.open pid_file, 'w' do |file| file.puts ::Process.pid end Events.register_callback :shutdown do File.delete(pid_file) if File.exists?(pid_file) end end |
- (Object) daemonize!
218 219 220 221 222 223 |
# File 'lib/adhearsion/initializer.rb', line 218 def daemonize! logger.info "Daemonizing now!" logger.debug "Creating PID file #{pid_file}" extend Adhearsion::CustomDaemonizer daemonize resolve_log_file_path end |
- (Object) debugging_items
69 70 71 72 73 74 75 76 |
# File 'lib/adhearsion/initializer.rb', line 69 def debugging_items [ "OS: #{RbConfig::CONFIG['host_os']} - RUBY: #{RUBY_ENGINE} #{RUBY_VERSION}", "Environment: #{ENV.inspect}", Adhearsion.config.description(:all), "Gem versions: #{Gem.loaded_specs.inject([]) { |c,g| c << "#{g[0]} #{g[1].version}" }}" ] end |
- (Object) debugging_log
78 79 80 81 82 |
# File 'lib/adhearsion/initializer.rb', line 78 def debugging_log debugging_items.each do |item| logger.trace item end end |
- (Object) default_pid_path
103 104 105 |
# File 'lib/adhearsion/initializer.rb', line 103 def default_pid_path File.join Adhearsion.config.root, DEFAULT_PID_FILE_NAME end |
- (Object) init_get_logging_appenders
172 173 174 |
# File 'lib/adhearsion/initializer.rb', line 172 def init_get_logging_appenders @file_loggers ||= memoize_logging_appenders end |
- (Object) init_plugins
202 203 204 |
# File 'lib/adhearsion/initializer.rb', line 202 def init_plugins Plugin.init_plugins end |
- (Object) initialize_exception_logger
257 258 259 260 261 |
# File 'lib/adhearsion/initializer.rb', line 257 def initialize_exception_logger Events.register_handler :exception do |e, l| (l || logger).error e end end |
- (Object) initialize_log_paths
Creates the relative paths associated to log files i.e.
-
log_file = "log/adhearsion.log" => creates 'log' folder
-
log_file = "log/test/adhearsion.log" => creates 'log' and 'log/test' folders
237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/adhearsion/initializer.rb', line 237 def initialize_log_paths outputters = Array(Adhearsion.config.platform.logging.outputters) outputters.select{|o| o.is_a?(String)}.each do |o| o = o.split("/") unless o[0].empty? # only if relative path o.pop # not consider filename o.inject("") do |path, folder| path = path.concat(folder).concat("/") Dir.mkdir(path) unless File.directory? path path end end end end |
- (Object) join_important_threads
This method will block Thread.main() until calling join() has returned for all Threads in Adhearsion::Process.important_threads. Note: important_threads won't always contain Thread instances. It simply requires the objects respond to join().
287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/adhearsion/initializer.rb', line 287 def join_important_threads # Note: we're using this ugly accumulator to ensure that all threads have ended since IMPORTANT_THREADS will almost # certainly change sizes after this method is called. index = 0 until index == Adhearsion::Process.important_threads.size begin Adhearsion::Process.important_threads[index].join rescue => e logger.error "Error after joining Thread #{Thread.inspect}. #{e.}" ensure index = index + 1 end end end |
- (Object) launch_console
225 226 227 228 229 230 231 |
# File 'lib/adhearsion/initializer.rb', line 225 def launch_console Adhearsion::Process.important_threads << Thread.new do catching_standard_errors do Adhearsion::Console.run end end end |
- (Object) load_config
168 169 170 |
# File 'lib/adhearsion/initializer.rb', line 168 def load_config require "#{Adhearsion.config.root}/config/adhearsion.rb" end |
- (Boolean) load_lib_folder
Loads files in application lib folder
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/adhearsion/initializer.rb', line 153 def load_lib_folder return false if Adhearsion.config.platform.lib.nil? lib_folder = [Adhearsion.config.platform.root, Adhearsion.config.platform.lib].join '/' return false unless File.directory? lib_folder Dir.chdir lib_folder do rbfiles = File.join "**", "*.rb" Dir.glob(rbfiles).each do |file| require "#{lib_folder}/#{file}" end end true end |
- (Object) memoize_logging_appenders
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/adhearsion/initializer.rb', line 176 def memoize_logging_appenders appenders = Array(Adhearsion.config.platform.logging.outputters.dup) # Any filename in the outputters array is mapped to a ::Logging::Appenders::File instance appenders.map! do |a| case a when String f = File.(Adhearsion.config.root.dup.concat("/").concat(a)) unless a.start_with?("/") ::Logging.appenders.file(f, :layout => ::Logging.layouts.pattern( :pattern => Adhearsion::Logging.adhearsion_pattern ), :auto_flushing => 2, :flush_period => 2 ) else a end end if should_daemonize? appenders else appenders += Adhearsion::Logging.default_appenders end end |
- (Boolean) need_console?
214 215 216 |
# File 'lib/adhearsion/initializer.rb', line 214 def need_console? @mode == :console end |
- (Object) resolve_log_file_path
119 120 121 122 123 124 |
# File 'lib/adhearsion/initializer.rb', line 119 def resolve_log_file_path _log_file = Adhearsion.config.platform.logging.outputters _log_file = _log_file[0] if _log_file.is_a?(Array) _log_file = File.(Adhearsion.config.root.dup.concat("/").concat(_log_file)) unless _log_file.start_with?("/") _log_file end |
- (Object) resolve_pid_file_path
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/adhearsion/initializer.rb', line 107 def resolve_pid_file_path @pid_file = if pid_file.equal?(true) default_pid_path elsif pid_file.equal?(false) nil elsif pid_file File. pid_file else should_daemonize? ? default_pid_path : nil end end |
- (Object) run_plugins
206 207 208 |
# File 'lib/adhearsion/initializer.rb', line 206 def run_plugins Plugin.run_plugins end |
- (Object) set_ahn_proc_name
275 276 277 |
# File 'lib/adhearsion/initializer.rb', line 275 def set_ahn_proc_name Adhearsion::LinuxProcName.set_proc_name Adhearsion.config.platform.process_name end |
- (Boolean) should_daemonize?
210 211 212 |
# File 'lib/adhearsion/initializer.rb', line 210 def should_daemonize? @mode == :daemon end |
- (Object) start
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/adhearsion/initializer.rb', line 40 def start resolve_pid_file_path load_lib_folder load_config initialize_log_paths daemonize! if should_daemonize? start_logging debugging_log launch_console if need_console? catch_termination_signal create_pid_file set_ahn_proc_name initialize_exception_logger update_rails_env_var init_plugins run_plugins trigger_after_initialized_hooks Adhearsion::Process.booted if Adhearsion.status == :booting logger.info "Adhearsion v#{Adhearsion::VERSION} initialized in \"#{Adhearsion.config.platform.environment}\"!" if Adhearsion.status == :running # This method will block until all important threads have finished. # When it does, the process will exit. join_important_threads self end |
- (Object) start_logging
252 253 254 255 |
# File 'lib/adhearsion/initializer.rb', line 252 def start_logging outputters = init_get_logging_appenders Adhearsion::Logging.start outputters, Adhearsion.config.platform.logging.level, Adhearsion.config.platform.logging.formatter end |
- (Object) trigger_after_initialized_hooks
279 280 281 |
# File 'lib/adhearsion/initializer.rb', line 279 def trigger_after_initialized_hooks Events.trigger_immediately :after_initialized end |
- (Object) update_rails_env_var
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/adhearsion/initializer.rb', line 84 def update_rails_env_var env = ENV['AHN_ENV'] if env && Adhearsion.config.valid_environment?(env.to_sym) unless ENV['RAILS_ENV'] logger.info "Copying AHN_ENV (#{env}) to RAILS_ENV" ENV['RAILS_ENV'] = env end else unless ENV['RAILS_ENV'] env = Adhearsion.config.platform.environment.to_s ENV['AHN_ENV'] = env logger.info "Setting RAILS_ENV to \"#{env}\"" ENV['RAILS_ENV'] = env end end logger.warn "AHN_ENV(#{ENV['AHN_ENV']}) does not match RAILS_ENV(#{ENV['RAILS_ENV']})!" unless ENV['RAILS_ENV'] == ENV['AHN_ENV'] env end |