2011-06-25 16:59:34 +00:00
|
|
|
require 'fileutils'
|
|
|
|
|
2011-06-01 14:06:35 +00:00
|
|
|
module Guard
|
|
|
|
class RailsRunner
|
2011-06-01 14:42:53 +00:00
|
|
|
MAX_WAIT_COUNT = 10
|
|
|
|
|
2011-06-01 14:06:35 +00:00
|
|
|
attr_reader :options
|
|
|
|
|
|
|
|
def initialize(options)
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
|
|
|
kill_unmanaged_pid! if options[:force_run]
|
|
|
|
run_rails_command!
|
2011-06-29 13:47:08 +00:00
|
|
|
wait_for_pid
|
2011-06-01 14:06:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
if File.file?(pid_file)
|
2011-06-20 00:42:03 +00:00
|
|
|
system %{kill -KILL #{File.read(pid_file).strip}}
|
2011-06-29 13:47:08 +00:00
|
|
|
wait_for_no_pid if $?.exitstatus == 0
|
|
|
|
FileUtils.rm pid_file
|
2011-06-01 14:06:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def restart
|
|
|
|
stop
|
|
|
|
start
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_rails_command
|
|
|
|
rails_options = [
|
|
|
|
'-e', options[:environment],
|
|
|
|
'-p', options[:port],
|
|
|
|
'--pid', pid_file
|
|
|
|
]
|
|
|
|
|
|
|
|
rails_options << '-d' if options[:daemon]
|
|
|
|
|
|
|
|
%{sh -c 'cd #{Dir.pwd} && rails s #{rails_options.join(' ')} &'}
|
|
|
|
end
|
|
|
|
|
2011-06-01 14:28:32 +00:00
|
|
|
def pid_file
|
|
|
|
File.expand_path("tmp/pids/#{options[:environment]}.pid")
|
|
|
|
end
|
|
|
|
|
|
|
|
def pid
|
|
|
|
File.file?(pid_file) ? File.read(pid_file).to_i : nil
|
|
|
|
end
|
|
|
|
|
2011-06-01 14:42:53 +00:00
|
|
|
def sleep_time
|
|
|
|
options[:timeout].to_f / MAX_WAIT_COUNT.to_f
|
|
|
|
end
|
|
|
|
|
2011-06-01 14:06:35 +00:00
|
|
|
private
|
|
|
|
def run_rails_command!
|
|
|
|
system build_rails_command
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_pid?
|
|
|
|
File.file?(pid_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for_pid_action
|
2011-06-01 14:42:53 +00:00
|
|
|
sleep sleep_time
|
2011-06-01 14:06:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def kill_unmanaged_pid!
|
|
|
|
if pid = unmanaged_pid
|
2011-06-29 13:47:08 +00:00
|
|
|
system %{kill -KILL #{pid}}
|
2011-06-25 16:59:34 +00:00
|
|
|
FileUtils.rm pid_file
|
2011-06-29 13:47:08 +00:00
|
|
|
wait_for_no_pid
|
2011-06-01 14:06:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unmanaged_pid
|
2011-06-29 13:47:08 +00:00
|
|
|
%x{lsof -n -i TCP:#{options[:port]}}.each_line { |line|
|
2011-06-20 00:42:03 +00:00
|
|
|
if line["*:#{options[:port]} "]
|
|
|
|
return line.split("\s")[1]
|
|
|
|
end
|
|
|
|
}
|
2011-06-01 14:06:35 +00:00
|
|
|
nil
|
|
|
|
end
|
2011-06-29 13:47:08 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
def wait_for_pid
|
|
|
|
wait_for_pid_loop
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for_no_pid
|
|
|
|
wait_for_pid_loop(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_for_pid_loop(check_for_existince = true)
|
|
|
|
count = 0
|
|
|
|
while !(check_for_existince ? has_pid? : !has_pid?) && count < MAX_WAIT_COUNT
|
|
|
|
wait_for_pid_action
|
|
|
|
count += 1
|
|
|
|
end
|
|
|
|
!(count == MAX_WAIT_COUNT)
|
|
|
|
end
|
2011-06-01 14:06:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|