Sig Hup was implemented

This commit is contained in:
Arturo Pie 2011-06-03 18:51:36 -04:00
parent d6ff3ea5d2
commit 49dfd644b8
4 changed files with 51 additions and 38 deletions

View File

@ -16,16 +16,16 @@ module Hydra #:nodoc:
# Boot up a runner. It takes an IO object (generally a pipe from its # Boot up a runner. It takes an IO object (generally a pipe from its
# parent) to send it messages on which files to execute. # parent) to send it messages on which files to execute.
def initialize(opts = {}) def initialize(opts = {})
redirect_output
reg_trap_sighup
@io = opts.fetch(:io) { raise "No IO Object" } @io = opts.fetch(:io) { raise "No IO Object" }
@verbose = opts.fetch(:verbose) { false } @verbose = opts.fetch(:verbose) { false }
@event_listeners = Array( opts.fetch( :runner_listeners ) { nil } ) @event_listeners = Array( opts.fetch( :runner_listeners ) { nil } )
$stdout.sync = true $stdout.sync = true
runner_begin runner_begin
reg_exit_hook
trace 'Booted. Sending Request for file' trace 'Booted. Sending Request for file'
@io.write RequestFile.new @io.write RequestFile.new
begin begin
@ -36,18 +36,19 @@ module Hydra #:nodoc:
end end
end end
def reg_trap_sighup
trap :SIGHUP do
File.open("_log_output", 'a'){ |f| f << "SIGHUP trapped"}
stop
end
@runner_began = true
end
def runner_begin def runner_begin
trace "Firing runner_begin event" trace "Firing runner_begin event"
@event_listeners.each {|l| l.runner_begin( self ) } @event_listeners.each {|l| l.runner_begin( self ) }
end end
def reg_exit_hook
at_exit do
# NOTE: do not use trace here
stop
end
end
# Run a test file and report the results # Run a test file and report the results
def run_file(file) def run_file(file)
trace "Running file: #{file}" trace "Running file: #{file}"
@ -71,16 +72,19 @@ module Hydra #:nodoc:
# Stop running # Stop running
def stop def stop
# NOTE: do not use trace here runner_end if @runner_began
runner_end if @running @runner_began = @running = false
@running = false
end end
def runner_end def runner_end
# trace "Firing runner_end event" trace "Ending runner #{self.inspect}"
@event_listeners.each {|l| l.runner_end( self ) } @event_listeners.each {|l| l.runner_end( self ) }
end end
def format_exception(ex)
"#{ex.class.name}: #{ex.message}\n #{ex.backtrace.join("\n ")}"
end
private private
# The runner will continually read messages and handle them. # The runner will continually read messages and handle them.
@ -108,10 +112,6 @@ module Hydra #:nodoc:
"Error in #{file}:\n #{format_exception(ex)}" "Error in #{file}:\n #{format_exception(ex)}"
end end
def format_exception(ex)
"#{ex.class.name}: #{ex.message}\n #{ex.backtrace.join("\n ")}"
end
# Run all the Test::Unit Suites in a ruby file # Run all the Test::Unit Suites in a ruby file
def run_test_unit_file(file) def run_test_unit_file(file)
begin begin
@ -288,5 +288,11 @@ module Hydra #:nodoc:
end end
end.compact end.compact
end end
def redirect_output file_name = nil
file_name = 'log/hydra.log' if !file_name and File.exists? 'log/'
file_name = 'hydra.log' unless file_name
$stderr = $stdout = File.open(file_name, 'a')
end
end end
end end

View File

@ -8,9 +8,14 @@ module HydraExtension
end end
class RunnerEndTest < Hydra::RunnerListener::Abstract class RunnerEndTest < Hydra::RunnerListener::Abstract
# Fired by the runner just before requesting the first file
def runner_begin( runner )
FileUtils.touch File.expand_path(File.join(Dir.consistent_tmpdir, 'runner_began_flag')) #used to know when the runner is ready
end
# Fired by the runner just after stoping # Fired by the runner just after stoping
def runner_end( runner ) def runner_end( runner )
# NOTE: do not use trace here # NOTE: do not use trace here
#runner.trace "Ending runner"
FileUtils.touch File.expand_path(File.join(Dir.consistent_tmpdir, 'alternate_hydra_test.txt')) FileUtils.touch File.expand_path(File.join(Dir.consistent_tmpdir, 'alternate_hydra_test.txt'))
end end
end end

View File

@ -188,8 +188,8 @@ class MasterTest < Test::Unit::TestCase
FileUtils.rm_f(target_file) FileUtils.rm_f(target_file)
FileUtils.rm_f(alternate_target_file) FileUtils.rm_f(alternate_target_file)
@worker_began_flag = File.expand_path(File.join(Dir.consistent_tmpdir, 'worker_began_flag')) #used to know when the worker is ready @runner_began_flag = File.expand_path(File.join(Dir.consistent_tmpdir, 'runner_began_flag')) #used to know when the worker is ready
FileUtils.rm_f(@worker_began_flag) FileUtils.rm_f(@runner_began_flag)
@runner_listener = 'HydraExtension::RunnerListener::RunnerEndTest.new' # runner_end method that creates alternate_target_file @runner_listener = 'HydraExtension::RunnerListener::RunnerEndTest.new' # runner_end method that creates alternate_target_file
@master_listener = HydraExtension::Listener::WorkerBeganFlag.new #used to know when the runner is up @master_listener = HydraExtension::Listener::WorkerBeganFlag.new #used to know when the runner is up
@ -217,13 +217,7 @@ class MasterTest < Test::Unit::TestCase
end end
should "run runner_end after interruption signal" do should "run runner_end after interruption signal" do
add_infinite_worker_begin_to @master_listener
class << @master_listener
def worker_begin( worker )
super
sleep 1 while true #ensure the process doesn't finish before killing it
end
end
capture_stderr do # redirect stderr capture_stderr do # redirect stderr
@pid = Process.fork do @pid = Process.fork do
@ -248,6 +242,13 @@ class MasterTest < Test::Unit::TestCase
context "running a remote worker" do context "running a remote worker" do
setup do setup do
copy_worker_init_file # this method has a protection to avoid erasing an existing worker_init_file copy_worker_init_file # this method has a protection to avoid erasing an existing worker_init_file
end
teardown do
FileUtils.rm_f(@remote_init_file) unless @protect_init_file
end
should "run runner_end on successful termination" do
capture_stderr do # redirect stderr capture_stderr do # redirect stderr
@pid = Process.fork do @pid = Process.fork do
Hydra::Master.new( Hydra::Master.new(
@ -265,13 +266,6 @@ class MasterTest < Test::Unit::TestCase
) )
end end
end end
end
teardown do
FileUtils.rm_f(@remote_init_file) unless @protect_init_file
end
should "run runner_end on successful termination" do
Process.waitpid @pid Process.waitpid @pid
assert_file_exists alternate_target_file assert_file_exists alternate_target_file
@ -280,11 +274,19 @@ class MasterTest < Test::Unit::TestCase
end end
private private
def add_infinite_worker_begin_to master_listener
class << master_listener
def worker_begin( worker )
super
sleep 1 while true #ensure the process doesn't finish before killing it
end
end
end
# this requires that a worker_begin listener creates a file named worker_began_flag in tmp directory # this requires that a worker_begin listener creates a file named worker_began_flag in tmp directory
def wait_for_runner_to_begin def wait_for_runner_to_begin
FileUtils.rm_f(@worker_began_flag) assert_file_exists @runner_began_flag
assert_file_exists @worker_began_flag
end end
# with a protection to avoid erasing something important in lib # with a protection to avoid erasing something important in lib

View File

@ -161,7 +161,7 @@ class RunnerTest < Test::Unit::TestCase
run_the_runner(pipe, [HydraExtension::RunnerListener::RunnerEndTest.new] ) run_the_runner(pipe, [HydraExtension::RunnerListener::RunnerEndTest.new] )
Process.wait(parent) Process.wait(parent)
# ensure runner_begin was fired # ensure runner_end was fired
assert File.exists?( alternate_target_file ) assert File.exists?( alternate_target_file )
end end
end end