Minor fixes to Jobs::BulkInvite.

This commit is contained in:
Guo Xiang Tan 2018-08-30 15:34:58 +08:00
parent 092fb0c170
commit 4f81d4cc8e

View File

@ -8,6 +8,7 @@ module Jobs
attr_accessor :current_user attr_accessor :current_user
def initialize def initialize
super
@logs = [] @logs = []
@sent = 0 @sent = 0
@failed = 0 @failed = 0
@ -38,13 +39,13 @@ module Jobs
@sent += 1 @sent += 1
else else
# invalid email # 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 @failed += 1
end end
end end
end end
rescue Exception => e rescue Exception => e
log "Bulk Invite Process Failed -- '#{e.message}'" save_log "Bulk Invite Process Failed -- '#{e.message}'"
@failed += 1 @failed += 1
ensure ensure
file.close file.close
@ -61,7 +62,7 @@ module Jobs
group_ids.push(group_detail.id) group_ids.push(group_detail.id)
else else
# invalid group # 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 @failed += 1
end end
} }
@ -74,7 +75,7 @@ module Jobs
if topic_id if topic_id
topic = Topic.find_by_id(topic_id) topic = Topic.find_by_id(topic_id)
if topic.nil? 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 @failed += 1
end end
end end
@ -88,16 +89,12 @@ module Jobs
begin begin
Invite.invite_by_email(email, @current_user, topic, group_ids) Invite.invite_by_email(email, @current_user, topic, group_ids)
rescue => e 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 @sent -= 1
@failed += 1 @failed += 1
end end
end end
def log(message)
save_log(message)
end
def save_log(message) def save_log(message)
@logs << "[#{Time.now}] #{message}" @logs << "[#{Time.now}] #{message}"
end end
@ -105,9 +102,19 @@ module Jobs
def notify_user def notify_user
if @current_user if @current_user
if (@sent > 0 && @failed == 0) 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 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 end
end end