Module: Cluster::ExecBase

Includes:
Mongrel::Command::Base
Included in:
Configure, Restart, Start, Status, Stop
Defined in:
lib/mongrel_cluster/init.rb

Constant Summary collapse

STATUS_OK =
0
STATUS_ERROR =
2

Instance Method Summary collapse

Instance Method Details

#chdir_cwdObject



186
187
188
189
190
191
# 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) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# 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_flagsObject



182
183
184
# File 'lib/mongrel_cluster/init.rb', line 182

def cmd_flags
  RUBY_PLATFORM =~ /solaris|aix/i ? "-eo" : "-ewwo"
end

#cmd_nameObject



178
179
180
# File 'lib/mongrel_cluster/init.rb', line 178

def cmd_name
  RUBY_PLATFORM =~ /solaris|aix/i ? "args" : "command"
end

#find_pid(port) ⇒ Object



202
203
204
205
206
207
208
209
210
211
# 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
end

#log(message) ⇒ Object



221
222
223
# File 'lib/mongrel_cluster/init.rb', line 221

def log(message)
  puts message
end

#log_error(message) ⇒ Object



213
214
215
# File 'lib/mongrel_cluster/init.rb', line 213

def log_error(message)
  log(message)
end

#log_verbose(message) ⇒ Object



217
218
219
# File 'lib/mongrel_cluster/init.rb', line 217

def log_verbose(message)
  log(message) if @verbose
end

#pid_file_exists?(port) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
161
162
163
164
165
# 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) ⇒ Object



54
55
56
57
# 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) ⇒ Object



49
50
51
52
# 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) ⇒ Object



43
44
45
46
47
# 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) ⇒ Object



37
38
39
40
41
# 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_optionsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



193
194
195
196
197
198
199
200
# 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

#startObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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

#statusObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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

#stopObject



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
# 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

#validateObject



12
13
14
15
# 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