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/pipe'
require 'hydra/ssh' require 'hydra/ssh'
require 'hydra/stdio' require 'hydra/stdio'

View File

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