DEV: optimize bulk invite process

This commit is contained in:
Arpit Jalan 2019-06-12 14:35:21 +05:30
parent f0846ea7cf
commit 7b66f8fb46
4 changed files with 28 additions and 11 deletions

View File

@ -176,20 +176,22 @@ class InvitesController < ApplicationController
begin
file = params[:file] || params[:files].first
if File.read(file.tempfile).scan(/\n/).count.to_i > 50000
return render json: failed_json.merge(errors: [I18n.t("bulk_invite.max_rows")]), status: 422
count = 0
invites = []
max_bulk_invites = SiteSetting.max_bulk_invites
CSV.foreach(file.tempfile) do |row|
count += 1
invites.push({ email: row[0], groups: row[1], topic_id: row[2] }) if row[0].present?
break if count >= max_bulk_invites
end
invites = []
CSV.foreach(file.tempfile) do |row|
invite_hash = { email: row[0], groups: row[1], topic_id: row[2] }
if invite_hash[:email].present?
invites.push(invite_hash)
end
end
if invites.present?
Jobs.enqueue(:bulk_invite, invites: invites, current_user_id: current_user.id)
if count >= max_bulk_invites
render json: failed_json.merge(errors: [I18n.t("bulk_invite.max_rows", max_bulk_invites: max_bulk_invites)]), status: 422
else
render json: success_json
end
else
render json: failed_json.merge(errors: [I18n.t("bulk_invite.error")]), status: 422
end

View File

@ -218,7 +218,7 @@ en:
bulk_invite:
file_should_be_csv: "The uploaded file should be of csv format."
max_rows: "Maximum of 50,000 invites can be sent at a time. Try splitting the file in smaller parts."
max_rows: "First %{max_bulk_invites} invites has been sent. Try splitting the file in smaller parts."
error: "There was an error uploading that file. Please try again later."
topic_invite:

View File

@ -1912,6 +1912,10 @@ uncategorized:
default: false
hidden: true
max_bulk_invites:
default: 50000
hidden: true
user_preferences:
default_email_digest_frequency:
enum: "DigestEmailSiteSetting"

View File

@ -508,6 +508,17 @@ describe InvitesController do
expect(response.status).to eq(200)
expect(Jobs::BulkInvite.jobs.size).to eq(1)
end
it "sends limited invites at a time" do
SiteSetting.max_bulk_invites = 3
sign_in(Fabricate(:admin))
post "/invites/upload_csv.json", params: { file: file, name: filename }
expect(response.status).to eq(422)
expect(Jobs::BulkInvite.jobs.size).to eq(1)
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("bulk_invite.max_rows", max_bulk_invites: SiteSetting.max_bulk_invites))
end
end
end
end