BURs: update posts in parallel.
When processing an alias, rename, implication, mass update, or nuke, update the posts in parallel. This means that if we alias foo to bar, for example, then we use four processes at once to retag the posts from foo to bar. This doesn't mean that if we have two aliases in a BUR, we process both aliases in parallel. It simply means that when processing an alias, we update the posts in parallel for that alias.
This commit is contained in:
@@ -166,6 +166,33 @@ class ApplicationRecord < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
concerning :ConcurrencyMethods do
|
||||
class_methods do
|
||||
def parallel_each(batch_size: 1000, in_processes: 4, in_threads: nil)
|
||||
# XXX Use threads in testing because processes can't see each other's
|
||||
# database transactions.
|
||||
if Rails.env.test?
|
||||
in_processes = nil
|
||||
in_threads = 2
|
||||
end
|
||||
|
||||
current_user = CurrentUser.user
|
||||
current_ip = CurrentUser.ip_addr
|
||||
|
||||
find_in_batches(batch_size: batch_size, error_on_ignore: true) do |batch|
|
||||
Parallel.each(batch, in_processes: in_processes, in_threads: in_threads) do |record|
|
||||
# XXX In threaded mode, the current user isn't inherited from the
|
||||
# parent thread because the current user is a thread-local
|
||||
# variable. Hence, we have to set it explicitly in the child thread.
|
||||
CurrentUser.scoped(current_user, current_ip) do
|
||||
yield record
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def warnings
|
||||
@warnings ||= ActiveModel::Errors.new(self)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user