2018-08-26 10:53:06 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
dev_null = Logger.new('/dev/null')
|
|
|
|
|
|
|
|
Rails.logger = dev_null
|
|
|
|
ActiveRecord::Base.logger = dev_null
|
2018-09-09 07:33:36 -04:00
|
|
|
ActiveJob::Base.logger = dev_null
|
2024-11-07 04:57:42 -05:00
|
|
|
HttpLog.configuration.logger = dev_null if defined?(HttpLog)
|
2018-08-26 10:53:06 -04:00
|
|
|
Paperclip.options[:log] = false
|
2020-07-14 12:10:35 -04:00
|
|
|
Chewy.logger = dev_null
|
2019-09-10 07:48:48 -04:00
|
|
|
|
2023-06-01 08:31:24 -04:00
|
|
|
require 'ruby-progressbar/outputs/null'
|
2020-04-26 17:29:08 -04:00
|
|
|
|
2023-06-01 08:31:24 -04:00
|
|
|
module Mastodon::CLI
|
|
|
|
module ProgressHelper
|
|
|
|
PROGRESS_FORMAT = '%c/%u |%b%i| %e'
|
2023-05-30 10:07:44 -04:00
|
|
|
|
2019-09-10 07:48:48 -04:00
|
|
|
def create_progress_bar(total = nil)
|
2023-06-01 08:31:24 -04:00
|
|
|
ProgressBar.create(
|
|
|
|
{
|
|
|
|
total: total,
|
|
|
|
format: PROGRESS_FORMAT,
|
|
|
|
}.merge(progress_output_options)
|
|
|
|
)
|
2022-04-29 16:43:07 -04:00
|
|
|
end
|
|
|
|
|
2019-09-10 07:48:48 -04:00
|
|
|
def parallelize_with_progress(scope)
|
2024-01-26 03:53:44 -05:00
|
|
|
fail_with_message 'Cannot run with this concurrency setting, must be at least 1' if options[:concurrency] < 1
|
2019-10-06 22:24:05 -04:00
|
|
|
|
2022-04-29 16:43:07 -04:00
|
|
|
reset_connection_pools!
|
2019-09-10 07:48:48 -04: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|
|
2024-04-10 08:46:43 -04:00
|
|
|
futures = items.map do |item|
|
|
|
|
Concurrent::Future.execute(executor: pool) do
|
2023-02-18 17:09:40 -05:00
|
|
|
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]
|
|
|
|
|
2023-03-20 15:02:58 -04:00
|
|
|
Chewy.strategy(:mastodon) do
|
|
|
|
result = ActiveRecord::Base.connection_pool.with_connection do
|
|
|
|
yield(item)
|
|
|
|
ensure
|
2024-09-02 10:19:55 -04:00
|
|
|
RedisConnection.pool.checkin if Thread.current[:redis]
|
2023-03-20 15:02:58 -04:00
|
|
|
Thread.current[:redis] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
aggregate.increment(result) if result.is_a?(Integer)
|
2019-09-10 07:48:48 -04:00
|
|
|
end
|
2023-02-18 17:09:40 -05:00
|
|
|
rescue => e
|
|
|
|
progress.log pastel.red("Error processing #{item.id}: #{e}")
|
|
|
|
ensure
|
|
|
|
progress.increment
|
2019-09-10 07:48:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
total.increment(items.size)
|
|
|
|
futures.map(&:value)
|
|
|
|
end
|
|
|
|
|
2019-10-06 22:24:05 -04:00
|
|
|
progress.stop
|
2019-09-10 07:48:48 -04:00
|
|
|
|
|
|
|
[total.value, aggregate.value]
|
|
|
|
end
|
|
|
|
|
2023-06-01 08:31:24 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def progress_output_options
|
|
|
|
Rails.env.test? ? { output: ProgressBar::Outputs::Null } : {}
|
2019-09-10 07:48:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|