run at any time in a console • Common Rails tasks: ◦ rake db:reset, rake db:create, rake db:migrate ◦ rake -T ◦ rake routes • Custom task: desc 'Rake task description' task :name_of_task do # Your code goes here end
'Import users from Mailchimp' task :import_users do puts 'Importing users from Mailchimp' result = Mailchimp::ImportUsers.call puts "Successfully imported #{result} users!" end end
do query = Cleanup::FixBadFees::Query.call progress_bar = ProgressBar.create( title: 'Fixing bad fees by adding a discount', total: query.count ) # ... end end namespace :cleanup desc 'Cleanup bad fees' task :fix_bad_fees do # ... result = Cleanup::FixBadFees::Task.call(query: query) do progress_bar.increment end puts "Successfully fixed #{result} fees!" end end
Define bottlenecks ◦ Reading from large file or from multiple database tables ◦ Calling external services ◦ Saving many rows to the database • Use database close to production (locally, staging)