Extract Organization#conditionally_update_articles to worker (#18956)

* Extract conditionally_update_articles to worker

* Split to individual articles on workergits

* Fix some failed specs

* Fix another spec
This commit is contained in:
Fernando Valverde 2023-01-18 11:58:57 -06:00 committed by GitHub
parent 83afd9f882
commit c24e227f55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 1 deletions

View file

@ -138,7 +138,8 @@ class Organization < ApplicationRecord
def conditionally_update_articles
return unless Article::ATTRIBUTES_CACHED_FOR_RELATED_ENTITY.detect { |attr| saved_change_to_attribute?(attr) }
articles.each(&:save)
article_ids = articles.ids.map { |id| [id] }
Organizations::SaveArticleWorker.perform_bulk(article_ids)
end
def bust_cache

View file

@ -0,0 +1,11 @@
module Organizations
class SaveArticleWorker
include Sidekiq::Job
sidekiq_options queue: :high_priority
def perform(article_id)
Article.find(article_id).save
end
end
end

View file

@ -273,6 +273,8 @@ RSpec.describe Organization do
organization.save!
end.to change { organization.reload.profile_image_url }
sidekiq_perform_enqueued_jobs
# I want to collect reloaded versions of the organization's articles so I can see their
# cached organization profile image
articles_profile_image_urls = organization.articles
@ -304,6 +306,8 @@ RSpec.describe Organization do
organization.update(name: "ACME Org")
sidekiq_perform_enqueued_jobs
expect(article.reload.reading_list_document).not_to eq(old_reading_list_document)
end

View file

@ -28,6 +28,9 @@ RSpec.describe Organizations::Delete, type: :service do
it "removes the organization name from the .reading_list_document after destroy" do
org.update(name: "ACME")
sidekiq_perform_enqueued_jobs
expect(article.reload.reading_list_document).to include("acme")
described_class.call(org)

View file

@ -0,0 +1,23 @@
require "rails_helper"
RSpec.describe Organizations::SaveArticleWorker do
describe "perform" do
let(:worker) { subject }
let(:organization) { create(:organization) }
let!(:articles) { (1..3).map { |_a| create(:article, organization: organization) } } # rubocop:disable RSpec/LetSetup
describe "save articles with worker" do
it "on organization slug change" do
new_slug = "newSlug"
organization.update(slug: new_slug)
sidekiq_perform_enqueued_jobs
organization.articles.each do |article|
# Articles were updated
expect(article.created_at).not_to eq(article.updated_at)
end
end
end
end
end