Module: Cow_Cluster::Base
Instance Method Summary collapse
- #close_rails_connections ⇒ Object
- #each_port(settings, &block) ⇒ Object
- #get_unbound_port ⇒ Object
- #load_settings ⇒ Object
- #open_rails_connections ⇒ Object
- #port_log_file(port) ⇒ Object
- #port_pid_file(port) ⇒ Object
- #rails_preload(settings) ⇒ Object
- #read_options(settings) ⇒ Object
- #reopen_child_log(settings) ⇒ Object
- #restart_run(restart = false) ⇒ Object
- #spawn_child(settings, port) ⇒ Object
- #start_options(restart = false) ⇒ Object
- #stop_child(settings, port, force = @force) ⇒ Object
- #stop_options ⇒ Object
- #validate_start_options ⇒ Object
- #validate_stop_options ⇒ Object
- #wait_for_child_ready(address, port) ⇒ Object
-
#write_child_pid(settings) ⇒ Object
restart_run.
Instance Method Details
#close_rails_connections ⇒ Object
308 309 310 311 |
# File 'lib/mongrel_cow_cluster/init.rb', line 308 def close_rails_connections ActiveRecord::Base.connection.disconnect! CACHE.reset if defined?(CACHE) && CACHE.respond_to?(:reset) end |
#each_port(settings, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mongrel_cow_cluster/init.rb', line 40 def each_port(settings, &block) if settings[:only] yield settings[:only] else max_port = settings[:port] + settings[:servers] - 1 (settings[:port]..max_port).each { |port| yield port } end end |
#get_unbound_port ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/mongrel_cow_cluster/init.rb', line 263 def get_unbound_port tmp_port = tmp_socket = nil while ! tmp_socket do begin tmp_port = rand(32768) + 32768 tmp_socket = TCPServer.new('127.0.0.1', tmp_port) rescue Errno::EADDRINUSE STDERR.puts "Warning: failed temporarily bind to random port: " + "#{tmp_port}, trying another..." retry end end tmp_socket.close tmp_port end |
#load_settings ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/mongrel_cow_cluster/init.rb', line 184 def load_settings # Config file settings will override command line settings settings = { :host => @address, :port => @port, :cwd => @cwd, :log_file => @log_file, :pid_file => @pid_file, :environment => @environment, :docroot => @docroot, :mime_map => @mime_map, :debug => @debug, :includes => ["mongrel"], :config_script => @config_script, :num_processors => @num_procs, :timeout => @timeout, :servers => @servers, :user => @user, :group => @group, :prefix => @prefix, :only => @only, :rolling_interval => @rolling_interval, :config_file => @config_file } (settings) settings end |
#open_rails_connections ⇒ Object
313 314 315 |
# File 'lib/mongrel_cow_cluster/init.rb', line 313 def open_rails_connections ActiveRecord::Base.establish_connection end |
#port_log_file(port) ⇒ Object
56 57 58 59 |
# File 'lib/mongrel_cow_cluster/init.rb', line 56 def port_log_file(port) base = "#{@file[:log][:base]}.#{port}#{@file[:log][:ext]}" File.join(@file[:log][:dir], base) end |
#port_pid_file(port) ⇒ Object
51 52 53 54 |
# File 'lib/mongrel_cow_cluster/init.rb', line 51 def port_pid_file(port) base = "#{@file[:pid][:base]}.#{port}#{@file[:pid][:ext]}" File.join(@file[:pid][:dir], base) end |
#rails_preload(settings) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/mongrel_cow_cluster/init.rb', line 279 def rails_preload(settings) GC.start # start a random, temporary alternate port settings = settings.dup settings[:host] = '127.0.0.1' settings[:port] = get_unbound_port config = Mongrel::Rails::RailsConfigurator.new(settings) do mime = {} if defaults[:mime_map] log "Loading additional MIME types from #{defaults[:mime_map]}" mime = load_mime_map(defaults[:mime_map], mime) end listener do uri defaults[:prefix] || "/", :handler => rails(:mime => mime, :prefix => defaults[:prefix]) if defaults[:config_script] log "Loading #{defaults[:config_script]} external config script" run_config(defaults[:config_script]) end log "Loading any Rails specific GemPlugins" load_plugins log "Rails loaded." end end # RailsConfigurator.new config end |
#read_options(settings) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mongrel_cow_cluster/init.rb', line 16 def (settings) if @config_file settings.merge!(config_settings = YAML.load_file(@config_file)) # symbolize keys, mongrel_cluster uses string keys :( keys = settings.keys.collect { |k| k.kind_of?(Symbol) ? nil : k } keys.compact.each { |k| settings[k.to_sym] = settings.delete(k) } STDERR.puts "** Loading settings from #{@config_file} " + "(they override command line)." if settings[:address] && ! config_settings[:host] settings[:host] = settings[:address] end settings.each { |k,v| instance_variable_set("@#{k.to_s}", v) } end @file = { :pid => {}, :log => {} } @file.keys.each { |f| x = "#{f.to_s}_file".to_sym next unless settings[x] @file[f][:ext] = File.extname(settings[x]) @file[f][:base] = File.basename(settings[x], @file[f][:ext]) @file[f][:dir] = File.dirname(settings[x]) } end |
#reopen_child_log(settings) ⇒ Object
255 256 257 258 259 260 261 |
# File 'lib/mongrel_cow_cluster/init.rb', line 255 def reopen_child_log(settings) STDIN.reopen '/dev/null' rescue nil STDOUT.reopen(settings[:log_file], 'a') STDOUT.sync = true STDERR.reopen(settings[:log_file], 'a') STDERR.sync = true end |
#restart_run(restart = false) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/mongrel_cow_cluster/init.rb', line 202 def restart_run(restart = false) settings = load_settings settings.merge(:force => @force, :wait => @wait) if restart config = rails_preload(settings) ObjectSpace.each_object(TCPServer) { |x| x.close rescue nil } close_rails_connections pid = fork { File.umask 0000 Process.setsid trap 'HUP', 'IGNORE' Dir.chdir(@cwd) ri = nil each_port(settings) { |port| if restart && settings[:rolling_interval] if ri STDERR.puts "Delaying next restart by #{ri} seconds " + "(rolling-interval)" sleep(ri) else ri = settings[:rolling_interval].to_f end end GC.start if restart stop_child(settings, port) GC.start end spawn_child(settings, port) } } Process.waitpid(pid) exit 0 end |
#spawn_child(settings, port) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/mongrel_cow_cluster/init.rb', line 337 def spawn_child(settings, port) fork { GC.start settings[:port] = port settings[:pid_file] = port_pid_file(port) settings[:log_file] = port_log_file(port) write_child_pid(settings) reopen_child_log(settings) $0 << " -p #{port}" open_rails_connections config = Mongrel::Rails::RailsConfigurator.new(settings) do if defaults[:debug] log "Installing debugging prefixed filters. " + "Look in log/mongrel_debug for the files." debug "/" end listener do uri defaults[:prefix] || "/", :handler => rails if defaults[:config_script] log "Loading #{defaults[:config_script]} external config script" run_config(defaults[:config_script]) end setup_rails_signals end # listener end # config config.log "Mongrel available at #{settings[:host]}:#{port}" config.run GC.start config.join exit 0 } # fork wait_for_child_ready(settings[:host], port) rescue Object => err STDERR.puts "Failed to spawn child on port #{port}: #{err.inspect}" end |
#start_options(restart = false) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mongrel_cow_cluster/init.rb', line 89 def (restart = false) ret = [ ["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"], ['-p', '--port INT', "First port to bind to", :@port, 3000], ['-s', '--servers INT', "Number of servers to start", :@servers, 2], ['-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 processors active before clients denied", :@num_procs, 1024], ['-t', '--timeout TIME', "Timeout all requests after seconds time", :@timeout, 60], ['-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"], ['-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], ['-o', '--only INT', 'Limit operation to only one port', :@only, 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] ] if restart seen = {} ret.each { |o| seen[o[1]] = true } .each { |o| ret << o unless seen[o[1]] } ret << [ '-i', '--rolling-interval SECONDS', "Seconds to delay between each child restart", :@rolling_interval, nil] end ret end |
#stop_child(settings, port, force = @force) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/mongrel_cow_cluster/init.rb', line 160 def stop_child(settings, port, force = @force) pid_file = port_pid_file(port) if File.exist?(pid_file) signal = force ? 'KILL' : 'TERM' pid = File.open(pid_file).read.to_i STDERR.print "Sending #{signal} to Mongrel at PID #{pid}..." begin Process.kill(signal, pid) rescue Errno::ESRCH STDERR.puts 'Process does not exist. Not running.' File.unlink(pid_file) end if force File.unlink(pid_file) rescue nil else @wait.to_i.times { |nr| sleep(1) if File.exist?(pid_file) } end STDERR.puts 'Done.' else STDERR.puts "#{pid_file} not found, skipping." end end |
#stop_options ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mongrel_cow_cluster/init.rb', line 61 def [ ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd], ['-C', '--config PATH', "Use a config file", :@config_file, nil], ['-f', '--force', "Force the shutdown (kill -9).", :@force, false], ['-o', '--only INT', 'Limit operation to only one port', :@only, nil ], ['-w', '--wait SECONDS', "Wait SECONDS before forcing shutdown", :@wait, "10"], ['-p', '--port INT', "First port to bind to", :@port, 3000], ['-s', '--servers INT', "Number of servers to start", :@servers, 2], ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"], ] end |
#validate_start_options ⇒ Object
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 |
# File 'lib/mongrel_cow_cluster/init.rb', line 134 def if @config_file valid_exists? @config_file, "Config file not there: #@config_file" load_settings 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].chr == "/" && @prefix[-1].chr != "/", "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_user? @user if @user valid_group? @group if @group return @valid end |
#validate_stop_options ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mongrel_cow_cluster/init.rb', line 77 def if @config_file valid_exists? @config_file, "Config file not there: #@config_file" load_settings end @cwd = File.(@cwd) valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd" Dir.chdir @cwd @valid end |
#wait_for_child_ready(address, port) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/mongrel_cow_cluster/init.rb', line 317 def wait_for_child_ready(address, port) sleep 1 tries = 10 begin http = Net::HTTP.new(address, port) http.start ret = http.get('/') STDERR.puts "http://#{address}:#{port} ready: #{ret}" rescue Errno::ECONNREFUSED => err if (tries -= 1) > 0 STDERR.puts "Waiting for http://#{address}:#{port} to respond, " + "#{tries} tries left" sleep 1 retry else throw err end end end |
#write_child_pid(settings) ⇒ Object
restart_run
240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/mongrel_cow_cluster/init.rb', line 240 def write_child_pid(settings) pid_file = settings[:pid_file] if File.exist?(pid_file) STDERR.puts "!!! PID file #{pid_file} already exists." + " Mongrel could be running already. " + "Check your #{settings[:log_file]} for errors." STDERR.puts "!!! Exiting with error. " + "You must stop mongrel and clear the .pid " + "before I'll attempt a start." exit 1 end File.open(pid_file, 'w') { |fp| fp.puts $$.to_s } end |