From a5b582f686c0a8427aad3f6caf9f3a8105e66ee9 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Thu, 9 Apr 2020 12:50:14 +1000 Subject: [PATCH] DEV: demux stdout when running multisite migrate This avoids mixing up output in such a way that we can not tell which site ran which migrations Avoids threads all fighting for output --- lib/tasks/db.rake | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 027483f39d0..6b1a8b657b5 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -70,6 +70,25 @@ end # this ensures we can run migrations concurrently to save huge amounts of time Rake::Task['multisite:migrate'].clear +class StdOutDemux + def initialize(stdout) + @stdout = stdout + @data = {} + end + + def write(data) + (@data[Thread.current] ||= +"") << data + end + + def finish_chunk + data = @data[Thread.current] + if data + @stdout.write(data) + @data.delete Thread.current + end + end +end + task 'multisite:migrate' => ['db:load_config', 'environment', 'set_locale'] do |_, args| if ENV["RAILS_ENV"] != "production" raise "Multisite migrate is only supported in production" @@ -83,6 +102,9 @@ task 'multisite:migrate' => ['db:load_config', 'environment', 'set_locale'] do | queue = Queue.new exceptions = Queue.new + old_stdout = $stdout + $stdout = StdOutDemux.new($stdout) + RailsMultisite::ConnectionManagement.each_connection do |db| queue << db end @@ -107,12 +129,21 @@ task 'multisite:migrate' => ['db:load_config', 'environment', 'set_locale'] do | end rescue => e exceptions << [db, e] + ensure + begin + $stdout.finish_chunk + rescue => ex + STDERR.puts ex.inspect + STDERR.puts ex.backtrace + end end end end } end.each(&:join) + $stdout = old_stdout + if exceptions.length > 0 STDERR.puts STDERR.puts "-" * 80