Included Modules

Cluster::ExecBase

Public Instance Methods

chdir_cwd() click to toggle source
# File lib/mongrel_cluster/init.rb, line 186
def chdir_cwd
  pwd = Dir.pwd
  Dir.chdir(@options["cwd"]) if @options["cwd"]     
  yield
  Dir.chdir(pwd) if @options["cwd"]
end
check_process(port) click to toggle source
# File lib/mongrel_cluster/init.rb, line 167
def check_process(port)
  if pid_file_exists?(port)
    pid = read_pid(port)
    ps_output = `ps -o #{cmd_name}= -p #{pid}`
    pid = ps_output =~ /mongrel_rails/ ? pid : nil
  else
    pid = find_pid(port)
  end
  pid
end
cmd_flags() click to toggle source
# File lib/mongrel_cluster/init.rb, line 182
def cmd_flags
  RUBY_PLATFORM =~ /solaris|aix/ ? "-eo" : "-ewwo"
end
cmd_name() click to toggle source
# File lib/mongrel_cluster/init.rb, line 178
def cmd_name
  RUBY_PLATFORM =~ /solaris|aix/ ? "args" : "command"
end
find_pid(port) click to toggle source
# File lib/mongrel_cluster/init.rb, line 202
def find_pid(port)
  ps_cmd = "ps #{cmd_flags} pid,#{cmd_name}"
  ps_output = `#{ps_cmd}`
  ps_output.each do |line|
    if line =~ /-P #{Regexp.escape(port_pid_file(port))} /
      pid = line.split[0]
      return pid
    end
  end
  nil
end
log(message) click to toggle source
# File lib/mongrel_cluster/init.rb, line 222
def log(message)
  puts message
end
log_error(message) click to toggle source
# File lib/mongrel_cluster/init.rb, line 214
def log_error(message)
  log(message)
end
log_verbose(message) click to toggle source
# File lib/mongrel_cluster/init.rb, line 218
def log_verbose(message)
  log(message) if @verbose
end
pid_file_exists?(port) click to toggle source
# File lib/mongrel_cluster/init.rb, line 158
def pid_file_exists?(port)    
  pid_file = port_pid_file(port)
  exists = false
  chdir_cwd do     
    exists = File.exists?(pid_file)  
  end
  exists
end
port_log_file(port) click to toggle source
# File lib/mongrel_cluster/init.rb, line 54
def port_log_file(port)
  log_file = [@log_file_base, port].join(".") +  @log_file_ext      
  File.join(@log_file_dir, log_file)
end
port_pid_file(port) click to toggle source
# File lib/mongrel_cluster/init.rb, line 49
def port_pid_file(port)
  pid_file = [@pid_file_base, port].join(".") + @pid_file_ext      
  File.join(@pid_file_dir, pid_file)
end
process_log_file(log_file) click to toggle source
# File lib/mongrel_cluster/init.rb, line 43
def process_log_file(log_file)
  @log_file_ext = File.extname(log_file)
  @log_file_base = File.basename(log_file, @log_file_ext)
  @log_file_dir = File.dirname(log_file)
end
process_pid_file(pid_file) click to toggle source
# File lib/mongrel_cluster/init.rb, line 37
def process_pid_file(pid_file)
  @pid_file_ext = File.extname(pid_file)
  @pid_file_base = File.basename(pid_file, @pid_file_ext)
  @pid_file_dir = File.dirname(pid_file)
end
read_options() click to toggle source
# File lib/mongrel_cluster/init.rb, line 17
def read_options
  @options = { 
    "environment" => ENV['RAILS_ENV'] || "development",
    "port" => 3000,
    "pid_file" => "tmp/pids/mongrel.pid",
    "log_file" => "log/mongrel.log",
    "servers" => 2
  }
  conf = YAML.load_file(@config_file)
  @options.merge! conf if conf
    
  process_pid_file @options["pid_file"]
  process_log_file @options["log_file"]

  start_port = end_port = @only
  start_port ||=  @options["port"].to_i
  end_port ||=  start_port + @options["servers"] - 1
  @ports = (start_port..end_port).to_a
end
read_pid(port) click to toggle source
# File lib/mongrel_cluster/init.rb, line 193
def read_pid(port)
  pid_file = port_pid_file(port)
  pid = 0
  chdir_cwd do     
    pid = File.read(pid_file)
  end
  pid
end
start() click to toggle source
# File lib/mongrel_cluster/init.rb, line 59
def start
  read_options
  
  argv = [ "mongrel_rails" ]
  argv << "start"
  argv << "-d"
  argv << "-e #{@options['environment']}" if @options['environment']
  argv << "-a #{@options['address']}"  if @options['address']
  argv << "-c #{@options['cwd']}" if @options['cwd']
  argv << "-o #{@options['timeout']}" if @options['timeout']
  argv << "-t #{@options['throttle']}" if @options['throttle']
  argv << "-m #{@options['mime_map']}" if @options['mime_map']
  argv << "-r #{@options['docroot']}" if @options['docroot']
  argv << "-n #{@options['num_procs']}" if @options['num_procs']
  argv << "-B" if @options['debug']
  argv << "-S #{@options['config_script']}" if @options['config_script']
  argv << "--user #{@options['user']}" if @options['user']
  argv << "--group #{@options['group']}" if @options['group']
  argv << "--prefix #{@options['prefix']}" if @options['prefix']
  cmd = argv.join " "
  
  @ports.each do |port|              
    if @clean && pid_file_exists?(port) && !check_process(port)
      pid_file = port_pid_file(port)        
      log "missing process: removing #{pid_file}"
      chdir_cwd do
        File.unlink(pid_file) 
      end
    end
    
    if pid_file_exists?(port) && check_process(port)
      log "already started port #{port}"         
      next
    end

    exec_cmd = cmd + " -p #{port} -P #{port_pid_file(port)}"
    exec_cmd += " -l #{port_log_file(port)}"
    log "starting port #{port}"          
    log_verbose exec_cmd
    output = `#{exec_cmd}`
    log_error output unless $?.success?
  end
end
status() click to toggle source
# File lib/mongrel_cluster/init.rb, line 133
def status
  read_options
  
  status = STATUS_OK
 
  @ports.each do |port|
    pid = check_process(port)        
    unless pid_file_exists?(port)        
      log "missing pid_file: #{port_pid_file(port)}"  
      status = STATUS_ERROR
    else
      log "found pid_file: #{port_pid_file(port)}"
    end    
    if pid
      log "found mongrel_rails: port #{port}, pid #{pid}"
    else
      log "missing mongrel_rails: port #{port}"
      status = STATUS_ERROR
    end
    puts ""
  end

  status
end
stop() click to toggle source
# File lib/mongrel_cluster/init.rb, line 103
def stop
  read_options

  argv = [ "mongrel_rails" ]
  argv << "stop"
  argv << "-c #{@options["cwd"]}" if @options["cwd"]
  argv << "-f" if @force
  cmd = argv.join " "

  @ports.each do |port|
    pid = check_process(port)        
    if @clean && pid && !pid_file_exists?(port)       
      log "missing pid_file: killing mongrel_rails port #{port}, pid #{pid}"
      Process.kill("KILL", pid.to_i)  
    end
    
    if !check_process(port)
      log "already stopped port #{port}"                   
      next       
    end

    exec_cmd = cmd + " -P #{port_pid_file(port)}"
    log "stopping port #{port}"          
    log_verbose exec_cmd
    output = `#{exec_cmd}`
    log_error output unless $?.success?
    
  end
end
validate() click to toggle source
# File lib/mongrel_cluster/init.rb, line 12
def validate
  valid_exists?(@config_file, "Configuration file does not exist. Run mongrel_rails cluster::configure.")
  @valid
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.