Skip to content

A platform for community discussion. Free, open, simple.

Revert "DEV: Resupport running `multisite:migrate` concurrently (#36853)" (#38367)

David Taylor330b50a89881
diff --git a/db/fixtures/600_themes.rb b/db/fixtures/600_themes.rb
index 8670b40852a..335bdb82275 100644
--- a/db/fixtures/600_themes.rb
+++ b/db/fixtures/600_themes.rb
@@ -6,7 +6,7 @@ SystemThemesManager.sync!
# we can not guess what to do if customization already started, so skip it
if !theme_exists
- $stdout.puts "Seeding theme and color schemes"
+ STDERR.puts "> Seeding theme and color schemes"
color_schemes = [
{ name: I18n.t("color_schemes.dark"), base_scheme_name: "Dark" },
diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake
index 6837fda64d3..5fd71e00f9f 100644
--- a/lib/tasks/db.rake
+++ b/lib/tasks/db.rake
@@ -86,6 +86,33 @@ 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 close
+ finish_chunk
+ end
+
+ def finish_chunk
+ data = @data[Thread.current]
+ if data
+ @stdout.write(data)
+ @data.delete Thread.current
+ end
+ end
+
+ def flush
+ # Do nothing
+ end
+end
+
class SeedHelper
def self.paths
DiscoursePluginRegistry.seed_paths
@@ -101,92 +128,107 @@ class SeedHelper
end
end
-def execute_db_migration
- ActiveRecord::Tasks::DatabaseTasks.migrate
- Rake::Task["db:seed"].invoke if ENV["SKIP_SEED_FU"] != "1"
-
- if !Discourse.skip_post_deployment_migrations? && ENV["SKIP_OPTIMIZE_ICONS"] != "1"
- SiteIconManager.ensure_optimized!
- end
-end
-
task "multisite:migrate" => %w[
db:load_config
environment
set_locale
assets:precompile:asset_processor
] do |_, args|
+ raise "Multisite migrate is only supported in production" if ENV["RAILS_ENV"] != "production"
+
DistributedMutex.synchronize(
"db_migration",
redis: Discourse.redis.without_namespace,
validity: 1200,
) do
- concurrency = ENV["DISCOURSE_MULTISITE_MIGRATE_CONCURRENCY"]&.to_i || 2
-
- puts "Multisite migrator is running using #{concurrency} processes"
+ # TODO: Switch to processes for concurrent migrations because Rails migration
+ # is not thread safe by default.
+ concurrency = 1
- databases = []
- RailsMultisite::ConnectionManagement.each_connection { |db| databases << db }
+ puts "Multisite migrator is running using #{concurrency} threads"
+ puts
- puts "Running migrations and seeds for #{databases.join(", ")} database(s)"
+ exceptions = Queue.new
- results =
- Parallel.map(
- databases,
- in_processes: concurrency,
- isolation: true,
- finish_in_order: true,
- finish:
- lambda do |db, _index, result|
- result[:stdout]&.lines&.each { |line| puts "[#{db}] #{line}" }
- end,
- ) do |db|
- Discourse.after_fork
-
- output_buffer = StringIO.new
- original_stdout = $stdout
- $stdout = output_buffer
-
- error = nil
+ if concurrency > 1
+ old_stdout = $stdout
+ $stdout = StdOutDemux.new($stdout)
+ end
- begin
- RailsMultisite::ConnectionManagement.with_connection(db) do
- puts "Processing"
- puts "-" * 40
- execute_db_migration
+ SeedFu.quiet = true
+
+ def execute_concurrently(concurrency, exceptions)
+ queue = Queue.new
+
+ RailsMultisite::ConnectionManagement.each_connection { |db| queue << db }
+
+ concurrency.times { queue << :done }
+
+ (1..concurrency)
+ .map do
+ Thread.new do
+ while true
+ db = queue.pop
+ break if db == :done
+
+ RailsMultisite::ConnectionManagement.with_connection(db) do
+ begin
+ yield(db) if block_given?
+ rescue => e
+ exceptions << [db, e]
+ ensure
+ begin
+ $stdout.finish_chunk if concurrency > 1
+ rescue => ex
+ STDERR.puts ex.inspect
+ STDERR.puts ex.backtrace
+ end
+ end
+ end
+ end
end
- rescue => e
- error = e
end
+ .each(&:join)
+ end
- if error.nil?
- puts "Completed"
- puts "-" * 40
+ def check_exceptions(exceptions)
+ if exceptions.length > 0
+ STDERR.puts
+ STDERR.puts "-" * 80
+ STDERR.puts "#{exceptions.length} migrations failed!"
+ while !exceptions.empty?
+ db, e = exceptions.pop
+ STDERR.puts
+ STDERR.puts "Failed to migrate #{db}"
+ STDERR.puts e.inspect
+ STDERR.puts e.backtrace
+ STDERR.puts
end
-
- { db: db, stdout: output_buffer.string, error: error }
- ensure
- $stdout = original_stdout
+ exit 1
end
+ end
- errors = results.select { |r| r[:error] }
+ execute_concurrently(concurrency, exceptions) do |db|
+ puts "Migrating #{db}"
+ ActiveRecord::Tasks::DatabaseTasks.migrate
+ end
- if errors.any?
- $stderr.puts
- $stderr.puts "-" * 80
- $stderr.puts "#{errors.length} database(s) failed!"
+ check_exceptions(exceptions)
- errors.each do |result|
- $stderr.puts
- $stderr.puts "Failed to process #{result[:db]}"
- $stderr.puts result[:error].inspect
- $stderr.puts result[:error].backtrace
- $stderr.puts
- end
+ SeedFu.seed(SeedHelper.paths, /001_refresh/)
+
+ execute_concurrently(concurrency, exceptions) do |db|
+ puts "Seeding #{db}"
+ SeedFu.seed(SeedHelper.paths, SeedHelper.filter)
- raise errors.first[:error]
+ if !Discourse.skip_post_deployment_migrations? && ENV["SKIP_OPTIMIZE_ICONS"] != "1"
+ SiteIconManager.ensure_optimized!
+ end
end
+ $stdout = old_stdout if concurrency > 1
+ check_exceptions(exceptions)
+
Rake::Task["db:_dump"].invoke
end
end
@@ -222,8 +264,15 @@ task "db:migrate" => %w[
end
end
- execute_db_migration
+ ActiveRecord::Tasks::DatabaseTasks.migrate
+
+ Rake::Task["db:seed"].invoke if ENV["SKIP_SEED_FU"] != "1"
+
Rake::Task["db:schema:cache:dump"].invoke if Rails.env.development? && !ENV["RAILS_DB"]
+
+ if !Discourse.skip_post_deployment_migrations? && ENV["SKIP_OPTIMIZE_ICONS"] != "1"
+ SiteIconManager.ensure_optimized!
+ end
end
if !Discourse.is_parallel_test? && MultisiteTestHelpers.load_multisite?