Class: Messed::Booter

Inherits:
Object
  • Object
show all
Includes:
Logger::LoggingModule
Defined in:
lib/messed/booter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger::LoggingModule

included, #logger, #logger=

Constructor Details

#initialize(root_directory, options = {}, &block) ⇒ Booter

Returns a new instance of Booter.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/messed/booter.rb', line 10

def initialize(root_directory, options = {}, &block)
  if block
    EMRunner.new(:detach => options[:detach], :supress_banner => options[:supress_banner]) {
      setup_booter(root_directory, options)
      record_pid(Process.pid)
      yield self
    }
  else
    setup_booter(root_directory, options)
  end
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



8
9
10
# File 'lib/messed/booter.rb', line 8

def application
  @application
end

#configurationObject (readonly)

Returns the value of attribute configuration.



8
9
10
# File 'lib/messed/booter.rb', line 8

def configuration
  @configuration
end

#environmentObject (readonly)

Returns the value of attribute environment.



8
9
10
# File 'lib/messed/booter.rb', line 8

def environment
  @environment
end

#interface_mapObject (readonly)

Returns the value of attribute interface_map.



8
9
10
# File 'lib/messed/booter.rb', line 8

def interface_map
  @interface_map
end

#pidObject (readonly)

Returns the value of attribute pid.



8
9
10
# File 'lib/messed/booter.rb', line 8

def pid
  @pid
end

#root_directoryObject (readonly)

Returns the value of attribute root_directory.



8
9
10
# File 'lib/messed/booter.rb', line 8

def root_directory
  @root_directory
end

Class Method Details

.possible_interfaces(path) ⇒ Object



80
81
82
83
# File 'lib/messed/booter.rb', line 80

def self.possible_interfaces(path)
  booter = new(path)
  booter.configuration.interfaces.names
end

Instance Method Details

#create_incoming_queueObject



105
106
107
# File 'lib/messed/booter.rb', line 105

def create_incoming_queue
  Messed::Queue::Beanstalk.new(configuration.queues.incoming.tube, configuration.queues.incoming.host, configuration.queues.incoming.port)
end

#create_outgoing_queueObject



109
110
111
# File 'lib/messed/booter.rb', line 109

def create_outgoing_queue
  Messed::Queue::Beanstalk.new(configuration.queues.outgoing.tube, configuration.queues.outgoing.host, configuration.queues.outgoing.port)
end

#environmental_configuration_fileObject



89
90
91
# File 'lib/messed/booter.rb', line 89

def environmental_configuration_file
  File.join(root_directory, "config/environments/#{environment}.rb")
end

#expand_pid_file(pid_file) ⇒ Object



40
41
42
# File 'lib/messed/booter.rb', line 40

def expand_pid_file(pid_file)
  File.expand_path(pid_file, root_directory)
end

#interface_for(name) ⇒ Object



101
102
103
# File 'lib/messed/booter.rb', line 101

def interface_for(name)
  interface_map[name]
end

#load_configurationObject



59
60
61
# File 'lib/messed/booter.rb', line 59

def load_configuration
  @configuration = Configuration.load_from_directory(@environment, File.expand_path(File.join(root_directory, 'config')))
end

#prepare_rootObject



54
55
56
57
# File 'lib/messed/booter.rb', line 54

def prepare_root
  FileUtils.mkdir_p(File.join(root_directory, 'log'))
  FileUtils.mkdir_p(File.join(root_directory, 'tmp', 'pid'))
end

#read_pid_file(pid_file) ⇒ Object



48
49
50
51
52
# File 'lib/messed/booter.rb', line 48

def read_pid_file(pid_file)
  Integer(File.read(expand_pid_file(pid_file)))
rescue ArgumentError
  nil
end

#record_pid(pid) ⇒ Object



36
37
38
# File 'lib/messed/booter.rb', line 36

def record_pid(pid)
  @pid = pid
end

#runner_fileObject



85
86
87
# File 'lib/messed/booter.rb', line 85

def runner_file
  File.join(root_directory, 'app/runner.rb')
end

#setup_applicationObject



68
69
70
71
72
73
74
# File 'lib/messed/booter.rb', line 68

def setup_application
  @application = Messed.new
  @application.booter = self
  @application.incoming = create_incoming_queue
  @application.outgoing = create_outgoing_queue
  @application.instance_eval(File.read(runner_file), runner_file)
end

#setup_booter(root_directory, options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/messed/booter.rb', line 22

def setup_booter(root_directory, options)
  $LOAD_PATH << File.expand_path(root_directory)
  
  @root_directory = root_directory
  @environment = options[:environment] || 'development'

  load_configuration
  prepare_root
  setup_logger
  setup_queues
  setup_interfaces
  setup_application
end

#setup_interfacesObject



93
94
95
96
97
98
99
# File 'lib/messed/booter.rb', line 93

def setup_interfaces
  @interface_map = {}
  configuration.interfaces.each do |name, conf|
    logger.info "Setting up interface: #{name.inspect}"
    @interface_map[name] = Interface.interface_from_configuration(self, name, conf)
  end
end

#setup_loggerObject



76
77
78
# File 'lib/messed/booter.rb', line 76

def setup_logger
  Messed::Logger.instance.setup_logger(configuration.logger || ::Logger.new(File.open(File.join(root_directory, 'log', "#{environment}.log"), File::WRONLY | File::APPEND | File::CREAT)), configuration.log_level || :debug)
end

#setup_queuesObject



63
64
65
66
# File 'lib/messed/booter.rb', line 63

def setup_queues
  @incoming_queue = create_incoming_queue
  @outgoing_queue = create_outgoing_queue
end

#write_pid_file(pid_file) ⇒ Object



44
45
46
# File 'lib/messed/booter.rb', line 44

def write_pid_file(pid_file)
  File.open(expand_pid_file(pid_file), 'w') {|f| f << pid} if pid_file
end