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



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

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
122
123
124
# 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
  when 'USR2'
    logger.info "Received USR2 signal. Printing Celluloid stack dump."
    Celluloid.stack_dump(STDOUT)
  end
end

#init_pluginsObject



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

def init_plugins
  Plugin.init_plugins
end

#initialize_exception_loggerObject



194
195
196
197
198
# File 'lib/adhearsion/initializer.rb', line 194

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/adhearsion/initializer.rb', line 212

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



182
183
184
185
186
187
188
# File 'lib/adhearsion/initializer.rb', line 182

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

#load_app_fileObject



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

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

#load_config_fileObject



151
152
153
# File 'lib/adhearsion/initializer.rb', line 151

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

#load_events_fileObject



155
156
157
158
159
# File 'lib/adhearsion/initializer.rb', line 155

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)



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

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



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

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

#need_console?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/adhearsion/initializer.rb', line 178

def need_console?
  @console == true
end

#run_pluginsObject



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

def run_plugins
  Plugin.run_plugins
end

#set_ahn_proc_nameObject



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

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



190
191
192
# File 'lib/adhearsion/initializer.rb', line 190

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

#trigger_after_initialized_hooksObject



204
205
206
# File 'lib/adhearsion/initializer.rb', line 204

def trigger_after_initialized_hooks
  Events.trigger_immediately :after_initialized
end