[#2180] Progress reporter that doesn't crash

This commit is contained in:
Frederic Merizen 2018-12-06 17:02:08 +01:00
parent 6522248801
commit c6c8bea095
2 changed files with 61 additions and 9 deletions

View file

@ -69,7 +69,7 @@ namespace :'2018_12_03_finish_piece_jointe_transfer' do
def refresh_outdated_files
rake_puts "Refresh outdated attachments"
bar = RakeProgressbar.new(ActiveStorage::Blob.count)
progress = ProgressReport.new(ActiveStorage::Blob.count)
refreshed_keys = []
missing_keys = []
old_pj_adapter.session do |old_pjs|
@ -105,10 +105,10 @@ namespace :'2018_12_03_finish_piece_jointe_transfer' do
file.unlink
end
end
bar.inc
progress.inc
end
end
bar.finished
progress.finish
if verbose?
rake_puts "Refreshed #{refreshed_keys.count} attachments\n#{refreshed_keys.join(', ')}"
@ -132,7 +132,7 @@ namespace :'2018_12_03_finish_piece_jointe_transfer' do
end
rake_puts "Fix MIME types"
bar = RakeProgressbar.new(ActiveStorage::Blob.count)
progress = ProgressReport.new(ActiveStorage::Blob.count)
failed_keys = []
updated_keys = []
ActiveStorage::Blob.find_each do |blob|
@ -144,9 +144,9 @@ namespace :'2018_12_03_finish_piece_jointe_transfer' do
end
end
end
bar.inc
progress.inc
end
bar.finished
progress.finish
if verbose?
rake_puts "Updated MIME Type for #{updated_keys.count} keys\n#{updated_keys.join(', ')}"
@ -160,7 +160,7 @@ namespace :'2018_12_03_finish_piece_jointe_transfer' do
def remove_unused_openstack_objects
rake_puts "Remove unused files"
bar = RakeProgressbar.new(new_pjs.count.to_i)
progress = ProgressReport.new(new_pjs.count.to_i)
removed_keys = []
new_pjs.files.each do |file|
if !ActiveStorage::Blob.exists?(key: file.key)
@ -170,9 +170,9 @@ namespace :'2018_12_03_finish_piece_jointe_transfer' do
end
end
bar.inc
progress.inc
end
bar.finished
progress.finish
if verbose?
rake_puts "Removed #{removed_keys.count} unused objects\n#{removed_keys.join(', ')}"

View file

@ -8,3 +8,55 @@ def rake_puts(*args)
puts(*args)
end
end
def rake_print(*args)
if Rake.verbose
print(*args)
end
end
class ProgressReport
def initialize(total)
@start = Time.zone.now
rake_puts
set_progress(total: total, count: 0)
end
def inc
set_progress(count: @count + 1)
if @per_10_000 % 10 == 0
print_progress
end
end
def finish
if @count > 0 && @per_10_000 != 10_000
set_progress(total: @count)
print_progress
end
rake_puts
end
def set_progress(total: nil, count: nil)
if total.present?
@total = total
end
if count.present?
@count = count
@total = [@count, @total].max
end
@per_10_000 = 10_000 * @count / @total
end
def print_progress
elapsed = Time.zone.now - @start
percent = sprintf('%5.1f%%', @per_10_000 / 100.0)
total = @total.to_s
count = @count.to_s.rjust(total.length)
rake_print("\r#{percent} (#{count}/#{total}) [#{format_duration(elapsed)}/#{format_duration(elapsed * 10_000 / @per_10_000)}]")
end
def format_duration(seconds)
Time.at(seconds).utc.strftime('%H:%M:%S')
end
end