Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Sig Hup was implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
arturopie committed Jun 3, 2011
1 parent d6ff3ea commit 49dfd64
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
42 changes: 24 additions & 18 deletions lib/hydra/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ class Runner
# Boot up a runner. It takes an IO object (generally a pipe from its
# parent) to send it messages on which files to execute.
def initialize(opts = {})
redirect_output
reg_trap_sighup

@io = opts.fetch(:io) { raise "No IO Object" }
@verbose = opts.fetch(:verbose) { false }
@event_listeners = Array( opts.fetch( :runner_listeners ) { nil } )

$stdout.sync = true

runner_begin

reg_exit_hook

trace 'Booted. Sending Request for file'
@io.write RequestFile.new
begin
Expand All @@ -36,18 +36,19 @@ def initialize(opts = {})
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
trace "Firing runner_begin event"
@event_listeners.each {|l| l.runner_begin( self ) }
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
def run_file(file)
trace "Running file: #{file}"
Expand All @@ -71,16 +72,19 @@ def run_file(file)

# Stop running
def stop
# NOTE: do not use trace here
runner_end if @running
@running = false
runner_end if @runner_began
@runner_began = @running = false
end

def runner_end
# trace "Firing runner_end event"
trace "Ending runner #{self.inspect}"
@event_listeners.each {|l| l.runner_end( self ) }
end

def format_exception(ex)
"#{ex.class.name}: #{ex.message}\n #{ex.backtrace.join("\n ")}"
end

private

# The runner will continually read messages and handle them.
Expand Down Expand Up @@ -108,10 +112,6 @@ def format_ex_in_file(file, ex)
"Error in #{file}:\n #{format_exception(ex)}"
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
def run_test_unit_file(file)
begin
Expand Down Expand Up @@ -288,5 +288,11 @@ def tag_excess(features, limits)
end
end.compact
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
5 changes: 5 additions & 0 deletions test/fixtures/runner_listeners.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ def runner_begin( runner )
end

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
def runner_end( runner )
# NOTE: do not use trace here
#runner.trace "Ending runner"
FileUtils.touch File.expand_path(File.join(Dir.consistent_tmpdir, 'alternate_hydra_test.txt'))
end
end
Expand Down
40 changes: 21 additions & 19 deletions test/master_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ def file_end(file, output)
FileUtils.rm_f(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
FileUtils.rm_f(@worker_began_flag)
@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(@runner_began_flag)

@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
Expand Down Expand Up @@ -217,13 +217,7 @@ def file_end(file, output)
end

should "run runner_end after interruption signal" do

class << @master_listener
def worker_begin( worker )
super
sleep 1 while true #ensure the process doesn't finish before killing it
end
end
add_infinite_worker_begin_to @master_listener

capture_stderr do # redirect stderr
@pid = Process.fork do
Expand All @@ -248,6 +242,13 @@ def worker_begin( worker )
context "running a remote worker" do
setup do
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
@pid = Process.fork do
Hydra::Master.new(
Expand All @@ -265,13 +266,6 @@ def worker_begin( worker )
)
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

assert_file_exists alternate_target_file
Expand All @@ -280,11 +274,19 @@ def worker_begin( worker )
end

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
def wait_for_runner_to_begin
FileUtils.rm_f(@worker_began_flag)

assert_file_exists @worker_began_flag
assert_file_exists @runner_began_flag
end

# with a protection to avoid erasing something important in lib
Expand Down
2 changes: 1 addition & 1 deletion test/runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class RunnerTest < Test::Unit::TestCase
run_the_runner(pipe, [HydraExtension::RunnerListener::RunnerEndTest.new] )
Process.wait(parent)

# ensure runner_begin was fired
# ensure runner_end was fired
assert File.exists?( alternate_target_file )
end
end
Expand Down

0 comments on commit 49dfd64

Please sign in to comment.