From 67a0d9e93388093eb8fb05dd42655a90a832bc21 Mon Sep 17 00:00:00 2001 From: zedshaw Date: Sun, 12 Feb 2006 03:37:38 +0000 Subject: Release that has better Rails servicing support. Might not work in win32. git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@32 19e92222-5c0b-0410-8929-a290d50e31e9 --- README | 52 +++++++++++++++++++----------------------- Rakefile | 6 +++-- bin/mongrel_rails | 39 ++++++++++++++++++++++++++++--- lib/mongrel/command.rb | 61 +++++++++++++++++++++---------------------------- tools/rakehelp.rb | 62 +++++++++++++++++++++++++++----------------------- 5 files changed, 123 insertions(+), 97 deletions(-) diff --git a/README b/README index 6a73398..45f4fbe 100644 --- a/README +++ b/README @@ -11,30 +11,31 @@ scream without too many portability issues. == Status -The 0.3 release is the first official release to start supporting Ruby on Rails -and to have a more complete DirHandler for serving directories of files. This release -is actually closer to a full functioning web server than the previous releases. +The 0.3.1 release support 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. -The Rails support is pretty rough right now, but check out the bin/mongrel_rails file, -which should be installed into your PATH if you use a gem. You should be able to -do the following to run your Rails applications: +After you've installed (either with gem install mongrel or via source) you should +have the mongrel_rails command available in your PATH. Then you just do the following: - > cd myrailsapp - > mongrel_rails 0.0.0.0 3000 + > cd myrailsapp + > mongrel_rails start -And then hit http://localhost:3000/ to see your app. One thing is that if you have -a public/index.html file then you'll get that served instead of your Rails application. +This will start it in the foreground so you can play with it. It runs your application +in production mode. To get help do: -People with the daemons gem installed will see that mongrel_rails will go into the -background. You can kill it with: + > mongrel_rails start -h - > kill -TERM `cat log/mongrel-3000.pid` +Finally, you can then start in background mode (probably won't work in win32): -Where "3000" is whatever port you told it to listen on when you ran it. + > mongrel_rails start -d -The file serving is still a little rough and the redirects might not work well, but -try it out and tell me about any weird errors. File uploads will definitely have some -problems. +And you can stop it whenever you like with: + + > mongrel_rails stop + +All of which should be done from your application's directory. It writes the +PID of the process you ran into log/mongrel.pid. == Install @@ -82,16 +83,13 @@ type mapping is missing though.* == Speed -The 0.2.1 release probably consists of the most effort I've ever put into -tuning a Ruby library for speed. It consists of nearly everything I could think -of to make Mongrel the fastest Ruby HTTP library possible. I've tried about -seven different architectures and IO processing methods and none of them -make it any faster. In short: Mongrel is amazingly fast considering Ruby's speed -limitations. +Like previous releases 0.3.1 continues the trend of making things +as fast as possible. It currently might be a little slower than +other releases but should hold up pretty good against at least +WEBrick (especially when running Rails). -This release also brings in controllable threads that you can scale to meet your -needs to do your processing. Simple pass in the HttpServer.new third optional -parameter: +As before you can control the number of processor threads (and thus +ActiveRecord database connections) with: h = Mongrel::HttpServer.new("0.0.0.0", "3000", 40) @@ -101,8 +99,6 @@ limited processors also means that you can use ActiveRecord as-is and it will create a matching database connection for each processor thread. More on this in future releases. -With this release I'm hoping that I've created a nice solid fast as hell core -upon which I can build the remaining features I want in Mongrel. == The Future diff --git a/Rakefile b/Rakefile index 71afd43..38470e7 100644 --- a/Rakefile +++ b/Rakefile @@ -24,6 +24,8 @@ end setup_extension("http11", "http11") -summary = "An experimental fast simple web server for Ruby." +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", "Zed A. Shaw", summary, ['mongrel_rails'], test_file) +setup_gem("mongrel", "0.3.1", "Zed A. Shaw", summary, ['mongrel_rails'], test_file) do |spec| + spec.add_dependency('daemons', '>= 0.4.2') +end diff --git a/bin/mongrel_rails b/bin/mongrel_rails index a184659..e94d855 100644 --- a/bin/mongrel_rails +++ b/bin/mongrel_rails @@ -83,6 +83,7 @@ class RailsHandler < Mongrel::HttpHandler end + class StartCommand < Mongrel::Command::Command def configure @@ -109,15 +110,16 @@ class StartCommand < Mongrel::Command::Command cwd = Dir.pwd if @daemon - STDERR.puts "Running as Daemon at #@address:#@port" + 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) } # change back to the original starting directory Dir.chdir(cwd) else - STDERR.puts "Running at #@address:#@port" + puts "Running at #@address:#@port" end + ENV['RAILS_ENV'] = @environment require 'config/environment' h = Mongrel::HttpServer.new(@address, @port) @@ -127,8 +129,39 @@ class StartCommand < Mongrel::Command::Command begin h.acceptor.join rescue Interrupt - STDERR.puts "Interrupted." + puts "Interrupted." + end + end +end + + + +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"] + ] + end + + def validate + valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?" + return @valid + end + + + def run + pid = open(@pid_file).read.to_i + print "Stopping Mongrel at PID #{pid}..." + begin + Process.kill("INT", pid) + rescue Errno::ESRCH + puts "Process does not exist. Not running." end + + File.unlink(@pid_file) + puts "Done." end end diff --git a/lib/mongrel/command.rb b/lib/mongrel/command.rb index 9d73dad..e5972b1 100644 --- a/lib/mongrel/command.rb +++ b/lib/mongrel/command.rb @@ -4,13 +4,21 @@ require 'pluginfactory' module Mongrel + + # Contains all of the various commands that are used with + # Mongrel servers. + module Command + + # A Command pattern implementation used to create the set of command available to the user # from Mongrel. The script uses objects which implement this interface to do the # user's bidding. # - # Implementing a command is fairly easy. Refer to some of the stock commands in the - # lib/mongrel/command directory for examples. + # Creating a new command is very easy, and you can do it without modifying the source + # of Mongrel thanks to PluginFactory. What you do is the following: + # + # 1. class Command include PluginFactory @@ -48,16 +56,18 @@ module Mongrel # I need to add my own -v definition to prevent the -h from exiting by default as well. @opt.on_tail("--version", "Show version") do @done_validating = true - puts "No version yet." + if VERSION + puts "Version #{VERSION}" + end end @opt.parse! argv end # Tells the PluginFactory where to look for additional commands. By default - # it's just a "mongrel" directory wherever we are located. + # it's just a "plugins" directory wherever we are located. def self.derivativeDirs - return ["mongrel"] + return ["plugins"] end # Returns true/false depending on whether the command is configured properly. @@ -80,7 +90,7 @@ module Mongrel # Validates the given expression is true and prints the message if not, exiting. def valid?(exp, message) if not @done_validating and (not exp) - STDERR.puts message + failure message @valid = false @done_validating = true end @@ -101,6 +111,11 @@ module Mongrel def valid_dir?(file, message) valid?(file != nil && File.directory?(file), message) end + + # Just a simple method to display failure until something better is developed. + def failure(message) + STDERR.puts "!!! #{message}" + end end @@ -117,7 +132,7 @@ module Mongrel results = [] list.keys.each do |key| - results << key unless match.match(key.to_s) + results << key.to_s unless match.match(key.to_s) end return results.sort @@ -125,13 +140,13 @@ module Mongrel # Prints a list of available commands. def print_command_list - puts "Available commands are:\n" + puts "Available commands are:\n\n" self.commands.each do |name| puts " - #{name}\n" end - puts "Each command takes -h as an option to get help." + puts "\nEach command takes -h as an option to get help." end @@ -152,7 +167,7 @@ module Mongrel begin command = Command.create(cmd_name, args) rescue FactoryError - STDERR.puts :command, "INVALID COMMAND." + STDERR.puts "INVALID COMMAND: #$!" print_command_list return end @@ -162,7 +177,7 @@ module Mongrel # needed so the command is already valid so we can skip it. if not command.done_validating if not command.validate - STDERR.puts :command, "#{cmd_name} reported an error. Use -h to get help." + STDERR.puts "#{cmd_name} reported an error. Use -h to get help." return false else command.run @@ -171,30 +186,6 @@ module Mongrel return true end - # Runs the command like normal, but redirects $stdout and $stderr to the - # requested log file (which should be a file like object opened by you). - # It also marks the start and end times in the log file. - def run_redirect(log, args) - res = false - - begin - oldstdout = $stdout - oldstderr = $stderr - - log.write ">>>>>> #{Time.now}\n" - $stdout = log - $stderr = log - - res = run(args) - - log.write "<<<<<< #{Time.now}\n" - - ensure - $stdout = oldstdout - $stderr = oldstderr - return res - end - end end end end diff --git a/tools/rakehelp.rb b/tools/rakehelp.rb index 6996658..7b36230 100644 --- a/tools/rakehelp.rb +++ b/tools/rakehelp.rb @@ -65,35 +65,39 @@ end def setup_gem(pkg_name, pkg_version, author, summary, executables, test_file) - pkg_version = pkg_version - pkg_name = pkg_name - pkg_file_name = "#{pkg_name}-#{pkg_version}" - - spec = Gem::Specification.new do |s| - s.name = pkg_name - s.version = pkg_version - s.platform = Gem::Platform::RUBY - s.author = author - s.summary = summary - s.test_file = test_file - s.has_rdoc = true - s.extra_rdoc_files = [ "README" ] - - s.files = %w(COPYING LICENSE ext/http11/MANIFEST README Rakefile setup.rb) + - Dir.glob("{bin,doc,test,lib}/**/*") + - Dir.glob("ext/**/*.{h,c,rb}") + - Dir.glob("examples/**/*.rb") + - Dir.glob("tools/*.rb") + pkg_version = pkg_version + pkg_name = pkg_name + pkg_file_name = "#{pkg_name}-#{pkg_version}" + + spec = Gem::Specification.new do |s| + s.name = pkg_name + s.version = pkg_version + s.platform = Gem::Platform::RUBY + s.author = author + s.summary = summary + s.test_file = test_file + s.has_rdoc = true + s.extra_rdoc_files = [ "README" ] - s.require_path = "lib" - s.extensions = FileList["ext/**/extconf.rb"].to_a - - s.executables = executables - s.bindir = "bin" - end - - Rake::GemPackageTask.new(spec) do |p| - p.gem_spec = spec - p.need_tar = true + s.files = %w(COPYING LICENSE ext/http11/MANIFEST README Rakefile setup.rb) + + Dir.glob("{bin,doc,test,lib}/**/*") + + Dir.glob("ext/**/*.{h,c,rb}") + + Dir.glob("examples/**/*.rb") + + Dir.glob("tools/*.rb") + + s.require_path = "lib" + s.extensions = FileList["ext/**/extconf.rb"].to_a + + s.executables = executables + s.bindir = "bin" + + if block_given? + yield s end + end + + Rake::GemPackageTask.new(spec) do |p| + p.gem_spec = spec + p.need_tar = true + end end -- cgit v1.2.3-24-ge0c7