lib/guard/rails/runner.rb in guard-rails-0.3.4 vs lib/guard/rails/runner.rb in guard-rails-0.4.0
- old
+ new
@@ -5,12 +5,12 @@
MAX_WAIT_COUNT = 10
attr_reader :options
def initialize(options)
- @env = ENV
@options = options
+ @root = options[:root] ? File.expand_path(options[:root]) : Dir.pwd
end
def start
kill_unmanaged_pid! if options[:force_run]
run_rails_command!
@@ -18,62 +18,81 @@
end
def stop
if File.file?(pid_file)
pid = File.read(pid_file).strip
- system %{kill -SIGINT #{pid}}
+ system "kill -SIGINT #{pid}"
wait_for_no_pid if $?.exitstatus == 0
# If you lost your pid_file, you are already died.
- system %{kill -KILL #{pid} >&2 2>/dev/null}
+ system "kill -KILL #{pid} >&2 2>/dev/null"
FileUtils.rm pid_file, :force => true
end
end
def restart
stop
start
end
- def build_rails_command
- return %{#{options[:CLI]} --pid #{pid_file}} if options[:CLI]
+ def build_command
+ command = build_cli_command if options[:CLI]
+ command ||= build_zeus_command if options[:zeus]
+ command ||= build_rails_command
+ "sh -c 'cd #{@root} && #{command} &'"
+ end
+ def pid_file
+ File.expand_path(options[:pid_file] || File.join(@root, "tmp/pids/#{options[:environment]}.pid"))
+ end
+
+ def pid
+ File.file?(pid_file) ? File.read(pid_file).to_i : nil
+ end
+
+ def sleep_time
+ options[:timeout].to_f / MAX_WAIT_COUNT.to_f
+ end
+
+ private
+
+ # command builders
+ def build_options
rails_options = [
options[:daemon] ? '-d' : nil,
options[:debugger] ? '-u' : nil,
'-e', options[:environment],
'--pid', pid_file,
'-p', options[:port],
options[:server],
]
+ rails_options.join(' ')
+ end
+
+ def build_cli_command
+ "#{options[:CLI]} --pid #{pid_file}"
+ end
+
+ def build_zeus_command
zeus_options = [
options[:zeus_plan] || 'server',
]
- # omit env when use zeus
- @env['RAILS_ENV'] = options[:environment] if options[:environment] && options[:zeus].nil?
- rails_runner = options[:zeus] ? "zeus #{zeus_options.join(' ')}" : "rails server"
-
- %{#{rails_runner} #{rails_options.join(' ')}}
+ # To avoid warning of Zeus
+ # Since setup RAILS_ENV is useless for Zeus
+ ENV['RAILS_ENV'] = nil
+ "zeus #{zeus_options.join(' ')} #{build_options}"
end
- def pid_file
- File.expand_path(options[:pid_file] || "tmp/pids/#{options[:environment]}.pid")
+ def build_rails_command
+ ENV['RAILS_ENV'] = options[:environment] if options[:environment]
+ "rails server #{build_options}"
end
- def pid
- File.file?(pid_file) ? File.read(pid_file).to_i : nil
- end
-
- def sleep_time
- options[:timeout].to_f / MAX_WAIT_COUNT.to_f
- end
-
- private
def run_rails_command!
- Process.spawn @env, build_rails_command
+ system build_command
end
def has_pid?
File.file?(pid_file)
end
@@ -82,26 +101,28 @@
sleep sleep_time
end
def kill_unmanaged_pid!
if pid = unmanaged_pid
- system %{kill -KILL #{pid}}
+ system "kill -KILL #{pid}"
FileUtils.rm pid_file
wait_for_no_pid
end
end
def unmanaged_pid
- %x{lsof -n -i TCP:#{options[:port]}}.each_line { |line|
+ file_list = `lsof -n -i TCP:#{options[:port]}`
+ file_list.each_line { |line|
if line["*:#{options[:port]} "]
return line.split("\s")[1]
end
}
nil
end
private
+
def wait_for_pid
wait_for_pid_loop
end
def wait_for_no_pid
@@ -116,6 +137,5 @@
end
!(count == MAX_WAIT_COUNT)
end
end
end
-