dryrun
is different depending on the writer, an accident may occur when executing a task in commercial use, so it is better if it can be unified.etc
lib/tasks/issue_6885.rake
require_relative 'helpers/all_user_name_update_helper.rb'
namespace :issue_6885 do
desc 'This is a sample'
task all_user_name_update: :environment do
helper = AllUserNameUpdateHelper.new
helper.main
end
end
lib/tasks/helpers/all_user_name_update_helper.rb
require_relative 'rake_helper_template'
class AllUserNameUpdateHelper < RakeHelperTemplate
NEW_NAME = 'bar'
def main
template do |logger|
all_users = get_all_user
logger.info("Number of target users: #{all_users.size}")
if all_users.blank?
logger.info('The process ends because the number of target users was 0.')
return
end
all_users.each { |user| update_name(user, NEW_NAME) }
end
end
private
def get_all_user
User.all
end
def update_name(user, new_name)
user.update!(name: new_name)
user
end
end
lib/tasks/helpers/rake_helper_template.rb
class RakeHelperTemplate
#Also performs standard output
def make_logger(log_file_path)
logger = ActiveSupport::Logger.new(log_file_path)
stdout_logger = ActiveSupport::Logger.new(STDOUT)
broadcast_logger = ActiveSupport::Logger.broadcast(stdout_logger)
logger.extend(broadcast_logger)
logger.formatter = Logger::Formatter.new
logger
end
#Make the log file name the same as the task name (replace it because it is better not to use a colon)
#Cases called from other than the execution of the rake task (eg:spec) should be taken into consideration
def make_log_file_path
task_name = Rake.try(:application)&.top_level_tasks&.[](0)&.gsub(':','_') || Rails.env
log_file_name = "#{task_name}.log"
Rails.root.join('log', log_file_name)
end
def template
log_file_path = make_log_file_path
#Be sure to dryrun unless you explicitly pass the string false
is_dryrun = ENV['is_dryrun'] != 'false'
logger = make_logger(log_file_path)
logger.info("Start. is_dryrun: #{is_dryrun}")
ActiveRecord::Base.transaction do
yield(logger)
raise ActiveRecord::Rollback if is_dryrun
end
logger.info("Finish. log_file_path: #{log_file_path}")
end
end
When making a dry run
$ is_dryrun=false bundle exec rake issue_6885:all_user_name_update
I, [2020-09-27T13:33:27.229764 #13040] INFO -- : Start. is_dryrun: true
I, [2020-09-27T13:33:27.556890 #13040] INFO -- :Number of target users: 1
I, [2020-09-27T13:33:27.755933 #13040] INFO -- : Finish. log_file_path: /app/log/issue_6885_all_user_name_update.log
dryrun
and logger
(part of) so that you don't have to define them every time you create a rake task.Once you have a template, you can concentrate on what you want to do like all_user_name_update_helper.rb! (^ ○ ^)
Recommended Posts