added Trace class to simplify output

This commit is contained in:
Nick Gauthier 2010-02-04 11:02:28 -05:00
parent 2bee1dde2d
commit ef7f18c60d
3 changed files with 32 additions and 7 deletions

View File

@ -1,3 +1,4 @@
require 'hydra/trace'
require 'hydra/pipe'
require 'hydra/ssh'
require 'hydra/stdio'

View File

@ -10,6 +10,7 @@ module Hydra #:nodoc:
# of a machine.
class Runner
include Hydra::Messages::Runner
traceable('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 = {})
@ -18,20 +19,20 @@ module Hydra #:nodoc:
Test::Unit.run = true
$stdout.sync = true
$stdout.write "RUNNER| Booted. Sending Request for file\n" if @verbose
trace 'Booted. Sending Request for file'
@io.write RequestFile.new
begin
process_messages
rescue => ex
$stdout.write "#{ex.to_s}\n" if @verbose
trace ex.to_s
raise ex
end
end
# Run a test file and report the results
def run_file(file)
$stdout.write "RUNNER| Running file: #{file}\n" if @verbose
trace "Running file: #{file}"
require file
output = []
@result = Test::Unit::TestResult.new
@ -60,20 +61,20 @@ module Hydra #:nodoc:
# The runner will continually read messages and handle them.
def process_messages
$stdout.write "RUNNER| Processing Messages\n" if @verbose
trace "Processing Messages"
@running = true
while @running
begin
message = @io.gets
if message
$stdout.write "RUNNER| Received message from worker\n" if @verbose
$stdout.write " | #{message.inspect}\n" if @verbose
trace "Received message from worker"
trace "\t#{message.inspect}"
message.handle(self)
else
@io.write Ping.new
end
rescue IOError => ex
$stderr.write "Runner lost Worker\n" if @verbose
$stderr.write "Runner lost Worker"
@running = false
end
end

23
lib/hydra/trace.rb Normal file
View File

@ -0,0 +1,23 @@
module Hydra #:nodoc:
# Trace output when in verbose mode.
module Trace
module ClassMethods
# Make a class traceable. Takes one parameter,
# which is the prefix for the trace to identify this class
def traceable(prefix = self.class.to_s)
include Hydra::Trace::InstanceMethods
class << self; attr_accessor :_traceable_prefix; end
self._traceable_prefix = prefix
end
end
module InstanceMethods
# Trace some output with the class's prefix and a newline.
# Checks to ensure we're running verbosely.
def trace(str)
$stdout.write "#{self.class._traceable_prefix}| #{str}\n" if @verbose
end
end
end
end
Object.extend(Hydra::Trace::ClassMethods)