From b8cff6023a28fcd77afb546ffece95357844399f Mon Sep 17 00:00:00 2001 From: Sean Kirby Date: Fri, 7 May 2010 16:15:57 -0400 Subject: [PATCH] added threading and ability to specify environment for remote tasks --- lib/hydra/sync.rb | 1 - lib/hydra/tasks.rb | 69 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/lib/hydra/sync.rb b/lib/hydra/sync.rb index f906a63..ffc9259 100644 --- a/lib/hydra/sync.rb +++ b/lib/hydra/sync.rb @@ -88,7 +88,6 @@ module Hydra #:nodoc: Sync.new worker_opts, @sync, @verbose rescue trace "Syncing failed [#{worker_opts.inspect}]" - Thread.exit end end end diff --git a/lib/hydra/tasks.rb b/lib/hydra/tasks.rb index 80ca5b7..dbafbb9 100644 --- a/lib/hydra/tasks.rb +++ b/lib/hydra/tasks.rb @@ -165,33 +165,60 @@ module Hydra #:nodoc: desc "Run #{@name} remotely on all workers" task "hydra:remote:#{@name}" do config = YAML.load_file(@config) + environment = config.fetch('environment') { 'test' } workers = config.fetch('workers') { [] } workers = workers.select{|w| w['type'] == 'ssh'} + + $stdout.write "==== Hydra Running #{@name} ====\n" + Thread.abort_on_exception = true + @listeners = [] + @results = {} workers.each do |worker| - $stdout.write "==== Hydra Running #{@name} on #{worker['connect']} ====\n" - ssh_opts = worker.fetch('ssh_opts') { '' } - writer, reader, error = popen3("ssh -tt #{ssh_opts} #{worker['connect']} ") - writer.write("cd #{worker['directory']}\n") - writer.write "echo BEGIN HYDRA\n" - writer.write("RAILS_ENV=test rake #{@name}\n") - writer.write "echo END HYDRA\n" - writer.write("exit\n") - writer.close - ignoring = true - while line = reader.gets - line.chomp! - if line =~ /echo END HYDRA$/ - ignoring = true - end - $stdout.write "#{line}\n" unless ignoring - if line == 'BEGIN HYDRA' - ignoring = false - end - end - $stdout.write "\n==== Hydra Running #{@name} COMPLETE ====\n\n" + @listeners << Thread.new do + begin + @results[worker] = if run_task(worker, environment) + "==== #{@name} passed on #{worker['connect']} ====\n" + else + "==== #{@name} failed on #{worker['connect']} ====\nPlease see above for more details.\n" + end + rescue + @results[worker] = "==== #{@name} failed for #{worker['connect']} ====\n#{$!.inspect}\n#{$!.backtrace.join("\n")}" + end + end end + @listeners.each{|l| l.join} + $stdout.write "\n==== Hydra Running #{@name} COMPLETE ====\n\n" + $stdout.write @results.values.join('\n') end end + + def run_task worker, environment + $stdout.write "==== Hydra Running #{@name} on #{worker['connect']} ====\n" + ssh_opts = worker.fetch('ssh_opts') { '' } + writer, reader, error = popen3("ssh -tt #{ssh_opts} #{worker['connect']} ") + writer.write("cd #{worker['directory']}\n") + writer.write "echo BEGIN HYDRA\n" + writer.write("RAILS_ENV=#{environment} rake #{@name}\n") + writer.write "echo END HYDRA\n" + writer.write("exit\n") + writer.close + ignoring = true + passed = true + while line = reader.gets + line.chomp! + if line =~ /^rake aborted!$/ + passed = false + end + if line =~ /echo END HYDRA$/ + ignoring = true + end + $stdout.write "#{worker['connect']}: #{line}\n" unless ignoring + if line == 'BEGIN HYDRA' + ignoring = false + end + end + passed + end end # A Hydra global task is a task that is run both locally and remotely.