Add CLI area progress bar helper (#25208)

This commit is contained in:
Matt Jankowski 2023-06-01 08:31:24 -04:00 committed by GitHub
parent 2a353200ad
commit 35c1c3e57a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 18 deletions

View file

@ -4,16 +4,39 @@ require_relative '../../../config/boot'
require_relative '../../../config/environment' require_relative '../../../config/environment'
require 'thor' require 'thor'
require_relative 'helper' require_relative 'progress_helper'
module Mastodon module Mastodon
module CLI module CLI
class Base < Thor class Base < Thor
include CLI::Helper include ProgressHelper
def self.exit_on_failure? def self.exit_on_failure?
true true
end end
private
def pastel
@pastel ||= Pastel.new
end
def dry_run?
options[:dry_run]
end
def dry_run_mode_suffix
dry_run? ? ' (DRY RUN)' : ''
end
def reset_connection_pools!
ActiveRecord::Base.establish_connection(
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).first.configuration_hash
.dup
.tap { |config| config['pool'] = options[:concurrency] + 1 }
)
RedisConfiguration.establish_pool(options[:concurrency])
end
end end
end end
end end

View file

@ -9,23 +9,19 @@ HttpLog.configuration.logger = dev_null
Paperclip.options[:log] = false Paperclip.options[:log] = false
Chewy.logger = dev_null Chewy.logger = dev_null
module Mastodon::CLI require 'ruby-progressbar/outputs/null'
module Helper
def dry_run?
options[:dry_run]
end
def dry_run_mode_suffix module Mastodon::CLI
dry_run? ? ' (DRY RUN)' : '' module ProgressHelper
end PROGRESS_FORMAT = '%c/%u |%b%i| %e'
def create_progress_bar(total = nil) def create_progress_bar(total = nil)
ProgressBar.create(total: total, format: '%c/%u |%b%i| %e') ProgressBar.create(
end {
total: total,
def reset_connection_pools! format: PROGRESS_FORMAT,
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Rails.env].dup.tap { |config| config['pool'] = options[:concurrency] + 1 }) }.merge(progress_output_options)
RedisConfiguration.establish_pool(options[:concurrency]) )
end end
def parallelize_with_progress(scope) def parallelize_with_progress(scope)
@ -82,8 +78,10 @@ module Mastodon::CLI
[total.value, aggregate.value] [total.value, aggregate.value]
end end
def pastel private
@pastel ||= Pastel.new
def progress_output_options
Rails.env.test? ? { output: ProgressBar::Outputs::Null } : {}
end end
end end
end end