Class: Adhearsion::Initializer

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/initializer.rb

Constant Summary collapse

InitializationFailedError =
Class.new Adhearsion::Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Initializer

Returns a new instance of Initializer


20
21
22
23
24
25
26
# File 'lib/adhearsion/initializer.rb', line 20

def initialize(options = {})
  @@started = true
  @path     = path
  @console  = options[:console]
  @loaded_init_files  = options[:loaded_init_files]
  Adhearsion.root = '.'
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path


18
19
20
# File 'lib/adhearsion/initializer.rb', line 18

def path
  @path
end

Class Method Details

.start(*args, &block) ⇒ Object


13
14
15
# File 'lib/adhearsion/initializer.rb', line 13

def start(*args, &block)
  new(*args, &block).start
end

Instance Method Details

#catch_termination_signalObject


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/adhearsion/initializer.rb', line 86

def catch_termination_signal
  self_read, self_write = IO.pipe

  %w(INT TERM HUP ALRM ABRT).each do |sig|
    trap sig do
      self_write.puts sig
    end
  end

  Thread.new do
    begin
      while readable_io = IO.select([self_read])
        signal = readable_io.first[0].gets.strip
        handle_signal signal
      end
    rescue => e
      logger.error "Crashed reading signals"
      logger.error e
      exit 1
    end
  end
end

#configure_pluginsObject


163
164
165
# File 'lib/adhearsion/initializer.rb', line 163

def configure_plugins
  Plugin.configure_plugins
end

#debugging_itemsObject


64
65
66
67
68
69
70
71
# File 'lib/adhearsion/initializer.rb', line 64

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

#debugging_logObject


73
74
75
76
77
# File 'lib/adhearsion/initializer.rb', line 73

def debugging_log
  debugging_items.each do |item|
    logger.trace item
  end
end

#handle_signal(signal) ⇒ Object


109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/adhearsion/initializer.rb', line 109

def handle_signal(signal)
  case signal
  when 'INT', 'TERM'
    logger.info "Received SIG#{signal}. Shutting down."
    Adhearsion::Process.shutdown
  when 'ALRM'
    logger.info "Received SIGALRM. Toggling trace logging."
    Adhearsion::Logging.toggle_trace!
  when 'ABRT'
    logger.info "Received ABRT signal. Forcing stop."
    Adhearsion::Process.force_stop
  end
end

#init_pluginsObject


167
168
169
# File 'lib/adhearsion/initializer.rb', line 167

def init_plugins
  Plugin.init_plugins
end

#initialize_exception_loggerObject


191
192
193
194
195
# File 'lib/adhearsion/initializer.rb', line 191

def initialize_exception_logger
  Events.register_handler :exception do |e, l|
    (l || logger).error e
  end
end

#join_important_threadsObject

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().


209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/adhearsion/initializer.rb', line 209

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.message}"
    ensure
      index = index + 1
    end
  end
end

#launch_consoleObject


179
180
181
182
183
184
185
# File 'lib/adhearsion/initializer.rb', line 179

def launch_console
  Thread.new do
    catching_standard_errors do
      Adhearsion::Console.run
    end
  end
end

#load_app_fileObject


143
144
145
146
# File 'lib/adhearsion/initializer.rb', line 143

def load_app_file
  path = "#{Adhearsion.config.root}/config/app.rb"
  load path if File.exists?(path)
end

#load_config_fileObject


148
149
150
# File 'lib/adhearsion/initializer.rb', line 148

def load_config_file
  load "#{Adhearsion.config.root}/config/adhearsion.rb"
end

#load_events_fileObject


152
153
154
155
156
# File 'lib/adhearsion/initializer.rb', line 152

def load_events_file
  Adhearsion::Events.init
  path = "#{Adhearsion.config.root}/config/events.rb"
  load path if File.exists?(path)
end

#load_lib_folderBoolean

Loads files in application lib folder

Returns:

  • (Boolean)

    if files have been loaded (lib folder is configured to not nil and actually exists)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/adhearsion/initializer.rb', line 126

def load_lib_folder
  return false if Adhearsion.config.core.lib.nil?

  lib_folder = [Adhearsion.config.core.root, Adhearsion.config.core.lib].join '/'
  return false unless File.directory? lib_folder

  $LOAD_PATH.unshift 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

#load_routes_fileObject


158
159
160
161
# File 'lib/adhearsion/initializer.rb', line 158

def load_routes_file
  path = "#{Adhearsion.config.root}/config/routes.rb"
  load path if File.exists?(path)
end

#need_console?Boolean

Returns:

  • (Boolean)

175
176
177
# File 'lib/adhearsion/initializer.rb', line 175

def need_console?
  @console == true
end

#run_pluginsObject


171
172
173
# File 'lib/adhearsion/initializer.rb', line 171

def run_plugins
  Plugin.run_plugins
end

#set_ahn_proc_nameObject


197
198
199
# File 'lib/adhearsion/initializer.rb', line 197

def set_ahn_proc_name
  Adhearsion::LinuxProcName.set_proc_name Adhearsion.config.core.process_name
end

#setup_i18n_load_pathObject


79
80
81
82
83
84
# File 'lib/adhearsion/initializer.rb', line 79

def setup_i18n_load_path
  Adhearsion.config.core.i18n.locale_path.each do |dir|
    logger.debug "Adding #{dir} to the I18n load path"
    I18n.load_path += Dir["#{dir}/**/*.yml"]
  end
end

#startObject


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/adhearsion/initializer.rb', line 28

def start
  catch :boot_aborted do
    configure_plugins
    load_lib_folder
    load_app_file
    load_config_file
    load_events_file
    load_routes_file

    Adhearsion.statistics
    start_logging
    debugging_log
    launch_console if need_console?
    catch_termination_signal
    set_ahn_proc_name
    initialize_exception_logger
    setup_i18n_load_path
    Rayo::Initializer.init
    HTTPServer.start
    init_plugins

    Rayo::Initializer.run
    run_plugins
    trigger_after_initialized_hooks

    Adhearsion::Process.booted if Adhearsion.status == :booting

    logger.info "Adhearsion v#{Adhearsion::VERSION} initialized in \"#{Adhearsion.environment}\"!" if Adhearsion.status == :running
  end

  # This method will block until all important threads have finished.
  # When it does, the process will exit.
  join_important_threads
  self
end

#start_loggingObject


187
188
189
# File 'lib/adhearsion/initializer.rb', line 187

def start_logging
  Adhearsion::Logging.start Adhearsion.config.core.logging.level, Adhearsion.config.core.logging.formatter
end

#trigger_after_initialized_hooksObject


201
202
203
# File 'lib/adhearsion/initializer.rb', line 201

def trigger_after_initialized_hooks
  Events.trigger_immediately :after_initialized
end