1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Added requested features for running RoR applications.

git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@36 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
zedshaw 2006-02-14 12:19:02 +00:00
parent d70fae78a6
commit 709ef40013
4 changed files with 100 additions and 35 deletions

5
README
View file

@ -11,7 +11,7 @@ scream without too many portability issues.
== Status
The 0.3.1 release support Ruby On Rails much better than previously, and also
The 0.3.2 release supports Ruby On Rails much better than previously, and also
sports the beginning of a command and plugin infrastructure. This last part
isn't documented yet.
@ -37,6 +37,9 @@ And you can stop it whenever you like with:
All of which should be done from your application's directory. It writes the
PID of the process you ran into log/mongrel.pid.
There are also many more new options for configuring the rails runner including
changing to a different directory, adding more MIME types, and setting processor
threads and timeouts.
== Install

View file

@ -31,6 +31,6 @@ setup_extension("http11", "http11")
summary = "A small fast HTTP library and server that runs Rails, Camping, and Nitro apps."
test_file = "test/test_ws.rb"
setup_gem("mongrel", "0.3.1", "Zed A. Shaw", summary, ['mongrel_rails'], test_file) do |spec|
setup_gem("mongrel", "0.3.2", "Zed A. Shaw", summary, ['mongrel_rails'], test_file) do |spec|
spec.add_dependency('daemons', '>= 0.4.2')
end

View file

@ -51,9 +51,13 @@ end
class RailsHandler < Mongrel::HttpHandler
def initialize(dir)
def initialize(dir, mime_map = {})
@files = Mongrel::DirHandler.new(dir,false)
@guard = Mutex.new
# register the requested mime types
mime_map.each {|k,v| Mongrel::DirHandler::add_mime_type(k,v) }
end
def process(request, response)
@ -88,50 +92,94 @@ class StartCommand < Mongrel::Command::Command
def configure
options [
["-e", "--environment ENV", "Rails environment to run as", :@environment, "production"],
["-d", "--daemonize", "Whether to run in the background or not", :@daemon, false],
['-p', '--port PORT', "Which port to bind to", :@port, 3000],
['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"],
['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"]
["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"],
["-d", "--daemonize", "Whether to run in the background or not", :@daemon, false],
['-p', '--port PORT', "Which port to bind to", :@port, 3000],
['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"],
['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"],
['-n', '--num-procs INT', "Number of processor threads to use", :@num_procs, 20],
['-t', '--timeout SECONDS', "Timeout all requests after SECONDS time", :@timeout, 120],
['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"],
]
end
def validate
valid? ["production","development"].include?(@environment), "Only valid environments are 'production' or 'development'"
@cwd = File.expand_path(@cwd)
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
# change there to start, then we'll have to come back after daemonize
Dir.chdir(@cwd)
valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file"
valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file"
valid_dir? @docroot, "Path to docroot not valid: #@docroot"
valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map
return @valid
end
def run
cwd = Dir.pwd
def daemonize
# save this for later since daemonize will hose it
if @daemon
puts "Running as Daemon at #@address:#@port"
Daemonize.daemonize(log_file=@log_file)
open(File.join(cwd,@pid_file),"w") {|f| f.write(Process.pid) }
puts "Started Mongrel server in #@environment mode at #@address:#@port"
Daemonize.daemonize(log_file=File.join(@cwd, @log_file))
# change back to the original starting directory
Dir.chdir(cwd)
Dir.chdir(@cwd)
open(@pid_file,"w") {|f| f.write(Process.pid) }
else
puts "Running at #@address:#@port"
puts "Running Mongrel server in #@environment mode at #@address:#@port"
end
end
def load_mime_map
mime = {}
# configure any requested mime map
if @mime_map
puts "Loading additional MIME types from #@mime_map"
mime.merge!(YAML.load_file(@mime_map))
# check all the mime types to make sure they are the right format
mime.each {|k,v| puts "WARNING: MIME type #{k} must start with '.'" if k.index(".") != 0 }
end
return mime
end
def configure_rails
ENV['RAILS_ENV'] = @environment
require 'config/environment'
h = Mongrel::HttpServer.new(@address, @port)
h.register("/", RailsHandler.new(File.join(cwd,"public")))
h.run
# configure the rails handler
rails = RailsHandler.new(@docroot, load_mime_map)
return rails
end
def start_mongrel(rails)
# start up mongrel with the right configurations
server = Mongrel::HttpServer.new(@address, @port, @num_procs.to_i, @timeout.to_i)
server.register("/", rails)
server.run
begin
h.acceptor.join
puts "Server ready."
server.acceptor.join
rescue Interrupt
puts "Interrupted."
end
end
def run
daemonize
rails = configure_rails
start_mongrel(rails)
end
end
@ -140,13 +188,18 @@ class StopCommand < Mongrel::Command::Command
def configure
options [
["-d", "--daemonize", "Whether to run in the background or not", :@daemon, false],
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"]
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"]
]
end
def validate
@cwd = File.expand_path(@cwd)
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
@pid_file = File.join(@cwd,@pid_file)
valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?"
return @valid
end

View file

@ -314,7 +314,7 @@ module Mongrel
# try changing it higher. If you find that responses are way too slow
# try lowering it (after you've tuned your stuff of course).
# Future versions of Mongrel will make this more dynamic (hopefully).
def initialize(host, port, num_processors=20)
def initialize(host, port, num_processors=20, timeout=120)
@socket = TCPServer.new(host, port)
@classifier = URIClassifier.new
@ -322,10 +322,17 @@ module Mongrel
@host = host
@port = port
@num_processors = num_processors
@timeout = timeout
@num_processors.times {|i| Thread.new do
while client = @req_queue.deq
process_client(client)
begin
Timeout.timeout(@timeout) do
process_client(client)
end
rescue Timeout::Error
STDERR.puts "WARNING: Request took longer than #@timeout second timeout"
end
end
end
}
@ -540,7 +547,7 @@ module Mongrel
end
end
open(req, "r") do |f|
open(req, "rb") do |f|
out.write(f.read)
end
end
@ -572,12 +579,14 @@ module Mongrel
end
end
end
# There is a small number of default mime types for extensions, but
# this lets you add any others you'll need when serving content.
def DirHandler::add_mime_type(extension, type)
MIME_TYPES[extension] = type
end
end
# There is a small number of default mime types for extensions, but
# this lets you add any others you'll need when serving content.
def add_mime_type(extension, type)
MIME_TYPES[extension] = type
end
end