Class Skates::Runner
In: lib/skates/runner.rb
Parent: Object

Runner is in charge of running the application.

Methods

Public Class methods

Adding a connection observer. These observer will receive on_connected and on_disconnected events.

[Source]

    # File lib/skates/runner.rb, line 83
83:     def self.add_connection_observer(observer)
84:       @@observers ||= Array.new 
85:       if observer.ancestors.include? Skates::Base::Controller
86:         Skates.logger.debug {
87:           "Added #{observer} to the list of Connection Observers"
88:         }
89:         @@observers.push(observer) unless @@observers.include? observer
90:       else
91:         Skates.logger.error {
92:           "Observer can only be Skates::Base::Controller"
93:         }
94:         false
95:       end
96:     end

Returns the list of connection observers

[Source]

    # File lib/skates/runner.rb, line 77
77:     def self.connection_observers()
78:       @@observers ||= Array.new
79:     end

Will be called by the connection class once it is connected to the server. It "plugs" the router and then calls on_connected on the various observers.

[Source]

     # File lib/skates/runner.rb, line 101
101:     def self.on_connected(connection)
102:       Skates.router.connected(connection)
103:       connection_observers.each do |observer|
104:         Skates.router.execute_route(observer, "on_connected")
105:       end
106:     end

Will be called by the connection class upon disconnection. It stops the event loop and then calls on_disconnected on the various observers.

[Source]

     # File lib/skates/runner.rb, line 111
111:     def self.on_disconnected()
112:       connection_observers.each do |conn_obs|
113:         observer = conn_obs.new
114:         observer.on_disconnected if observer.respond_to?("on_disconnected")
115:       end
116:       EventMachine.stop_event_loop
117:     end

Will be called by the connection class when it receives and parses a stanza.

[Source]

     # File lib/skates/runner.rb, line 121
121:     def self.on_stanza(stanza)
122:       begin
123:         Skates.router.route(stanza)
124:       rescue Skates::NotConnected
125:         Skates.logger.fatal {
126:           "#{$!.class} => #{$!.inspect}\n#{$!.backtrace.join("\n")}"
127:         }
128:         EventMachine::stop_event_loop
129:       rescue
130:         Skates.logger.error {
131:           "#{$!.class} => #{$!.inspect}\n#{$!.backtrace.join("\n")}"
132:         }
133:       end
134:     end

Prepares the Application to run.

[Source]

    # File lib/skates/runner.rb, line 9
 9:     def self.prepare(env)
10:       # Load the configuration
11:       config_file = File.open('config/config.yaml')
12:       Skates.config = YAML.load(config_file)[Skates.environment]
13:       
14:       # Add an outputter to the logger
15:       log_file = Log4r::RollingFileOutputter.new("#{Skates.environment}", :filename => "log/#{Skates.environment}.log", :trunc => false)
16:       case Skates.environment
17:       when "production"
18:         log_file.formatter = Log4r::PatternFormatter.new(:pattern => "%d (#{Process.pid}) [%l] :: %m", :date_pattern => "%d/%m %H:%M")      
19:       when "development"
20:         log_file.formatter = Log4r::PatternFormatter.new(:pattern => "%d (#{Process.pid}) [%l] :: %m", :date_pattern => "%d/%m %H:%M")      
21:       else
22:         log_file.formatter = Log4r::PatternFormatter.new(:pattern => "%d (#{Process.pid}) [%l] :: %m", :date_pattern => "%d/%m %H:%M")      
23:       end
24:       Skates.logger.add(log_file)
25:       
26:       # Requiring all models, stanza, controllers
27:       ['app/models/*.rb', 'app/stanzas/*.rb', 'app/controllers/*_controller.rb'].each do |dir|
28:         Runner.require_directory(dir)
29:       end
30:       
31:       # Create the router
32:       Skates.router = Skates::StanzaRouter.new
33:       
34:       # Evaluate routes defined with the new DSL router.
35:       require 'config/routes.rb'
36:             
37:       # Caching views
38:       Skates.cache_views
39:       
40:     end

Convenience method to require files in a given directory

[Source]

    # File lib/skates/runner.rb, line 44
44:     def self.require_directory(path)
45:       Dir.glob(path).each { |f| require f }
46:     end

When run is called, it loads the configuration, the routes and add them into the router It then loads the models. Finally it starts the EventMachine and connect the ComponentConnection You can pass an additional block that will be called upon launching, when the eventmachine has been started.

[Source]

    # File lib/skates/runner.rb, line 53
53:     def self.run(env)
54:       
55:       Skates.environment = env
56:       
57:       # Starting the EventMachine
58:       EventMachine.epoll
59:       EventMachine.run do
60:         
61:         Runner.prepare(env)
62:         
63:         case Skates.config["application_type"] 
64:         when "client"
65:           Skates::ClientConnection.connect(Skates.config, self) 
66:         else # By default, we assume it's a component
67:           Skates::ComponentConnection.connect(Skates.config, self) 
68:         end
69:         
70:         # And finally, let's allow the application to do all it wants to do after we started the EventMachine!
71:         yield(self) if block_given?
72:       end
73:     end

[Validate]