From 4f81d4cc8ec3e7085d3adf683ec790a9022878dc Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 30 Aug 2018 15:34:58 +0800 Subject: [PATCH] Minor fixes to `Jobs::BulkInvite`. --- app/jobs/regular/bulk_invite.rb | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/app/jobs/regular/bulk_invite.rb b/app/jobs/regular/bulk_invite.rb index 2d6f0e6454e..fae3916cf7f 100644 --- a/app/jobs/regular/bulk_invite.rb +++ b/app/jobs/regular/bulk_invite.rb @@ -8,6 +8,7 @@ module Jobs attr_accessor :current_user def initialize + super @logs = [] @sent = 0 @failed = 0 @@ -38,13 +39,13 @@ module Jobs @sent += 1 else # invalid email - log "Invalid Email '#{csv_info[0]}' at line number '#{$INPUT_LINE_NUMBER}'" + save_log "Invalid Email '#{csv_info[0]}' at line number '#{$INPUT_LINE_NUMBER}'" @failed += 1 end end end rescue Exception => e - log "Bulk Invite Process Failed -- '#{e.message}'" + save_log "Bulk Invite Process Failed -- '#{e.message}'" @failed += 1 ensure file.close @@ -61,7 +62,7 @@ module Jobs group_ids.push(group_detail.id) else # invalid group - log "Invalid Group '#{group_name}' at line number '#{csv_line_number}'" + save_log "Invalid Group '#{group_name}' at line number '#{csv_line_number}'" @failed += 1 end } @@ -74,7 +75,7 @@ module Jobs if topic_id topic = Topic.find_by_id(topic_id) if topic.nil? - log "Invalid Topic ID '#{topic_id}' at line number '#{csv_line_number}'" + save_log "Invalid Topic ID '#{topic_id}' at line number '#{csv_line_number}'" @failed += 1 end end @@ -88,16 +89,12 @@ module Jobs begin Invite.invite_by_email(email, @current_user, topic, group_ids) rescue => e - log "Error inviting '#{email}' -- #{Rails::Html::FullSanitizer.new.sanitize(e.message)}" + save_log "Error inviting '#{email}' -- #{Rails::Html::FullSanitizer.new.sanitize(e.message)}" @sent -= 1 @failed += 1 end end - def log(message) - save_log(message) - end - def save_log(message) @logs << "[#{Time.now}] #{message}" end @@ -105,9 +102,19 @@ module Jobs def notify_user if @current_user if (@sent > 0 && @failed == 0) - SystemMessage.create_from_system_user(@current_user, :bulk_invite_succeeded, sent: @sent) + SystemMessage.create_from_system_user( + @current_user, + :bulk_invite_succeeded, + sent: @sent + ) else - SystemMessage.create_from_system_user(@current_user, :bulk_invite_failed, sent: @sent, failed: @failed, logs: @logs.join("\n")) + SystemMessage.create_from_system_user( + @current_user, + :bulk_invite_failed, + sent: @sent, + failed: @failed, + logs: @logs.join("\n") + ) end end end