From a7678ef54b9b40de1cfe394c149d4a3711fd34f9 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 1 Jun 2011 10:42:53 -0400 Subject: [PATCH] timeout --- README.md | 3 ++- lib/guard/rails.rb | 3 ++- lib/guard/rails/runner.rb | 12 +++++++++--- spec/lib/guard/rails/runner_spec.rb | 11 ++++++++++- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 32ca982..1c3060a 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,14 @@ Want to restart your Rails development server whilst you work? Now you can! watch(%r{^(config|lib)/.*}) end -Five fun options! +Lots of fun options! * `:port` is the port number to run on (default `3000`) * `:environment` is the environment to use (default `development`) * `: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`). * `: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 OSes, and to add some more real tests. diff --git a/lib/guard/rails.rb b/lib/guard/rails.rb index 420f688..4d87c26 100644 --- a/lib/guard/rails.rb +++ b/lib/guard/rails.rb @@ -13,7 +13,8 @@ module Guard :port => 3000, :environment => 'development', :start_on_start => true, - :force_run => false + :force_run => false, + :timeout => 20 }.merge(options) @runner = RailsRunner.new(@options) diff --git a/lib/guard/rails/runner.rb b/lib/guard/rails/runner.rb index a5633ce..1a9a37c 100644 --- a/lib/guard/rails/runner.rb +++ b/lib/guard/rails/runner.rb @@ -1,5 +1,7 @@ module Guard class RailsRunner + MAX_WAIT_COUNT = 10 + attr_reader :options def initialize(options) @@ -10,11 +12,11 @@ module Guard kill_unmanaged_pid! if options[:force_run] run_rails_command! count = 0 - while !has_pid? && count < 10 + while !has_pid? && count < MAX_WAIT_COUNT wait_for_pid_action count += 1 end - !(count == 10) + !(count == MAX_WAIT_COUNT) end def stop @@ -48,6 +50,10 @@ module Guard 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! system build_rails_command @@ -58,7 +64,7 @@ module Guard end def wait_for_pid_action - sleep 2 + sleep sleep_time end def kill_unmanaged_pid! diff --git a/spec/lib/guard/rails/runner_spec.rb b/spec/lib/guard/rails/runner_spec.rb index d439c45..2280cf4 100644 --- a/spec/lib/guard/rails/runner_spec.rb +++ b/spec/lib/guard/rails/runner_spec.rb @@ -86,7 +86,7 @@ describe Guard::RailsRunner do before do pid_stub.returns(false) kill_expectation.never - runner.expects(:wait_for_pid_action).times(10) + runner.expects(:wait_for_pid_action).times(Guard::RailsRunner::MAX_WAIT_COUNT) end it "should act properly" do @@ -94,4 +94,13 @@ describe Guard::RailsRunner do 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