Class: Rails::MongrelServer::Start
- Includes:
- Mongrel::Command::Base
- Defined in:
- lib/rails/mongrel_server/commands.rb
Instance Method Summary collapse
- #config_keys ⇒ Object
- #configure ⇒ Object
- #load_config ⇒ Object
- #run ⇒ Object
- #settings ⇒ Object
- #start_debugger ⇒ Object
- #tail(log_file) ⇒ Object
- #validate ⇒ Object
Instance Method Details
#config_keys ⇒ Object
235 236 237 238 |
# File 'lib/rails/mongrel_server/commands.rb', line 235 def config_keys @config_keys ||= %w(address host port cwd log_file pid_file environment docroot mime_map daemon debug includes config_script num_processors timeout throttle user group prefix) end |
#configure ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rails/mongrel_server/commands.rb', line 55 def configure [ ["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"], ["-d", "--daemonize", "Run daemonized in the background", :@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, "tmp/pids/mongrel.pid"], ['-n', '--num-procs INT', "Number of processors active before clients denied", :@num_procs, 1024], ['-o', '--timeout TIME', "Time to wait (in seconds) before killing a stalled thread", :@timeout, 60], ['-t', '--throttle TIME', "Time to pause (in hundredths of a second) between accepting clients", :@throttle, 0], ['-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, RAILS_ROOT], ['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"], ['-B', '--debug', "Enable debugging mode", :@debug, false], ['-C', '--config PATH', "Use a config file", :@config_file, nil], ['-S', '--script PATH', "Load the given file as an extra config script", :@config_script, nil], ['-G', '--generate PATH', "Generate a config file for use with -C", :@generate, nil], ['', '--user USER', "User to run as", :@user, nil], ['', '--group GROUP', "Group to run as", :@group, nil], ['', '--prefix PATH', "URL prefix for Rails app", :@prefix, nil], ['-b', '--binding ADDR', "Address to bind to (deprecated, use -a)", :@address, "0.0.0.0"], ['-u', '--debugger', "Enable debugging mode (deprecated, use -B)", :@debug, false] ] end |
#load_config ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/rails/mongrel_server/commands.rb', line 212 def load_config settings = {} begin settings = YAML.load_file(@config_file) ensure Mongrel.log(:error, "** Loading settings from #{@config_file} (they override command line).") unless @daemon || settings[:daemon] end settings[:includes] ||= ["mongrel"] # Config file settings will override command line settings settings.each do |key, value| key = key.to_s if config_keys.include?(key) key = 'address' if key == 'host' self.instance_variable_set("@#{key}", value) else failure "Unknown configuration setting: #{key}" @valid = false end end end |
#run ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rails/mongrel_server/commands.rb', line 110 def run if @generate @generate = File.(@generate) Mongrel.log(:error, "** Writing config to \"#@generate\".") open(@generate, "w") {|f| f.write(settings.to_yaml) } Mongrel.log(:error, "** Finished. Run \"mongrel_rails start -C #@generate\" to use the config file.") exit 0 end config = RailsConfigurator.new(settings) do defaults[:log] = $stdout if defaults[:environment] == 'development' Mongrel.log("=> Enhanced Rails #{Rails.version} application starting on http://#{defaults[:host]}:#{defaults[:port]}") unless defaults[:daemon] Mongrel.log("=> Call with -d to detach") Mongrel.log("=> Ctrl-C to shutdown server") start_debugger if defaults[:debug] end if defaults[:daemon] if File.exist? defaults[:pid_file] Mongrel.log(:error, "!!! PID file #{defaults[:pid_file]} already exists. Mongrel could be running already. Check your #{defaults[:log_file]} for errors.") Mongrel.log(:error, "!!! Exiting with error. You must stop mongrel and clear the .pid before I'll attempt a start.") exit 1 end daemonize Mongrel.log("Daemonized, any open files are closed. Look at #{defaults[:pid_file]} and #{defaults[:log_file]} for info.") Mongrel.log("Settings loaded from #{@config_file} (they override command line).") if @config_file end Mongrel.log("Starting Mongrel listening at #{defaults[:host]}:#{defaults[:port]}, further information can be found in log/mongrel-#{defaults[:host]}-#{defaults[:port]}.log") listener do prefix = defaults[:prefix] || '/' if defaults[:debug] Mongrel.log("Installing debugging prefixed filters. Look in log/mongrel_debug for the files.") debug(prefix) end setup_mime_types dir_handler = Mongrel::DirHandler.new(defaults[:docroot], false) dir_handler.passthrough_missing_files = true unless defaults[:environment] == 'production' Mongrel.log("Mounting DirHandler at #{prefix}...") uri prefix, :handler => dir_handler end Mongrel.log("Starting Rails with #{defaults[:environment]} environment...") Mongrel.log("Mounting Rails at #{prefix}...") mount_rails(prefix) Mongrel.log("Rails loaded.") Mongrel.log("Loading any Rails specific GemPlugins" ) load_plugins if defaults[:config_script] Mongrel.log("Loading #{defaults[:config_script]} external config script") run_config(defaults[:config_script]) end setup_signals end end config.run Mongrel.log("Mongrel #{Mongrel::Const::MONGREL_VERSION} available at #{@address}:#{@port}") if config.defaults[:daemon] config.write_pid_file else Mongrel.log("Use CTRL-C to stop.") tail "log/#{config.defaults[:environment]}.log" end config.join if config.needs_restart unless RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ cmd = "ruby #{__FILE__} start #{original_args.join(' ')}" Mongrel.log("Restarting with arguments: #{cmd}") config.stop(false, true) config.remove_pid_file if config.defaults[:daemon] system cmd else Mongrel.log(:error, "Can't restart unless in daemon mode.") exit 1 end else Mongrel.log("Win32 does not support restarts. Exiting.") end end end |
#settings ⇒ Object
240 241 242 243 244 245 246 247 |
# File 'lib/rails/mongrel_server/commands.rb', line 240 def settings config_keys.inject({}) do |hash, key| value = self.instance_variable_get("@#{key}") key = 'host' if key == 'address' hash[key.to_sym] ||= value hash end end |
#start_debugger ⇒ Object
249 250 251 252 253 254 255 256 257 |
# File 'lib/rails/mongrel_server/commands.rb', line 249 def start_debugger require_library_or_gem 'ruby-debug' Debugger.start Debugger.settings[:autoeval] = true if Debugger.respond_to?(:settings) Mongrel.log("=> Debugger enabled") rescue Exception Mongrel.log(:error, "You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'") exit end |
#tail(log_file) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/rails/mongrel_server/commands.rb', line 259 def tail(log_file) cursor = File.size(log_file) last_checked = Time.now tail_thread = Thread.new do File.open(log_file, 'r') do |f| loop do f.seek cursor if f.mtime > last_checked last_checked = f.mtime contents = f.read cursor += contents.length print contents end sleep 1 end end end tail_thread end |
#validate ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rails/mongrel_server/commands.rb', line 82 def validate if @config_file valid_exists?(@config_file, "Config file not there: #@config_file") return false unless @valid @config_file = File.(@config_file) load_config return false unless @valid end @cwd = File.(@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?(@prefix[0] == ?/ && @prefix[-1] != ?/, "Prefix must begin with / and not end in /") if @prefix 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 valid_exists? @config_file, "Config file not there: #@config_file" if @config_file valid_dir? File.dirname(@generate), "Problem accessing directory to #@generate" if @generate valid_user? @user if @user valid_group? @group if @group return @valid end |