Skip to content

AO3-6272 Fix chapter count caching 0 after posting draft#5833

Open
pmonfort wants to merge 1 commit into
otwcode:masterfrom
pmonfort:AO3-6272-chapter-count-zero-on-post
Open

AO3-6272 Fix chapter count caching 0 after posting draft#5833
pmonfort wants to merge 1 commit into
otwcode:masterfrom
pmonfort:AO3-6272-chapter-count-zero-on-post

Conversation

@pmonfort
Copy link
Copy Markdown
Contributor

Descripción:

Issue

https://otwarchive.atlassian.net/browse/AO3-6272

Purpose

When posting a work from draft, the chapter count sometimes displays "0/1" instead of "1/1". This happens because number_of_posted_chapters uses Rails.cache.fetch, and the block inside it queries chapters.posted.count , which in production can hit a read replica that hasn't yet received the newly posted chapter. The stale 0 gets cached, and subsequent reads return the wrong count until the cache expires.

This PR writes the correct chapter count (1) directly to the cache immediately after saving in post_draft, so number_of_posted_chapters returns the cached value instead of querying a potentially stale replica.

Testing Instructions

This bug is not reproducible locally (no read replicas in development). The automated test simulates the stale replica by stubbing chapters.posted.count to return 0 and verifying that the cached value from post_draft takes precedence.

References

Related to AO3-6273 (word count blank after posting), which has a similar production environment but a different root cause.

Credit

Pablo Monfort (he/him)

@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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants