Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4570

Show
Ignore:
Timestamp:
07/06/06 17:21:42 (2 years ago)
Author:
david
Message:

Added Mongrel-spawning capabilities to script/process/spawner. Mongrel will be the default choice if installed, otherwise FCGI is tried [DHH]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/lib/commands/process/spawner.rb

    r4502 r4570  
     1require 'active_support' 
    12require 'optparse' 
    23require 'socket' 
     
    3031        srv = nil 
    3132 
    32         print "NO\n
    33         print "Starting FCGI on port: #{port}\n 
     33        puts "NO
     34        puts "Starting dispatcher on port: #{port}
    3435 
    3536        FileUtils.mkdir_p(OPTIONS[:pids]) 
    3637        spawn(port) 
    3738      rescue 
    38         print "YES\n
     39        puts "YES
    3940      end 
    4041    end 
     
    4849end 
    4950 
    50 # TODO: 
    51 # class MongrelSpawner < Spawner 
    52 #   def self.spawn(port) 
    53 #   end 
    54 # end 
     51class MongrelSpawner < Spawner 
     52  def self.spawn(port) 
     53    system("mongrel_rails start -d -p #{port} -P #{OPTIONS[:pids]}/#{OPTIONS[:process]}.#{port}.pid -e #{OPTIONS[:environment]}") 
     54  end 
     55end 
     56 
     57 
     58begin 
     59  require_library_or_gem 'fcgi' 
     60rescue Exception 
     61  # FCGI not available 
     62end 
     63 
     64begin 
     65  require_library_or_gem 'mongrel' 
     66rescue Exception 
     67  # Mongrel not available 
     68end 
     69 
     70server = case ARGV.first 
     71  when "fcgi", "mongrel" 
     72    ARGV.shift 
     73  else 
     74    if defined?(Mongrel) 
     75      "mongrel" 
     76    elsif RUBY_PLATFORM !~ /mswin/ && !silence_stderr { `spawn-fcgi -version` }.blank? && defined?(FCGI) 
     77      "fcgi" 
     78    end 
     79end 
     80 
     81case server 
     82  when "fcgi" 
     83    puts "=> Starting FCGI dispatchers" 
     84    spawner_class = FcgiSpawner 
     85  when "mongrel" 
     86    puts "=> Starting mongrel dispatchers" 
     87    spawner_class = MongrelSpawner 
     88  else 
     89    puts "Neither FCGI (spawn-fcgi) nor Mongrel was installed and available!" 
     90    exit(0) 
     91end 
     92 
    5593 
    5694 
     
    67105 
    68106ARGV.options do |opts| 
    69   opts.banner = "Usage: spawner [options]" 
     107  opts.banner = "Usage: spawner [platform] [options]" 
    70108 
    71109  opts.separator "" 
     
    73111  opts.on <<-EOF 
    74112  Description: 
    75     The spawner is a wrapper for spawn-fcgi that makes it easier to start multiple FCGI 
     113    The spawner is a wrapper for spawn-fcgi and mongrel that makes it easier to start multiple 
    76114    processes running the Rails dispatcher. The spawn-fcgi command is included with the lighttpd  
    77115    web server, but can be used with both Apache and lighttpd (and any other web server supporting 
    78     externally managed FCGI processes). 
     116    externally managed FCGI processes). Mongrel automatically ships with with mongrel_rails for starting 
     117    dispatchers. 
     118     
     119    The first choice you need to make is whether to spawn the Rails dispatchers as FCGI or Mongrel. By default, 
     120    this spawner will prefer Mongrel, so if that's installed, and no platform choice is made, Mongrel is used. 
    79121 
    80     You decide a starting port (default is 8000) and the number of FCGI process instances you'd  
     122    Then decide a starting port (default is 8000) and the number of FCGI process instances you'd  
    81123    like to run. So if you pick 9100 and 3 instances, you'll start processes on 9100, 9101, and 9102. 
    82124 
     
    85127 
    86128  Examples: 
    87     spawner               # starts instances on 8000, 8001, and 8002 
    88     spawner -p 9100 -i 10 # starts 10 instances counting from 9100 to 9109 
     129    spawner               # starts instances on 8000, 8001, and 8002 using Mongrel if available 
     130    spawner fcgi          # starts instances on 8000, 8001, and 8002 using FCGI 
     131    spawner mongrel -i 5  # starts instances on 8000, 8001, 8002, 8003, and 8004 using Mongrel 
     132    spawner -p 9100 -i 10 # starts 10 instances counting from 9100 to 9109 using Mongrel if available 
    89133    spawner -p 9100 -r 5  # starts 3 instances counting from 9100 to 9102 and attempts start them every 5 seconds 
    90134  EOF 
     
    112156  daemonize 
    113157  trap("TERM") { exit } 
    114   FcgiSpawner.record_pid 
     158  spawner_class.record_pid 
    115159 
    116160  loop do 
    117     FcgiSpawner.spawn_all 
     161    spawner_class.spawn_all 
    118162    sleep(OPTIONS[:repeat]) 
    119163  end 
    120164else 
    121   FcgiSpawner.spawn_all 
     165  spawner_class.spawn_all 
    122166end