Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ def post_draft
# Instead, as the work in this context is reduced its first chapter, we copy the value directly
@work.word_count = @work.first_chapter.word_count
@work.save
# AO3-6272: write chapter count to cache after save to avoid stale replica data
Rails.cache.write(@work.key_for_chapter_posted_counting(@work), 1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry to say it is possible to create a multi chapter draft so just using one is not a great idea, if you did use 1 which I think you shouldn't you could have a very short expiry as the issue is mostly that the wrong answer stays for a long time.


if !@collection.nil? && @collection.moderated?
redirect_to work_path(@work), notice: ts('Work was submitted to a moderated collection. It will show up in the collection once approved.')
Expand Down
25 changes: 25 additions & 0 deletions spec/controllers/works/drafts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@
)
end

# AO3-6272: In production, a read replica may return stale data
# right after posting, causing the chapter count to cache as 0.
it "should show the correct chapter count after posting the draft" do
draft = create(:draft, authors: [drafts_user.default_pseud])

original_cache = Rails.cache
Rails.cache = ActiveSupport::Cache::MemoryStore.new

begin
put :post_draft, params: { id: draft.id }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem described on Jira is not with the post_draft action, the issue specifically states that the "Post Draft" button was not used


draft.reload
expect(draft.first_chapter.posted).to be true

# Simulate a stale read replica returning 0 posted chapters.
# The cache should already hold the correct value from post_draft.
stale_relation = double(count: 0)
allow(draft).to receive(:chapters).and_return(double(posted: stale_relation))

expect(draft.number_of_posted_chapters).to eq(1)
ensure
Rails.cache = original_cache
end
end

it "should only count the words of the first, published, chapter after posting the draft" do
draft = create(:draft, authors: [drafts_user.default_pseud])
create(:chapter, :draft, work: draft)
Expand Down
Loading