Move RssReaderFetchUserJob to Sidekiq (#5685) [deploy]
* Create RssReaderFetchUserWorker * Create RssReaderFetchUserWorker spec * Update reference to RssReaderFetchUserWorker * Update spec for clarity - Update fake user_id to 9999 - Update return for user.feed_url * Add feed_url check before calling worker * Update random user_id for queue test * Update fake user_id argument in spec
This commit is contained in:
parent
c3529d4b10
commit
62f3e31698
3 changed files with 52 additions and 1 deletions
|
|
@ -19,7 +19,7 @@ class UsersController < ApplicationController
|
|||
def update
|
||||
set_tabs(params["user"]["tab"])
|
||||
if @user.update(permitted_attributes(@user))
|
||||
RssReaderFetchUserJob.perform_later(@user.id)
|
||||
RssReaderFetchUserWorker.perform_async(@user.id) if @user.feed_url.present?
|
||||
notice = "Your profile was successfully updated."
|
||||
if config_changed?
|
||||
notice = "Your config has been updated. Refresh to see all changes."
|
||||
|
|
|
|||
11
app/workers/rss_reader_fetch_user_worker.rb
Normal file
11
app/workers/rss_reader_fetch_user_worker.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class RssReaderFetchUserWorker
|
||||
include Sidekiq::Worker
|
||||
|
||||
sidekiq_options queue: :medium_priority
|
||||
|
||||
def perform(user_id)
|
||||
user = User.find_by(id: user_id)
|
||||
|
||||
RssReader.new.fetch_user(user) if user&.feed_url.present?
|
||||
end
|
||||
end
|
||||
40
spec/workers/rss_reader_fetch_user_worker_spec.rb
Normal file
40
spec/workers/rss_reader_fetch_user_worker_spec.rb
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe RssReaderFetchUserWorker, type: :worker do
|
||||
let(:worker) { subject }
|
||||
|
||||
# passing in a random user_id argument since the worker itself won't be executed
|
||||
include_examples "#enqueues_on_correct_queue", "medium_priority", [456]
|
||||
|
||||
describe "#perform_now" do
|
||||
let(:rss_reader_service) { instance_double(RssReader) }
|
||||
|
||||
before do
|
||||
allow(RssReader).to receive(:new).and_return(rss_reader_service)
|
||||
allow(rss_reader_service).to receive(:fetch_user)
|
||||
end
|
||||
|
||||
context "when user found and feed_url present" do
|
||||
let(:user) { double }
|
||||
|
||||
before do
|
||||
allow(User).to receive(:find_by).and_return(user)
|
||||
allow(user).to receive(:feed_url).and_return(:feed_url)
|
||||
allow(user).to receive(:id)
|
||||
end
|
||||
|
||||
it "calls the service" do
|
||||
worker.perform(user.id)
|
||||
expect(rss_reader_service).to have_received(:fetch_user).with(user).once
|
||||
end
|
||||
end
|
||||
|
||||
context "when no user found" do
|
||||
it "does not call the service" do
|
||||
allow(User).to receive(:find_by)
|
||||
worker.perform(9999)
|
||||
expect(rss_reader_service).not_to have_received(:fetch_user)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue