FIX: Thread safety issues with `multisite:migrate` and `SeedFu`.
This commit is contained in:
parent
86b43c5329
commit
45eb97c202
|
@ -1,8 +1,19 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
# fix any bust caches post initial migration
|
class SeedData::Refresher
|
||||||
ActiveRecord::Base.connection.tables.each do |table|
|
def self.refresh!
|
||||||
table.classify.constantize.reset_column_information rescue nil
|
return if @refreshed
|
||||||
|
|
||||||
|
# Fix any bust caches post initial migration
|
||||||
|
# Not that reset_column_information is not thread safe so we have to becareful
|
||||||
|
# not to run it concurrently within the same process.
|
||||||
|
ActiveRecord::Base.connection.tables.each do |table|
|
||||||
|
table.classify.constantize.reset_column_information rescue nil
|
||||||
|
end
|
||||||
|
|
||||||
|
@refreshed = true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
SeedData::Refresher.refresh!
|
||||||
SiteSetting.refresh!
|
SiteSetting.refresh!
|
||||||
|
|
|
@ -107,48 +107,63 @@ task 'multisite:migrate' => ['db:load_config', 'environment', 'set_locale'] do |
|
||||||
puts "Multisite migrator is running using #{concurrency} threads"
|
puts "Multisite migrator is running using #{concurrency} threads"
|
||||||
puts
|
puts
|
||||||
|
|
||||||
queue = Queue.new
|
|
||||||
exceptions = Queue.new
|
exceptions = Queue.new
|
||||||
|
|
||||||
old_stdout = $stdout
|
old_stdout = $stdout
|
||||||
$stdout = StdOutDemux.new($stdout)
|
$stdout = StdOutDemux.new($stdout)
|
||||||
|
|
||||||
RailsMultisite::ConnectionManagement.each_connection do |db|
|
|
||||||
queue << db
|
|
||||||
end
|
|
||||||
|
|
||||||
concurrency.times { queue << :done }
|
|
||||||
|
|
||||||
SeedFu.quiet = true
|
SeedFu.quiet = true
|
||||||
|
|
||||||
(1..concurrency).map do
|
def execute_concurently(concurrency)
|
||||||
Thread.new {
|
queue = Queue.new
|
||||||
while true
|
|
||||||
db = queue.pop
|
|
||||||
break if db == :done
|
|
||||||
|
|
||||||
RailsMultisite::ConnectionManagement.with_connection(db) do
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
||||||
begin
|
queue << db
|
||||||
puts "Migrating #{db}"
|
end
|
||||||
ActiveRecord::Tasks::DatabaseTasks.migrate
|
|
||||||
SeedFu.seed(DiscoursePluginRegistry.seed_paths)
|
concurrency.times { queue << :done }
|
||||||
if !Discourse.skip_post_deployment_migrations? && ENV['SKIP_OPTIMIZE_ICONS'] != '1'
|
|
||||||
SiteIconManager.ensure_optimized!
|
(1..concurrency).map do
|
||||||
end
|
Thread.new {
|
||||||
rescue => e
|
while true
|
||||||
exceptions << [db, e]
|
db = queue.pop
|
||||||
ensure
|
break if db == :done
|
||||||
|
|
||||||
|
RailsMultisite::ConnectionManagement.with_connection(db) do
|
||||||
begin
|
begin
|
||||||
$stdout.finish_chunk
|
yield(db) if block_given?
|
||||||
rescue => ex
|
rescue => e
|
||||||
STDERR.puts ex.inspect
|
exceptions << [db, e]
|
||||||
STDERR.puts ex.backtrace
|
ensure
|
||||||
|
begin
|
||||||
|
$stdout.finish_chunk
|
||||||
|
rescue => ex
|
||||||
|
STDERR.puts ex.inspect
|
||||||
|
STDERR.puts ex.backtrace
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
}
|
||||||
}
|
end.each(&:join)
|
||||||
end.each(&:join)
|
end
|
||||||
|
|
||||||
|
execute_concurently(concurrency) do |db|
|
||||||
|
puts "Migrating #{db}"
|
||||||
|
ActiveRecord::Tasks::DatabaseTasks.migrate
|
||||||
|
|
||||||
|
if !Discourse.skip_post_deployment_migrations? && ENV['SKIP_OPTIMIZE_ICONS'] != '1'
|
||||||
|
SiteIconManager.ensure_optimized!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
seed_paths = DiscoursePluginRegistry.seed_paths
|
||||||
|
SeedFu.seed(seed_paths, /001_refresh/)
|
||||||
|
|
||||||
|
execute_concurently(concurrency) do |db|
|
||||||
|
puts "Seeding #{db}"
|
||||||
|
SeedFu.seed(seed_paths)
|
||||||
|
end
|
||||||
|
|
||||||
$stdout = old_stdout
|
$stdout = old_stdout
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue