江边闲话集

09/17/2007

mongrel启动问题的解决方案

Filed under: 一技之长 — Tags: — 张太国 @ 16:19

如果使用Mongrel来部署Rails,就不可避免的使用

  1. mongrel_rails start -C mongrel.config  

这里mongrel.config是mongrel的配置文件,可以为:

  1. —  
  2.  :config_script:  
  3.  :environment: development  
  4.  :pid_file: log/mongrel.pid  
  5.  :num_processors: 1024  
  6.  :docroot: public  
  7.  :timeout: 0  
  8.  :host: 0.0.0.0  
  9.  :mime_map:  
  10.  :port: 3000  
  11.  :daemon: false  
  12.  :cwd: /home/linux/projects/mongrel/testapp  
  13.  :includes:  
  14.  – mongrel  
  15.  :debug: false  
  16.  :log_file: log/mongrel.log  

然后到app的根目录下运行上述的命令,恭喜您,你启动成功了.
但是有个问题,如果我不在app的根目录下运行呢?系统会给出我们信息:

  1. !!! Path to log file not valid: log/mongrel.log  
  2. mongrel::start reported an error. Use mongrel_rails mongrel::start -h to get help.  

第一眼看见这个错误,觉得非常简单,Mongrels的创始人给出的答案是将相对路径改为绝对路径.
也许上述的方法能解决问题,但是我相信仍然有一种情况:即使改为绝对路径,还是出现同样的错误.
如果发生上述的错误应该怎么办?
不幸的是,上述的问题我已经碰见过,很长时间不得要领.文档看破皮了,还是不能运行.不得已,打开mongrels的源代码看看.最后发现需要指定工作目录,因为工作目录是在log之前验证的.
摘录一段代码:

ruby 代码
  1. module Mongrel  
  2.   class Start < GemPlugin::Plugin "/commands"  
  3.     include Mongrel::Command::Base  
  4.   
  5.     def configure  
  6.       options [  
  7.         ["-e""–environment ENV""Rails environment to run as", :@environment, ENV[‘RAILS_ENV’] || "development"],  
  8.         ["-d""–daemonize""Run daemonized in the background", :@daemonfalse],  
  9.         [‘-p’, ‘–port PORT’, "Which port to bind to", :@port, 3000],  
  10.         [‘-a’, ‘–address ADDR’, "Address to bind to", :@address"0.0.0.0"],  
  11.         [‘-l’, ‘–log FILE’, "Where to write log messages", :@log_file"log/mongrel.log"],  
  12.         [‘-P’, ‘–pid FILE’, "Where to write the PID", :@pid_file"log/mongrel.pid"],  
  13.         [‘-n’, ‘–num-procs INT’, "Number of processors active before clients denied", :@num_procs, 1024],  
  14.         [‘-t’, ‘–timeout TIME’, "Timeout all requests after 100th seconds time", :@timeout, 0],  
  15.         [‘-m’, ‘–mime PATH’, "A YAML file that lists additional MIME types", :@mime_mapnil],  
  16.         [‘-c’, ‘–chdir PATH’, "Change to dir before starting (will be expanded)", :@cwdDir.pwd],  
  17.         [‘-r’, ‘–root PATH’, "Set the document root (default ‘public’)", :@docroot"public"],  
  18.         [‘-B’, ‘–debug’, "Enable debugging mode", :@debugfalse],  
  19.         [‘-C’, ‘–config PATH’, "Use a config file", :@config_filenil],  
  20.         [‘-S’, ‘–script PATH’, "Load the given file as an extra config script", :@config_scriptnil],  
  21.         [‘-G’, ‘–generate PATH’, "Generate a config file for use with -C", :@generatenil],  
  22.         [, ‘–user USER’, "User to run as", :@usernil],  
  23.         [, ‘–group GROUP’, "Group to run as", :@groupnil],  
  24.         [, ‘–prefix PATH’, "URL prefix for Rails app", :@prefixnil]  
  25.       ]  
  26.     end  
  27.   
  28.     def validate  
  29.       @cwd = File.expand_path(@cwd)  
  30.       valid_dir? @cwd"Invalid path to change to during daemon mode: #@cwd"  
  31.   
  32.       # Change there to start, then we’ll have to come back after daemonize  
  33.       Dir.chdir(@cwd)  
  34.   
  35.       valid?(@prefix[0].chr == "/" && @prefix[-1].chr != "/""Prefix must begin with / and not end in /"if @prefix  
  36.       valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file"  
  37.       valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file"  
  38.       valid_dir? @docroot"Path to docroot not valid: #@docroot"  
  39.       valid_exists? @mime_map"MIME mapping file does not exist: #@mime_map" if @mime_map  
  40.       valid_exists? @config_file"Config file not there: #@config_file" if @config_file  
  41.       valid_dir? File.dirname(@generate), "Problem accessing directory to #@generate" if @generate  
  42.       valid_user? @user if @user  
  43.       valid_group? @group if @group  
  44.   
  45.       return @valid  
  46.     end  
  47.   
  48.     def run  
  49.       # Config file settings will override command line settings  
  50.       settings = { :host => @address,  :port => @port:cwd => @cwd,  
  51.         :log_file => @log_file:pid_file => @pid_file:environment => @environment,  
  52.         :docroot => @docroot:mime_map => @mime_map:daemon => @daemon,  
  53.         :debug => @debug:includes => ["mongrel"], :config_script => @config_script,  
  54.         :num_processors => @num_procs:timeout => @timeout,  
  55.         :user => @user:group => @group:prefix => @prefix:config_file => @config_file  
  56.       }  
  57.   
  58.      #……  
  59.   
  60.       if @config_file  
  61.         settings.merge! YAML.load_file(@config_file)  
  62.         STDERR.puts "** Loading settings from #{@config_file} (they override command line)." unless settings[:daemon]  
  63.       end  
  64.   
  65.       config = Mongrel::Rails::RailsConfigurator.new(settings) do  
  66.         if defaults[:daemon]  
  67.           if File.exist? defaults[:pid_file]  
  68.             log "!!! PID file #{defaults[:pid_file]} already exists.  Mongrel could be running already.  Check your #{defaults[:log_file]} for errors."  
  69.             log "!!! Exiting with error.  You must stop mongrel and clear the .pid before I’ll attempt a start."  
  70.             exit 1  
  71.           end  
  72.   
  73.           daemonize  
  74.           log "Daemonized, any open files are closed.  Look at #{defaults[:pid_file]} and #{defaults[:log_file]} for info."  
  75.           log "Settings loaded from #{@config_file} (they override command line)." if @config_file  
  76.         end  

看看上面的代码就非常清楚了.
那么如何解决,可以这样:

  1. mongrel_rails  start -c youapp/path/to -C mongrel.config  

这样就OK了.

扩展应用:可以配置为服务自动启动.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress