A platform for community discussion. Free, open, simple.
PERF: optimize GitHub PR onebox rebaking (#36739)
Régis Hanol67985bff528e
diff --git a/app/models/post.rb b/app/models/post.rbindex 78624a14123..79d8ae04402 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -827,8 +827,13 @@ class Post < ActiveRecord::Baseproblemsend- def rebake!(invalidate_broken_images: false, invalidate_oneboxes: false, priority: nil)- new_cooked = cook(raw, topic_id: topic_id, invalidate_oneboxes: invalidate_oneboxes)+ def rebake!(+ invalidate_broken_images: false,+ invalidate_oneboxes: false,+ priority: nil,+ skip_publish_rebaked_changes: false+ )+ new_cooked = cook(raw, topic_id:, invalidate_oneboxes:)old_cooked = cookedupdate_columns(cooked: new_cooked, baked_at: Time.zone.now, baked_version: BAKED_VERSION)@@ -840,17 +845,16 @@ class Post < ActiveRecord::Basepost_hotlinked_media.upload_create_failed.destroy_allend- # Extracts urls from the bodyTopicLink.extract_from(self)QuotedPost.extract_from(self)- # make sure we trigger the post process- trigger_post_process(bypass_bump: true, priority: priority)+ trigger_post_process(bypass_bump: true, priority:)# Skip publishing if invalidating oneboxes - the ProcessPost job will# publish :revised after fetching fresh oneboxes, avoiding an intermediate# state where raw links are shown before oneboxes are loaded.- publish_change_to_clients!(:rebaked) unless invalidate_oneboxes+ should_publish = !skip_publish_rebaked_changes && !invalidate_oneboxes+ publish_change_to_clients!(:rebaked) if should_publishnew_cooked != old_cookedenddiff --git a/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb b/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rbindex dd4348bfa7e..50e484f59d5 100644--- a/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb+++ b/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb@@ -5,48 +5,38 @@ module Jobssidekiq_options queue: "low"def execute(args)- pr_url = args[:pr_url]- return if pr_url.blank?+ url = args[:pr_url]+ return if url.blank?- rebake_posts(pr_url)- rebake_chat_messages(pr_url) if SiteSetting.chat_enabled+ # invalidate & refresh the onebox cache for this PR URL+ Oneboxer.preview(url, invalidate_oneboxes: true)++ rebake_posts(url)+ rebake_chat_messages(url) if SiteSetting.chat_enabledendprivate- def rebake_posts(pr_url)- post_ids =- TopicLink- .where(url: pr_url)- .or(TopicLink.where("url LIKE ?", "#{pr_url}%"))- .select(:post_id)+ def rebake_posts(url)+ post_ids = TopicLink.where(url:).or(TopicLink.where("url LIKE ?", "#{url}%")).select(:post_id)Post.where(id: post_ids)- .find_each do |post|- next unless has_github_pr_onebox?(post.cooked, pr_url)- post.rebake!(invalidate_oneboxes: true, priority: :low)- end+ .where("cooked LIKE ?", "%githubpullrequest%#{url}%")+ .find_each { |post| post.rebake!(priority: :low, skip_publish_rebaked_changes: true) }end- def rebake_chat_messages(pr_url)+ def rebake_chat_messages(url)message_ids =::Chat::MessageLink- .where(url: pr_url)- .or(::Chat::MessageLink.where("url LIKE ?", "#{pr_url}%"))+ .where(url:)+ .or(::Chat::MessageLink.where("url LIKE ?", "#{url}%")).select(:chat_message_id)::Chat::Message.where(id: message_ids)- .find_each do |message|- next unless has_github_pr_onebox?(message.cooked, pr_url)- message.rebake!(invalidate_oneboxes: true, priority: :low, skip_notifications: true)- end- end-- def has_github_pr_onebox?(cooked, pr_url)- # quick & dirty check to avoid doing unnecessary rebakes- cooked.present? && cooked.include?("githubpullrequest") && cooked.include?(pr_url)+ .where("cooked LIKE ?", "%githubpullrequest%#{url}%")+ .find_each { |message| message.rebake!(priority: :low, skip_notifications: true) }endendenddiff --git a/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb b/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rbindex 6b6cf69e163..060d31b14e6 100644--- a/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb+++ b/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb@@ -15,19 +15,27 @@ RSpec.describe Jobs::RebakeGithubPrPosts doenddescribe "#execute" do+ before { allow(Oneboxer).to receive(:preview) }+it "does nothing with blank or missing pr_url" doexpect { described_class.new.execute(pr_url: nil) }.not_to raise_errorexpect { described_class.new.execute(pr_url: "") }.not_to raise_errorend+ it "invalidates the onebox cache for the PR URL" do+ described_class.new.execute(pr_url:)++ expect(Oneboxer).to have_received(:preview).with(pr_url, invalidate_oneboxes: true)+ end+it "rebakes posts with full GitHub PR oneboxes" docreate_post_with_link(<<~HTML)<aside class="onebox githubpullrequest"><a href="#{pr_url}">PR</a></aside>HTMLexpect_any_instance_of(Post).to receive(:rebake!).with(- invalidate_oneboxes: true,priority: :low,+ skip_publish_rebaked_changes: true,)described_class.new.execute(pr_url:)@@ -50,8 +58,8 @@ RSpec.describe Jobs::RebakeGithubPrPosts doTopicLink.create!(topic:, post:, user:, url: "#{pr_url}/files", domain:)expect_any_instance_of(Post).to receive(:rebake!).with(- invalidate_oneboxes: true,priority: :low,+ skip_publish_rebaked_changes: true,)described_class.new.execute(pr_url:)@@ -77,7 +85,6 @@ RSpec.describe Jobs::RebakeGithubPrPosts doHTMLexpect_any_instance_of(Chat::Message).to receive(:rebake!).with(- invalidate_oneboxes: true,priority: :low,skip_notifications: true,)diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rbindex 371e94fa6ac..116a8ae441c 100644--- a/spec/models/post_spec.rb+++ b/spec/models/post_spec.rb@@ -1371,6 +1371,12 @@ RSpec.describe Post dopost.rebake!(invalidate_oneboxes: true)end+ it "does not publish to clients when skip_publish_rebaked_changes is true" do+ post = create_post+ post.expects(:publish_change_to_clients!).never+ post.rebake!(skip_publish_rebaked_changes: true)+ end+it "uses inline onebox cache by default" doJobs.run_immediately!stub_request(:get, "http://testonebox.com/vvf").to_return(status: 200, body: <<~HTML)