Class: N::Runner

Inherits:
Object show all
Defined in:
lib/nitro/runner.rb

Overview

The Runner is a helper class that encapsulates a web application and is responsible for launching the application in differnt modes.

The runner provides default parsing of command line and environment parameters.

The default execution modes are:

:debug, :stage, :live

You can implement your own, custom version of the Runner to run your custom web applications.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionObject

:start, :stop, :restart



51
52
53
# File 'lib/nitro/runner.rb', line 51

def action
  @action
end

#modeObject

Execution mode = (:debug, :stage, :live)

:debug

useful when debugging, extra debug information is emmited, actions, templates and shaders are reloaded, etc. The execution speed of the application is impaired.

:stage

test the application with live parameters (typically on a staging server).

:live

use the parameters for the live (production) server. Optimized for speed.

The default mode is :debug



47
48
49
# File 'lib/nitro/runner.rb', line 47

def mode
  @mode
end

#serverObject

The server used to run this web application. :webrick, :nitro, :lhttp, :apache, :mod_apache

At the moment only :webrick and :lhttp are available.



58
59
60
# File 'lib/nitro/runner.rb', line 58

def server
  @server
end

Class Method Details

.run(conf) ⇒ Object

Helper method.



226
227
228
# File 'lib/nitro/runner.rb', line 226

def self.run(conf)
	runner = Runner.new.setup.run(conf)
end

Instance Method Details

#invoke_fcgi_proc(conf) ⇒ Object



195
196
197
198
# File 'lib/nitro/runner.rb', line 195

def invoke_fcgi_proc(conf)
	require 'nitro/adapters/fastcgi'
	FastCGI.start(conf)
end

#invoke_irb(conf) ⇒ Object



200
201
202
# File 'lib/nitro/runner.rb', line 200

def invoke_irb(conf)
	$conf = conf
end

#invoke_server(conf) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/nitro/runner.rb', line 204

def invoke_server(conf)
	case @action
	when :start
		
		case @server

		when :webrick
			require 'nitro/adapters/webrick'
			Webrick.start(conf)
			
		when :lhttpd
			require 'nitro/adapters/fastcgi'
			`lighttpd -f conf/lhttpd.conf`
		end
	
	when :stop

	end
end

#run(conf) ⇒ Object

Run the application in the declared execution mode, using the passed configuration parameters.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/nitro/runner.rb', line 150

def run(conf)

	if conf.is_a?(Hash)
		conf = Conf.new(conf)
	end

	case @mode
	when :debug
		setup_debug(conf)

	when :stage
		setup_stage(conf)
		
	when :live
		setup_live(conf)
	end

	if 'fcgi_proc' == ENV['NITRO_INVOKE']
		invoke_fcgi_proc(conf)
	elsif 'irb' == ENV['NITRO_INVOKE']
		invoke_irb(conf)
	else
		invoke_server(conf)
	end

end

#setupObject

Parse the command line arguments and the environment parameters to setup the application.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/nitro/runner.rb', line 63

def setup

	@mode = :debug
	@action = :start
	@server = :webrick
	@daemon = false

	parser = OptionParser.new do |opts|

		opts.banner = 'Usage: run.rb [options]'
		opts.separator ''
		opts.separator 'Specific options:'

		opts.on('-s', '--start', 'Start application.') do 
			@action = :start	
		end

		opts.on('-S', '--stop', 'Stop application.') do
			@action = :stop
		end

		opts.on('-r', '--restart', 'Restart application.') do
			@action = :restart
		end

		opts.on('-d', '--daemon', 'Run application as a daemon.') do
			@daemon = true
		end
		
		opts.on('-D', '--debug', 'Run application in debug mode.') do
			@mode = :debug
		end

		opts.on('-T', '--stage', 'Run application in stage mode.') do
			@mode = :stage
		end

		opts.on('-L', '--live', 'Run application in live mode.') do
			@mode = :live
		end

		opts.on('-w', '--webrick', 'Use a webrick server [default].') do
			@server = :webrick
		end

		opts.on('-l', '--lhttpd', 'Use a lighttpd server.') do
			@server = :lhttpd
			Logger.set(Logger.new('log/app.log'))
		end

		opts.on('-C', '--console', 'Start a console attached to an instance of the application.') do
			if RUBY_PLATFORM =~ /mswin32/
   			irb_name = 'irb.bat'
  		else
	  	  irb_name = 'irb'
			end
			ENV['NITRO_INVOKE'] = 'irb'
			conf_file = File.basename(caller.last.split(':').first)
	  	exec "#{irb_name} -r #{conf_file} -r irb/completion --noinspect"
			exit
		end

		opts.on('-l', '--lhttpd', 'Use a lighttpd server.') do
			@server = :lhttpd
			Logger.set(Logger.new('log/app.log'))
		end

		opts.on_tail('-v', '--version', 'Show version.') do 
			puts "Nitro #{Nitro::Version}"
			exit
		end

		opts.on_tail('-h', '--help', 'Show this message.') do
			puts opts
			exit
		end
		
	end

	parser.parse!(ARGV)

	return self
end

#setup_debug(conf) ⇒ Object



177
178
179
180
# File 'lib/nitro/runner.rb', line 177

def setup_debug(conf)
	$DBG = true
	Rendering.reload = :full
end

#setup_live(conf) ⇒ Object Also known as: setup_production



188
189
190
191
192
# File 'lib/nitro/runner.rb', line 188

def setup_live(conf)
	$DBG = false
	Rendering.reload = false 
	Logger.set(Logger.new('log/app.log'))
end

#setup_stage(conf) ⇒ Object



182
183
184
185
186
# File 'lib/nitro/runner.rb', line 182

def setup_stage(conf)
	$DBG = false
	Rendering.reload = false 
	Logger.set(Logger.new('log/app.log'))
end