class Devserver::CommandManager

Attributes

environment[RW]
log_file[RW]
mode[RW]
pid_file[RW]
port[RW]
server[RW]

Public Class Methods

new(options = {}) click to toggle source

sets instance variables based on options, not limited to attr_accesor values

@param [Hash] options intialization options

# File lib/devserver/command_manager.rb, line 25
def initialize(options = {})
  options.keys.each do |key|
    self.instance_variable_set("@#{key.to_s}",options[key])
  end
end

Public Instance Methods

command(mode = self.mode) click to toggle source

returns command for @server and @mode (start, stop, debug)

@return [String] command for @server and @mode

# File lib/devserver/command_manager.rb, line 104
def command(mode = self.mode)
  case mode
  when 'start'
    self.start_command(mode)
  when 'debug'
    self.start_command(mode)
  when 'stop'
    self.stop_command
  else
    raise DevserverError, "Unrecognized mode: #{mode}"
  end
end
command_available?() click to toggle source

returns true/false if the gem corresponding to self.server is installed

@return [Boolean] whether the gem is installed or not

# File lib/devserver/command_manager.rb, line 120
def command_available?
  case self.server
  when 'passenger'
    gem_available?('passenger')
  when 'thin'
    gem_available?('thin')
  when 'mongrel'
    gem_available?('mongrel')
  else
    raise DevserverError, "Unrecognized web server: #{self.server}"
  end
end
determine_app_root() click to toggle source

Pretend that we are checking for rails by checking for config/boot.rb we'll even do something smart for ourselves by chdir .. if ../config/boot.rb exists. “smart for ourselves” is the operative phrase

@return [String] the current app root path if we think this is a rails dir, else nil

# File lib/devserver/command_manager.rb, line 36
def determine_app_root
  if(File.exist?('config/boot.rb'))
    return Dir.pwd
  elsif(File.exist?('../config/boot.rb'))
    Dir.chdir('..')
    return Dir.pwd
  else
    return nil
  end
end
is_port_open?() click to toggle source

check to see if anything is still running on @port

@return [Boolean] whether port responds or not

# File lib/devserver/command_manager.rb, line 136
def is_port_open?
  begin
    Timeout::timeout(1) do
      begin
        s = TCPSocket.new('127.0.0.1', self.port)
        s.close
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      end
    end
  rescue Timeout::Error
  end
  return false
end
start_command(mode = self.mode) click to toggle source

builds the start command for @server

@param [Boolean] mode mode to start in, either debug or start @return [String] start command

# File lib/devserver/command_manager.rb, line 72
def start_command(mode = self.mode)
  case self.server
  when 'passenger'
    "passenger start #{self.start_options(mode)}"
  when 'thin'
    "thin #{self.start_options(mode)} start"
  when 'mongrel'
    "mongrel_rails start #{self.start_options(mode)}"
  else
    raise DevserverError, "Unrecognized web server: #{self.server}"
  end
end
start_devserver(mode = self.mode) click to toggle source
# File lib/devserver/command_manager.rb, line 152
def start_devserver(mode = self.mode)
  if(self.command_available?)
    system("#{self.start_command(mode)}")
  end
end
start_options(mode = self.mode) click to toggle source

builds the command options with regard to the specified server

@param [Boolean] mode mode to start in, either debug or start @return [String] command options

# File lib/devserver/command_manager.rb, line 51
def start_options(mode = self.mode)
  common_options = "--port #{self.port} --environment #{self.environment}"
  if(mode == 'debug')
    common_options += " --debug"
  end
  case self.server
  when 'passenger'
    "#{common_options} --log-file #{self.log_file} --pid-file #{self.pid_file}"
  when 'thin'
    "#{common_options} --log #{self.log_file} --pid #{self.pid_file}"
  when 'mongrel'
    "#{common_options} --log #{self.log_file} --pid #{self.pid_file}"
  else
    nil
  end
end
stop_command() click to toggle source

builds the stop command for @server

@return [String] stop command

# File lib/devserver/command_manager.rb, line 88
def stop_command
  case self.server
  when 'passenger'
    "passenger stop --pid-file #{self.pid_file}"
  when 'thin'
    "thin --pid #{self.pid_file} stop"
  when 'mongrel'
    "mongrel_rails stop --pid #{self.pid_file}"
  else
    raise DevserverError, "Unrecognized web server: #{self.server}"
  end
end
stop_devserver() click to toggle source
# File lib/devserver/command_manager.rb, line 158
def stop_devserver
  if(self.command_available?)
    system("#{self.stop_command}")
  end
end

Private Instance Methods

gem_available?(name) click to toggle source
# File lib/devserver/command_manager.rb, line 166
def gem_available?(name)
  if Gem::Specification.methods.include?(:find_all_by_name)
    !Gem::Specification.find_all_by_name(name).empty?
  else
    Gem.available?(name)
  end
end