2018-08-26 17:53:06 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
dev_null = Logger.new('/dev/null')
|
|
|
|
|
|
|
|
Rails.logger = dev_null
|
|
|
|
ActiveRecord::Base.logger = dev_null
|
2018-09-09 14:33:36 +03:00
|
|
|
ActiveJob::Base.logger = dev_null
|
2018-08-26 17:53:06 +03:00
|
|
|
HttpLog.configuration.logger = dev_null
|
|
|
|
Paperclip.options[:log] = false
|
2019-09-10 14:48:48 +03:00
|
|
|
|
|
|
|
module Mastodon
|
|
|
|
module CLIHelper
|
2020-04-27 00:29:08 +03:00
|
|
|
def dry_run?
|
|
|
|
options[:dry_run]
|
|
|
|
end
|
|
|
|
|
2019-09-10 14:48:48 +03:00
|
|
|
def create_progress_bar(total = nil)
|
|
|
|
ProgressBar.create(total: total, format: '%c/%u |%b%i| %e')
|
|
|
|
end
|
|
|
|
|
|
|
|
def parallelize_with_progress(scope)
|
2019-10-07 05:24:05 +03:00
|
|
|
if options[:concurrency] < 1
|
|
|
|
say('Cannot run with this concurrency setting, must be at least 1', :red)
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
2019-10-07 06:05:02 +03:00
|
|
|
ActiveRecord::Base.configurations[Rails.env]['pool'] = options[:concurrency] + 1
|
2019-09-10 14:48:48 +03:00
|
|
|
|
|
|
|
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
|
2019-10-07 05:24:05 +03:00
|
|
|
begin
|
|
|
|
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
|
2019-09-10 14:48:48 +03:00
|
|
|
end
|
2019-10-07 05:24:05 +03:00
|
|
|
|
|
|
|
progress.log("Processing #{item.id}") if options[:verbose]
|
|
|
|
|
|
|
|
result = ActiveRecord::Base.connection_pool.with_connection do
|
|
|
|
yield(item)
|
|
|
|
end
|
|
|
|
|
|
|
|
aggregate.increment(result) if result.is_a?(Integer)
|
|
|
|
rescue => e
|
|
|
|
progress.log pastel.red("Error processing #{item.id}: #{e}")
|
|
|
|
ensure
|
|
|
|
progress.increment
|
2019-09-10 14:48:48 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
total.increment(items.size)
|
|
|
|
futures.map(&:value)
|
|
|
|
end
|
|
|
|
|
2019-10-07 05:24:05 +03:00
|
|
|
progress.stop
|
2019-09-10 14:48:48 +03:00
|
|
|
|
|
|
|
[total.value, aggregate.value]
|
|
|
|
end
|
|
|
|
|
|
|
|
def pastel
|
|
|
|
@pastel ||= Pastel.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|