Class: Devserver::CommandManager

Inherits:
Object
  • Object
show all
Defined in:
lib/devserver/command_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CommandManager

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

Parameters:

  • options (Hash) (defaults to: {})

    intialization options



25
26
27
28
29
# 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

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



20
21
22
# File 'lib/devserver/command_manager.rb', line 20

def environment
  @environment
end

#log_fileObject

Returns the value of attribute log_file.



20
21
22
# File 'lib/devserver/command_manager.rb', line 20

def log_file
  @log_file
end

#modeObject

Returns the value of attribute mode.



20
21
22
# File 'lib/devserver/command_manager.rb', line 20

def mode
  @mode
end

#pid_fileObject

Returns the value of attribute pid_file.



20
21
22
# File 'lib/devserver/command_manager.rb', line 20

def pid_file
  @pid_file
end

#portObject

Returns the value of attribute port.



20
21
22
# File 'lib/devserver/command_manager.rb', line 20

def port
  @port
end

#serverObject

Returns the value of attribute server.



20
21
22
# File 'lib/devserver/command_manager.rb', line 20

def server
  @server
end

Instance Method Details

#command(mode = self.mode) ⇒ String

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

Returns:

  • (String)

    command for @server and @mode



104
105
106
107
108
109
110
111
112
113
114
115
# 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?Boolean

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

Returns:

  • (Boolean)

    whether the gem is installed or not



120
121
122
123
124
125
126
127
128
129
130
131
# 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_rootString

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

Returns:

  • (String)

    the current app root path if we think this is a rails dir, else nil



36
37
38
39
40
41
42
43
44
45
# 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?Boolean

check to see if anything is still running on @port

Returns:

  • (Boolean)

    whether port responds or not



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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) ⇒ String

builds the start command for @server

Parameters:

  • mode (Boolean) (defaults to: self.mode)

    mode to start in, either debug or start

Returns:

  • (String)

    start command



72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object



152
153
154
155
156
# 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) ⇒ String

builds the command options with regard to the specified server

Parameters:

  • mode (Boolean) (defaults to: self.mode)

    mode to start in, either debug or start

Returns:

  • (String)

    command options



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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_commandString

builds the stop command for @server

Returns:

  • (String)

    stop command



88
89
90
91
92
93
94
95
96
97
98
99
# 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_devserverObject



158
159
160
161
162
# File 'lib/devserver/command_manager.rb', line 158

def stop_devserver
  if(self.command_available?)
    system("#{self.stop_command}")
  end
end