lib/guard/rails/runner.rb in guard-rails-0.0.2 vs lib/guard/rails/runner.rb in guard-rails-0.0.3
- old
+ new
@@ -1,5 +1,7 @@
+require 'fileutils'
+
module Guard
class RailsRunner
MAX_WAIT_COUNT = 10
attr_reader :options
@@ -9,21 +11,18 @@
end
def start
kill_unmanaged_pid! if options[:force_run]
run_rails_command!
- count = 0
- while !has_pid? && count < MAX_WAIT_COUNT
- wait_for_pid_action
- count += 1
- end
- !(count == MAX_WAIT_COUNT)
+ wait_for_pid
end
def stop
if File.file?(pid_file)
- system %{kill -INT #{File.read(pid_file).strip}}
+ system %{kill -KILL #{File.read(pid_file).strip}}
+ wait_for_no_pid if $?.exitstatus == 0
+ FileUtils.rm pid_file
end
end
def restart
stop
@@ -36,10 +35,11 @@
'-p', options[:port],
'--pid', pid_file
]
rails_options << '-d' if options[:daemon]
+ rails_options << options[:server] if options[:server]
%{sh -c 'cd #{Dir.pwd} && rails s #{rails_options.join(' ')} &'}
end
def pid_file
@@ -67,22 +67,40 @@
sleep sleep_time
end
def kill_unmanaged_pid!
if pid = unmanaged_pid
- system %{kill -INT #{pid}}
+ system %{kill -KILL #{pid}}
+ FileUtils.rm pid_file
+ wait_for_no_pid
end
end
def unmanaged_pid
- if RbConfig::CONFIG['host_os'] =~ /darwin/
- %x{lsof -P}.each_line { |line|
- if line["*:#{options[:port]} "]
- return line.split("\s")[1]
- end
- }
- end
+ %x{lsof -n -i TCP:#{options[:port]}}.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
+ 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
end
end