From 110e92752bd182459a08db3fcb8eb4a48d2f846f Mon Sep 17 00:00:00 2001 From: zedshaw Date: Mon, 27 Mar 2006 06:10:07 +0000 Subject: Final tweaks to speed up the file serving a bit using sendfile and a modified file handler. git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@124 19e92222-5c0b-0410-8929-a290d50e31e9 --- bin/mongrel_rails_svc | 246 +++++++++++++++++++++++++++----------------------- 1 file changed, 131 insertions(+), 115 deletions(-) (limited to 'bin') diff --git a/bin/mongrel_rails_svc b/bin/mongrel_rails_svc index 5fa4caa..8056bcf 100644 --- a/bin/mongrel_rails_svc +++ b/bin/mongrel_rails_svc @@ -4,6 +4,7 @@ # This is where Win32::Daemon resides. ############################################### require 'rubygems' +require 'mongrel' require 'mongrel/rails' require 'optparse' require 'win32/service' @@ -70,14 +71,15 @@ class MongrelRails def delayed_initialize dbg "delayed_initialize entered" - + @rails = configure_rails # start up mongrel with the right configurations @server = Mongrel::HttpServer.new(@ip, @port, @num_procs.to_i, @timeout.to_i) @server.register("/", @rails) - + dbg "delayed_initialize left" + end def load_mime_map @@ -104,11 +106,12 @@ class MongrelRails Dir.chdir(@rails_root) + ENV['RAILS_ENV'] = @environment - require File.join(@rails_root, 'config/environment') + require 'config/environment' # configure the rails handler - rails = RailsHandler.new(@docroot, load_mime_map) + rails = Mongrel::Rails::RailsHandler.new(@docroot, load_mime_map) dbg "configure_rails left" @@ -116,24 +119,29 @@ class MongrelRails end def start_serve - dbg "start_serve entered" - - @runner = Thread.new do - dbg_th "runner_thread suspended" - Thread.stop + begin + dbg "start_serve entered" + + @runner = Thread.new do + dbg_th "runner_thread suspended" + Thread.stop + + dbg_th "runner_thread resumed" + dbg_th "runner_thread acceptor.join" + @server.acceptor.join + end - dbg_th "runner_thread resumed" - dbg_th "runner_thread acceptor.join" - @server.acceptor.join + dbg "server.run" + @server.run + + dbg "runner.run" + @runner.run + + dbg "start_serve left" + rescue + dbg "ERROR: #$!\r\n" + dbg $!.backtrace.join("\r\n") end - - dbg "server.run" - @server.run - - dbg "runner.run" - @runner.run - - dbg "start_serve left" end def stop_serve @@ -193,99 +201,107 @@ class RailsDaemon < Win32::Daemon end -if ARGV[0] == 'service' - ARGV.shift - - # default options - OPTIONS = { - :rails_root => Dir.pwd, - :environment => 'production', - :ip => '0.0.0.0', - :port => 3000, - :mime_map => nil, - :num_procs => 1024, - :timeout => 0, - :cpu => nil - } - - ARGV.options do |opts| - opts.on('-r', '--root PATH', "Set the root path where your rails app resides.") { |OPTIONS[:rails_root]| } - opts.on('-e', '--environment ENV', "Rails environment to run as. (default: production)") { |OPTIONS[:environment]| } - opts.on('-b', '--binding ADDR', "Address to bind to") { |OPTIONS[:ip]| } - opts.on('-p', '--port PORT', "Which port to bind to") { |OPTIONS[:port]| } - opts.on('-m', '--mime PATH', "A YAML file that lists additional MIME types") { |OPTIONS[:mime_map]| } - opts.on('-P', '--num-procs INT', "Number of processor threads to use") { |OPTIONS[:num_procs]| } - opts.on('-t', '--timeout SECONDS', "Timeout all requests after SECONDS time") { |OPTIONS[:timeout]| } - opts.on('-c', '--cpu CPU', "Bind the process to specific cpu") { |OPTIONS[:cpu]| } - - opts.parse! - end - - #expand RAILS_ROOT - OPTIONS[:rails_root] = File.expand_path(OPTIONS[:rails_root]) - - OPTIONS[:docroot] = File.expand_path(OPTIONS[:rails_root] + '/public') - - # We must bind to a specific cpu? - if OPTIONS[:cpu] - Kernel32.set_affinity(Process.pid, OPTIONS[:cpu]) - end - - rails = MongrelRails.new(OPTIONS[:ip], OPTIONS[:port], OPTIONS[:rails_root], OPTIONS[:docroot], OPTIONS[:environment], OPTIONS[:mime_map], OPTIONS[:num_procs].to_i, OPTIONS[:timeout].to_i) - rails_svc = RailsDaemon.new(rails) - rails_svc.mainloop - -elsif ARGV[0] == 'debug' - ARGV.shift - - # default options - OPTIONS = { - :rails_root => Dir.pwd, - :environment => 'production', - :ip => '0.0.0.0', - :port => 3000, - :mime_map => nil, - :num_procs => 20, - :timeout => 120, - :cpu => nil - } - - ARGV.options do |opts| - opts.on('-r', '--root PATH', "Set the root path where your rails app resides.") { |OPTIONS[:rails_root]| } - opts.on('-e', '--environment ENV', "Rails environment to run as.") { |OPTIONS[:environment]| } - opts.on('-b', '--binding ADDR', "Address to bind to") { |OPTIONS[:ip]| } - opts.on('-p', '--port PORT', "Which port to bind to") { |OPTIONS[:port]| } - opts.on('-m', '--mime PATH', "A YAML file that lists additional MIME types") { |OPTIONS[:mime_map]| } - opts.on('-P', '--num-procs INT', "Number of processor threads to use") { |OPTIONS[:num_procs]| } - opts.on('-t', '--timeout SECONDS', "Timeout all requests after SECONDS time") { |OPTIONS[:timeout]| } - opts.on('-c', '--cpu CPU', "Bind the process to specific cpu") { |OPTIONS[:cpu]| } - - opts.parse! - end - - #expand RAILS_ROOT - OPTIONS[:rails_root] = File.expand_path(OPTIONS[:rails_root]) - - OPTIONS[:docroot] = File.expand_path(OPTIONS[:rails_root] + '/public') - - # We must bind to a specific cpu? - if OPTIONS[:cpu] - Kernel32.set_affinity(Process.pid, OPTIONS[:cpu]) - end - - rails = MongrelRails.new(OPTIONS[:ip], OPTIONS[:port], OPTIONS[:rails_root], OPTIONS[:docroot], OPTIONS[:environment], OPTIONS[:mime_map], OPTIONS[:num_procs].to_i, OPTIONS[:timeout].to_i) - rails.delayed_initialize - rails.start_serve - - begin - sleep - rescue Interrupt - puts "graceful shutdown?" - end - - begin - rails.stop_serve - rescue - end - +begin + if ARGV[0] == 'service' + ARGV.shift + + # default options + OPTIONS = { + :rails_root => Dir.pwd, + :environment => 'production', + :ip => '0.0.0.0', + :port => 3000, + :mime_map => nil, + :num_procs => 1024, + :timeout => 0, + :cpu => nil + } + + ARGV.options do |opts| + opts.on('-r', '--root PATH', "Set the root path where your rails app resides.") { |OPTIONS[:rails_root]| } + opts.on('-e', '--environment ENV', "Rails environment to run as. (default: production)") { |OPTIONS[:environment]| } + opts.on('-b', '--binding ADDR', "Address to bind to") { |OPTIONS[:ip]| } + opts.on('-p', '--port PORT', "Which port to bind to") { |OPTIONS[:port]| } + opts.on('-m', '--mime PATH', "A YAML file that lists additional MIME types") { |OPTIONS[:mime_map]| } + opts.on('-P', '--num-procs INT', "Number of processor threads to use") { |OPTIONS[:num_procs]| } + opts.on('-t', '--timeout SECONDS', "Timeout all requests after SECONDS time") { |OPTIONS[:timeout]| } + opts.on('-c', '--cpu CPU', "Bind the process to specific cpu") { |OPTIONS[:cpu]| } + + opts.parse! + end + + #expand RAILS_ROOT + OPTIONS[:rails_root] = File.expand_path(OPTIONS[:rails_root]) + + OPTIONS[:docroot] = File.expand_path(OPTIONS[:rails_root] + '/public') + + # We must bind to a specific cpu? + if OPTIONS[:cpu] + Kernel32.set_affinity(Process.pid, OPTIONS[:cpu]) + end + + rails = MongrelRails.new(OPTIONS[:ip], OPTIONS[:port], OPTIONS[:rails_root], OPTIONS[:docroot], OPTIONS[:environment], OPTIONS[:mime_map], OPTIONS[:num_procs].to_i, OPTIONS[:timeout].to_i) + rails_svc = RailsDaemon.new(rails) + rails_svc.mainloop + + elsif ARGV[0] == 'debug' + ARGV.shift + + # default options + OPTIONS = { + :rails_root => Dir.pwd, + :environment => 'production', + :ip => '0.0.0.0', + :port => 3000, + :mime_map => nil, + :num_procs => 20, + :timeout => 120, + :cpu => nil + } + + ARGV.options do |opts| + opts.on('-r', '--root PATH', "Set the root path where your rails app resides.") { |OPTIONS[:rails_root]| } + opts.on('-e', '--environment ENV', "Rails environment to run as.") { |OPTIONS[:environment]| } + opts.on('-b', '--binding ADDR', "Address to bind to") { |OPTIONS[:ip]| } + opts.on('-p', '--port PORT', "Which port to bind to") { |OPTIONS[:port]| } + opts.on('-m', '--mime PATH', "A YAML file that lists additional MIME types") { |OPTIONS[:mime_map]| } + opts.on('-P', '--num-procs INT', "Number of processor threads to use") { |OPTIONS[:num_procs]| } + opts.on('-t', '--timeout SECONDS', "Timeout all requests after SECONDS time") { |OPTIONS[:timeout]| } + opts.on('-c', '--cpu CPU', "Bind the process to specific cpu") { |OPTIONS[:cpu]| } + + opts.parse! + end + + #expand RAILS_ROOT + OPTIONS[:rails_root] = File.expand_path(OPTIONS[:rails_root]) + + OPTIONS[:docroot] = File.expand_path(OPTIONS[:rails_root] + '/public') + + # We must bind to a specific cpu? + if OPTIONS[:cpu] + Kernel32.set_affinity(Process.pid, OPTIONS[:cpu]) + end + + rails = MongrelRails.new(OPTIONS[:ip], OPTIONS[:port], OPTIONS[:rails_root], OPTIONS[:docroot], OPTIONS[:environment], OPTIONS[:mime_map], OPTIONS[:num_procs].to_i, OPTIONS[:timeout].to_i) + rails.delayed_initialize + rails.start_serve + + begin + sleep + rescue Interrupt + dbg "ERROR: #$!\r\n" + dbg $!.backtrace.join("\r\n") + puts "graceful shutdown?" + end + + begin + rails.stop_serve + rescue + dbg "ERROR: #$!\r\n" + dbg $!.backtrace.join("\r\n") + end + end +rescue + dbg "ERROR: #$!\r\n" + dbg $!.backtrace.join("\r\n") end -- cgit v1.2.3-24-ge0c7