A platform for community discussion. Free, open, simple.
DEV: handle termination cleanly in autospec
Samcc3fc87dd7e2
diff --git a/lib/autospec/manager.rb b/lib/autospec/manager.rbindex 6e611a550a7..b7810feffeb 100644--- a/lib/autospec/manager.rb+++ b/lib/autospec/manager.rb@@ -30,7 +30,15 @@ class Autospec::Managerdef runSignal.trap("HUP") { stop_runners; exit }- Signal.trap("INT") { stop_runners; exit }++ Signal.trap("INT") do+ begin+ stop_runners+ rescue => e+ puts "FAILED TO STOP RUNNERS #{e}"+ end+ exit+ endensure_all_specs_will_runstart_runnersdiff --git a/lib/autospec/simple_runner.rb b/lib/autospec/simple_runner.rbindex 2c1fe8e656f..24150c96f54 100644--- a/lib/autospec/simple_runner.rb+++ b/lib/autospec/simple_runner.rb@@ -3,11 +3,16 @@ require "autospec/rspec_runner"module Autospecclass SimpleRunner < RspecRunner+ def initialize+ @mutex = Mutex.new+ enddef run(specs)puts "Running Rspec: " << specs# kill previous rspec instance- self.abort+ @mutex.synchronize do+ self.abort+ end# we use our custom rspec formatterargs = ["-r", "#{File.dirname(__FILE__)}/formatter.rb","-f", "Autospec::Formatter", specs.split].flatten.join(" ")@@ -18,24 +23,42 @@ module Autospecenv["LOAD_PLUGINS"] = "1"puts "Loading plugins while running specs"end- @pid = Process.spawn(env, "bin/rspec #{args}")- _, status = Process.wait2(@pid)+ pid =+ @mutex.synchronize do+ @pid = Process.spawn(env, "bin/rspec #{args}")+ end++ _, status = Process.wait2(pid)+status.exitstatusendenddef abort- if @pid- Process.kill("INT", @pid) rescue nil- while (Process.getpgid(@pid) rescue nil)- sleep 0.001- end- @pid = nil+ if pid = @pid+ Process.kill("TERM", pid) rescue nil+ wait_for_done(pid)+ pid = nilendenddef stop- abort+ # assume sigint on child will take care of this?+ if pid = @pid+ wait_for_done(pid)+ end+ end++ def wait_for_done(pid)+ i = 3000+ while (i > 0 && Process.getpgid(pid) rescue nil)+ sleep 0.001+ i -= 1+ end+ if (Process.getpgid(pid) rescue nil)+ STDERR.puts "Terminating rspec #{pid} by force cause it refused graceful termination"+ Process.kill("KILL", pid)+ endendenddiff --git a/lib/demon/base.rb b/lib/demon/base.rbindex af4a73bb3cd..8cda80a0af4 100644--- a/lib/demon/base.rb+++ b/lib/demon/base.rb@@ -61,11 +61,14 @@ class Demon::Baseendend+ def stop_signal+ "HUP"+ end+def stop@started = falseif @pid- # TODO configurable stop signal- Process.kill("HUP", @pid)+ Process.kill(stop_signal, @pid)wait_for_stop = lambda {timeout = @stop_timeout@@ -82,7 +85,7 @@ class Demon::Basewait_for_stop.callif alive?- STDERR.puts "Process would not terminate cleanly, force quitting. pid: #{@pid}"+ STDERR.puts "Process would not terminate cleanly, force quitting. pid: #{@pid} #{self.class}"Process.kill("KILL", @pid)enddiff --git a/lib/demon/rails_autospec.rb b/lib/demon/rails_autospec.rbindex 702876c87e4..38444d4853f 100644--- a/lib/demon/rails_autospec.rb+++ b/lib/demon/rails_autospec.rb@@ -6,6 +6,10 @@ class Demon::RailsAutospec < Demon::Base"rails-autospec"end+ def stop_signal+ "TERM"+ end+privatedef after_fork