You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.4 KiB
88 lines
2.4 KiB
6 years ago
|
# frozen_string_literal: true
|
||
|
|
||
|
dev_null = Logger.new('/dev/null')
|
||
|
|
||
|
Rails.logger = dev_null
|
||
|
ActiveRecord::Base.logger = dev_null
|
||
6 years ago
|
ActiveJob::Base.logger = dev_null
|
||
6 years ago
|
HttpLog.configuration.logger = dev_null
|
||
|
Paperclip.options[:log] = false
|
||
4 years ago
|
Chewy.logger = dev_null
|
||
5 years ago
|
|
||
1 year ago
|
require 'ruby-progressbar/outputs/null'
|
||
5 years ago
|
|
||
1 year ago
|
module Mastodon::CLI
|
||
|
module ProgressHelper
|
||
|
PROGRESS_FORMAT = '%c/%u |%b%i| %e'
|
||
1 year ago
|
|
||
5 years ago
|
def create_progress_bar(total = nil)
|
||
1 year ago
|
ProgressBar.create(
|
||
|
{
|
||
|
total: total,
|
||
|
format: PROGRESS_FORMAT,
|
||
|
}.merge(progress_output_options)
|
||
|
)
|
||
3 years ago
|
end
|
||
|
|
||
5 years ago
|
def parallelize_with_progress(scope)
|
||
5 years ago
|
if options[:concurrency] < 1
|
||
|
say('Cannot run with this concurrency setting, must be at least 1', :red)
|
||
|
exit(1)
|
||
|
end
|
||
|
|
||
3 years ago
|
reset_connection_pools!
|
||
5 years ago
|
|
||
|
progress = create_progress_bar(scope.count)
|
||
|
pool = Concurrent::FixedThreadPool.new(options[:concurrency])
|
||
|
total = Concurrent::AtomicFixnum.new(0)
|
||
|
aggregate = Concurrent::AtomicFixnum.new(0)
|
||
|
|
||
|
scope.reorder(nil).find_in_batches do |items|
|
||
|
futures = []
|
||
|
|
||
|
items.each do |item|
|
||
|
futures << Concurrent::Future.execute(executor: pool) do
|
||
2 years ago
|
if !progress.total.nil? && progress.progress + 1 > progress.total
|
||
|
# The number of items has changed between start and now,
|
||
|
# since there is no good way to predict the final count from
|
||
|
# here, just change the progress bar to an indeterminate one
|
||
|
|
||
|
progress.total = nil
|
||
|
end
|
||
|
|
||
|
progress.log("Processing #{item.id}") if options[:verbose]
|
||
|
|
||
2 years ago
|
Chewy.strategy(:mastodon) do
|
||
|
result = ActiveRecord::Base.connection_pool.with_connection do
|
||
|
yield(item)
|
||
|
ensure
|
||
|
RedisConfiguration.pool.checkin if Thread.current[:redis]
|
||
|
Thread.current[:redis] = nil
|
||
|
end
|
||
|
|
||
|
aggregate.increment(result) if result.is_a?(Integer)
|
||
5 years ago
|
end
|
||
2 years ago
|
rescue => e
|
||
|
progress.log pastel.red("Error processing #{item.id}: #{e}")
|
||
|
ensure
|
||
|
progress.increment
|
||
5 years ago
|
end
|
||
|
end
|
||
|
|
||
|
total.increment(items.size)
|
||
|
futures.map(&:value)
|
||
|
end
|
||
|
|
||
5 years ago
|
progress.stop
|
||
5 years ago
|
|
||
|
[total.value, aggregate.value]
|
||
|
end
|
||
|
|
||
1 year ago
|
private
|
||
|
|
||
|
def progress_output_options
|
||
|
Rails.env.test? ? { output: ProgressBar::Outputs::Null } : {}
|
||
5 years ago
|
end
|
||
|
end
|
||
|
end
|