This commit is contained in:
John Bintz 2011-06-01 10:42:53 -04:00
parent f27e753caa
commit a7678ef54b
4 changed files with 23 additions and 6 deletions

View File

@ -5,13 +5,14 @@ Want to restart your Rails development server whilst you work? Now you can!
watch(%r{^(config|lib)/.*}) watch(%r{^(config|lib)/.*})
end end
Five fun options! Lots of fun options!
* `:port` is the port number to run on (default `3000`) * `:port` is the port number to run on (default `3000`)
* `:environment` is the environment to use (default `development`) * `:environment` is the environment to use (default `development`)
* `:start_on_start` will start the server when starting Guard (default `true`) * `:start_on_start` will start the server when starting Guard (default `true`)
* `:force_run` kills any process that's holding open the listen port before attempting to (re)start Rails (default `false`). * `:force_run` kills any process that's holding open the listen port before attempting to (re)start Rails (default `false`).
* `:deamon` runs the server as a daemon, without any output to the terminal that ran `guard` (default `false`). * `:deamon` runs the server as a daemon, without any output to the terminal that ran `guard` (default `false`).
* `:timeout` waits this number of seconds when restarting the Rails server before reporting there's a problem (default `20`).
This is super-alpha, but it works for me! Only really hand-tested in Mac OS X. Feel free to fork'n'fix for other This is super-alpha, but it works for me! Only really hand-tested in Mac OS X. Feel free to fork'n'fix for other
OSes, and to add some more real tests. OSes, and to add some more real tests.

View File

@ -13,7 +13,8 @@ module Guard
:port => 3000, :port => 3000,
:environment => 'development', :environment => 'development',
:start_on_start => true, :start_on_start => true,
:force_run => false :force_run => false,
:timeout => 20
}.merge(options) }.merge(options)
@runner = RailsRunner.new(@options) @runner = RailsRunner.new(@options)

View File

@ -1,5 +1,7 @@
module Guard module Guard
class RailsRunner class RailsRunner
MAX_WAIT_COUNT = 10
attr_reader :options attr_reader :options
def initialize(options) def initialize(options)
@ -10,11 +12,11 @@ module Guard
kill_unmanaged_pid! if options[:force_run] kill_unmanaged_pid! if options[:force_run]
run_rails_command! run_rails_command!
count = 0 count = 0
while !has_pid? && count < 10 while !has_pid? && count < MAX_WAIT_COUNT
wait_for_pid_action wait_for_pid_action
count += 1 count += 1
end end
!(count == 10) !(count == MAX_WAIT_COUNT)
end end
def stop def stop
@ -48,6 +50,10 @@ module Guard
File.file?(pid_file) ? File.read(pid_file).to_i : nil File.file?(pid_file) ? File.read(pid_file).to_i : nil
end end
def sleep_time
options[:timeout].to_f / MAX_WAIT_COUNT.to_f
end
private private
def run_rails_command! def run_rails_command!
system build_rails_command system build_rails_command
@ -58,7 +64,7 @@ module Guard
end end
def wait_for_pid_action def wait_for_pid_action
sleep 2 sleep sleep_time
end end
def kill_unmanaged_pid! def kill_unmanaged_pid!

View File

@ -86,7 +86,7 @@ describe Guard::RailsRunner do
before do before do
pid_stub.returns(false) pid_stub.returns(false)
kill_expectation.never kill_expectation.never
runner.expects(:wait_for_pid_action).times(10) runner.expects(:wait_for_pid_action).times(Guard::RailsRunner::MAX_WAIT_COUNT)
end end
it "should act properly" do it "should act properly" do
@ -94,4 +94,13 @@ describe Guard::RailsRunner do
end end
end end
end end
describe '#sleep_time' do
let(:timeout) { 30 }
let(:options) { default_options.merge(:timeout => timeout) }
it "should adjust the sleep time as necessary" do
runner.sleep_time.should == (timeout.to_f / Guard::RailsRunner::MAX_WAIT_COUNT.to_f)
end
end
end end