Class: Nitro::Runner

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

Overview

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

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

You can implement your own, custom version of the Runner to run your custom web applications. – WARNING: this class will be deprecated! FIXME: Trim this class, most functionality goes to bin/nitro. ++

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actionObject

:start, :stop, :restart



29
30
31
# File 'lib/nitro/server/runner.rb', line 29

def action
  @action
end

#daemonObject

Run as daemon.



40
41
42
# File 'lib/nitro/server/runner.rb', line 40

def daemon
  @daemon
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.



36
37
38
# File 'lib/nitro/server/runner.rb', line 36

def server
  @server
end

#spiderObject

Spidering mode. Acceptable values are :crawl, :render and false.



45
46
47
# File 'lib/nitro/server/runner.rb', line 45

def spider
  @spider
end

Instance Method Details

#daemonizeObject

Run this proccess as a daemon (UNIX only).



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/nitro/server/runner.rb', line 369

def daemonize
  require 'daemons/daemonize'

  pwd = Dir.pwd
  Daemonize.daemonize(File.join(pwd, 'log/app.log'))

  # Restore the original pwd (daemonize sets the 
  # pwd to '/').

  Dir.chdir(pwd)

  # Save a process sentinel file.
  
  FileUtils.touch ".a#{Process.pid}.pid"
  
  # Set the logger to a file (daemonize closes the
  # std streams).

  Logger.set(Logger.new('log/app.log'))
end

#invoke(server) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/nitro/server/runner.rb', line 262

def invoke(server)
  case ENV['NITRO_INVOKE']
  when 'fcgi_proc'
    require 'nitro/adapter/fastcgi'
    FastCGI.start(server)

  when 'scgi_proc'
    require 'nitro/adapter/scgi'
    Scgi.start(server)
    
  when 'cgi_proc'
    require 'nitro/adapter/cgi'
    CgiAdapter.start(server)
    
  when 'irb'
    require 'nitro/adapter/console'
    require 'nitro/caching/proxy'
    $server = server
    $app = ConsoleAdapter.new(server)
    $cache = Caching::Proxy
    
  else
    invoke_server(server)
  end
end

#invoke_server(server) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/nitro/server/runner.rb', line 290

def invoke_server(server)
  spider_thread = nil
  
  # FIXME refactor !
  server.address = @server_address if @server_address
  server.port = @server_port if @server_port
  server.port += @server_port_offset if @server_port_offset
  
  case @action
  when :start
    
    case @spider
      when :render
        spider_thread = Thread.new do
          sleep(6)
          `wget -k -m -p #{server.address}:#{server.port} -directory-prefix=rendered`
        end 
      when :crawl
        spider_thread = Thread.new do
          sleep(6)
          `wget -m --spider #{server.host}:#{server.port}`
        end 
    end

    @server = Nitro.adapter

    Logger.info ""
    Logger.info "Setup for #{Configuration.mode} mode."

    case @server
      when :webrick
        require 'nitro/adapter/webrick'
        Logger.info "Starting Webrick on #{server.address}:#{server.port}"
        Logger.info "Press Ctrl-C to shutdown; Run with --help for options."
        Logger.info ""      
        Webrick.start(server)

      when :mongrel
        require 'nitro/adapter/mongrel'
        Logger.info "Starting Mongrel on #{server.address}:#{server.port}"
        Logger.info "Press Ctrl-C to shutdown; Run with --help for options."
        Logger.info ""      
        Mongrel.start(server)

      when :lhttpd
        require 'nitro/adapter/fastcgi'
        puts "==> Launching lighttpd (FastCGI)."
        `lighttpd -f conf/lhttpd_fcgi.conf`

      when :apache
        require 'nitro/adapter/fastcgi'
        puts "==> Launching apache (FastCGI)."
        `apachectl -d #{Dir.pwd} -f conf/apache.conf -k start`

      when :cgi
        require 'nitro/adapter/cgi'
        puts "==> Using standard CGI. Please look into using Fast/Scgi"

    end
    
  when :stop

    case @server
    when :webrick
      
    when :lhttpd

    when :apache
      `apachectl -d #{Dir.pwd} -f conf/apache.conf -k stop`
    
    end

  end
end

#load_external_configuration(mode = :debug) ⇒ Object

Attempt to load external configuration in Ruby or YAML format. The files:

  • conf/mode.rb

  • conf/mode.yaml

are considered.



398
399
400
401
402
403
404
405
406
407
408
# File 'lib/nitro/server/runner.rb', line 398

def load_external_configuration(mode = :debug)
  
  # require "conf/#{mode}.rb"
  ruby_conf = "conf/#{mode}.rb"
  load ruby_conf if File.exist?(ruby_conf)

  # Try to configure from a yaml file.
  yml_conf = "conf/#{mode}.yml"
  Configuration.load yml_conf if File.exist?(yml_conf)

end

#setup_debugObject

Setup in debug mode.



223
224
225
226
227
228
229
230
# File 'lib/nitro/server/runner.rb', line 223

def setup_debug
  $DBG.nil? ? true : $DBG
  Compiler.reload = true
  autoreload(3)
  Caching.caching_enabled = false
  
#    load_external_configuration(:debug)
end

#setup_liveObject Also known as: setup_production



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/nitro/server/runner.rb', line 242

def setup_live
  $DBG.nil? ? false : $DBG
  
  # Enable the reloading even on live apps by default.
  # But have a longer thread sleep time.
  # If you really need sligthly faster dispatching enable
  # reloading (Compiler.reload = false)

  Compiler.reload = true
  autoreload(2 * 60)
  
#    load_external_configuration(:live)
#    load_external_configuration(:production)
  
  Logger.set(Configuration.log)
end

#setup_modeObject

Setup the declared execution mode, using the passed configuration parameters.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/nitro/server/runner.rb', line 199

def setup_mode
  case Configuration.mode
    when :debug
      setup_debug

    when :stage
      setup_stage
      
    when :live
      setup_live
  end
  
  # Special setup for distributed sessions.
=begin    
  if defined?(Session) and defined?(DRbObject)
    if Session.store.is_a?(DRbObject)
      system('ruby ' + File.join(Nitro::LibPath, 'session', 'drbserver.rb') + ' --address #{Session.drb_address} --port #{Session.drb_port} --daemon')
    end
  end
=end    
end

#setup_optionsObject

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



50
51
52
53
54
55
56
57
58
59
60
61
62
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
146
147
148
149
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/nitro/server/runner.rb', line 50

def setup_options
  @action ||= :start
  @daemon = false
  @spider = false

  # Setup from command line arguments.
  
  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
      Configuration.mode = :debug
    end

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

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

    opts.on('--address IP', 'Force the server to run on this address.') do |a|
      @server_address = a
    end

    opts.on('--port PORT', 'Force the server to run on this port.') do |p|
      @server_port = p.to_i
    end

    opts.on('--port_offset PORT', 'The port offset in the cluster.') do |o|
      @server_port_offset = o.to_i
    end

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

    opts.on('-m', '--mongrel', 'Use the Mongrel Ruby server.') do
      Nitro.adapter = :mongrel
      # FIXME: handle logging.
    end

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

    opts.on('-l', '--lhttpd-scgi', 'Use the SCGI adapter (Lighttpd).') do
      Nitro.adapter = :lhttpd_scgi
      Logger.set(Logger.new('log/app.log'))
    end

    opts.on('-a', '--apache', 'Use an apache server.') do
      Nitro.adapter = :apache
      Logger.set(Logger.new('log/app.log'))
    end

    opts.on('--apache-cgi', 'Use the CGI adapter (Apache)') do
      Nitro.adapter = :cgi
      Logger.set(Logger.new('log/app.log'))
    end

    opts.on('--scgi', 'Use the generic SCGI adapter') do
      Nitro.adapter = :scgi
    end
    
    opts.on('--crawl', 'Crawl the application.') do
      Nitro.adapter = :webrick
      @spider = :crawl
    end

    opts.on('--render', 'Crawl the application and render all pages as static html files.') do
      Nitro.adapter = :webrick
      @spider = :render
    end

    opts.on_tail('-h', '--help', 'Show this message.') do
      puts opts
      exit
    end
  
    # Hack fix for bin/nitro.

    opts.on_tail('-v', '--verbose', 'Verbose mode') do 
      # nop, hack fix.
    end

    opts.on('--destroy') do
      # nop, hack fix.
    end      

    opts.on('-C', '--console', 'Start a console attached to an instance of the application.') do
      # nop, hack fix.
    end

    opts.on('--app', 'The application name') do |adapter|
      # nop, hack fix.
    end

    opts.on('--mode', 'Set the execution mode') do |adapter|
      # nop, hack fix.
    end

    opts.on('--adapter', 'Set the adapter to use') do |adapter|
      # nop, hack fix.
    end

    opts.on('--record', 'Record the application server session to the given file.') do |filename|
      # nop, hack fix.
    end

    opts.on('--playback', 'Playback a previously recorded session from the given file.') do |filename|
      # nop, hack fix.
    end

    opts.on('--cluster', 'Setup a cluster.') do |ic|
      # nop, hack fix.
    end

  end

  parser.parse!(ARGV)

  return self
end

#setup_stageObject



232
233
234
235
236
237
238
239
240
# File 'lib/nitro/server/runner.rb', line 232

def setup_stage
  $DBG.nil? ? false : $DBG
  Compiler.reload = true
  autoreload(3)
  
#    load_external_configuration(:stage)
  
  Logger.set(Configuration.log)
end