@@ -7,8 +7,21 @@ def perform(*args)
77 document = Document . find_by ( id : document_id )
88 return unless document
99
10- # Update cached word count for the document regardless of how often this is called
11- new_word_count = document . computed_word_count
10+ # Load body once (needed for potential fallback and revision)
11+ body_text = document . body || ""
12+ new_word_count = 0
13+
14+ begin
15+ # Try the accurate (but potentially memory-intensive) count first
16+ new_word_count = document . computed_word_count # Let it load body internally
17+ rescue StandardError => e
18+ # Log the error for visibility
19+ Rails . logger . warn ( "SaveDocumentRevisionJob: Failed accurate word count for Document #{ document_id } : #{ e . message } . Falling back to basic count." )
20+ # Fallback to basic count if accurate one fails (e.g., NoMemoryError or other issues)
21+ new_word_count = body_text . split . size
22+ end
23+
24+ # Update cached word count for the document
1225 document . update ( cached_word_count : new_word_count )
1326
1427 # Save a WordCountUpdate for this document for today
@@ -28,7 +41,7 @@ def perform(*args)
2841 # Store the document information as-is
2942 document . document_revisions . create! (
3043 title : document . title ,
31- body : document . body ,
44+ body : body_text , # Use the body_text we already loaded
3245 synopsis : document . synopsis ,
3346 universe_id : document . universe_id ,
3447 notes_text : document . notes_text ,
0 commit comments