Move exporting into a job (#2299)

This commit is contained in:
rhymes 2019-04-16 20:01:00 +02:00 committed by Mac Siri
parent d39d30b8fd
commit 3b60a59ef8
4 changed files with 43 additions and 7 deletions

View file

@ -27,7 +27,7 @@ class UsersController < ApplicationController
notice = "Your profile was successfully updated."
if @user.export_requested?
notice += " The export will be emailed to you shortly."
Exporter::Service.new(@user).delay.export(send_email: true)
ExportContentJob.perform_later(@user.id)
end
cookies.permanent[:user_experience_level] = @user.experience_level.to_s if @user.experience_level.present?
follow_hiring_tag(@user)

View file

@ -0,0 +1,8 @@
class ExportContentJob < ApplicationJob
queue_as :export_content
def perform(user_id, exporter = Exporter::Service)
user = User.find_by(id: user_id)
exporter.new(user).export(send_email: true) if user
end
end

View file

@ -0,0 +1,26 @@
require "rails_helper"
RSpec.describe ExportContentJob, type: :job do
include_examples "#enqueues_job", "export_content", 1
describe "#perform_now" do
let(:exporter_service) { double }
let(:exporter) { double }
let(:user) { create(:user) }
before do
allow(exporter).to receive(:export)
allow(exporter_service).to receive(:new).and_return(exporter)
end
it "calls the service" do
described_class.perform_now(user.id, exporter_service)
expect(exporter).to have_received(:export).once
end
it "doesn't call the service if non existent user ID is given" do
described_class.perform_now(9999, exporter_service)
expect(exporter).not_to have_received(:export)
end
end
end

View file

@ -70,6 +70,10 @@ RSpec.describe "UserSettings", type: :request do
end
context "when requesting an export of the articles" do
before do
ActiveJob::Base.queue_adapter = :test
end
def send_request(flag = true)
put "/users/#{user.id}", params: {
user: { tab: "misc", export_requested: flag }
@ -99,15 +103,13 @@ RSpec.describe "UserSettings", type: :request do
end
it "sends an email" do
run_background_jobs_immediately do
expect { send_request }.to change { ActionMailer::Base.deliveries.count }.by(1)
end
ActiveJob::Base.queue_adapter = :inline
expect { send_request }.to change { ActionMailer::Base.deliveries.count }.by(1)
end
it "does not send an email if there was no request" do
run_background_jobs_immediately do
expect { send_request(false) }.not_to(change { ActionMailer::Base.deliveries.count })
end
ActiveJob::Base.queue_adapter = :inline
expect { send_request(false) }.not_to(change { ActionMailer::Base.deliveries.count })
end
end
end