Basic requirements: when using Rake to process data, we need to know the current processing status and progress.

Before, my processing method is: use puts to print some unique information so that we can process the progress through the current. Finally, Googled found a good gem. It may be more friendly to help us output the data processing status and schedule. So recommend it to everyone today.


Gem - ruby-processbar  this Gem is also very easy to use.

  • Add gem 'ruby-processbar' to Gemfile
gem 'ruby-progressbar'
  • Add 'ruby-processbar' to your rake file
require 'ruby-progressbar'

namespace :icmoc do
  namespace :articles do
    desc "Add categories to article"
    task add_categories: :environment do
      PROGRESS_BAR_FORMAT = '%t %a %e %P% %B Processed: %c from %C'
      bar = ProgressBar.create(title: "Add categories to article", format: PROGRESS_BAR_FORMAT, starting_at: 0, total: Article.count)

      Article.all.find_each do |article|
        bar.increment

        article.add_categories!
      end
    end
  end
end
  • run rake command from your terminal
➜ blog$ bundle exec rake icmoc:articles:add_categories --trace
** Invoke load_app (first_time)
** Execute load_app
** Invoke icmoc:articles:add_categories (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute icmoc:articles:add_categories
Add categories to article Time: 00:02:26  ETA: 01:23:52  2.82% ==                                                                                   Processed: 122001 from 4312318

You should see the last line of output from the above command. (description, total count, processed count, percentage)

Please share it with me if you have other gems or a good way.

Thanks for reading! 


Encore