-
Notifications
You must be signed in to change notification settings - Fork 75
Q&Aを自動的にクローズする機能のバグ修正 #9024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Q&Aを自動的にクローズする機能のバグ修正 #9024
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 5 additions & 9 deletions
14
app/controllers/scheduler/daily/auto_close_questions_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Scheduler | ||
| module Daily | ||
| class AutoCloseQuestionsController < ApplicationController | ||
| def show | ||
| QuestionAutoCloser.post_warning | ||
| QuestionAutoCloser.close_and_select_best_answer | ||
| head :ok | ||
| end | ||
| end | ||
| class Scheduler::Daily::AutoCloseQuestionsController < SchedulerController | ||
| def show | ||
| QuestionAutoCloser.post_warning | ||
| QuestionAutoCloser.close_and_select_best_answer | ||
| head :ok | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
QuestionAutoCloser内部のN+1/並行実行による重複発生の可能性を軽減できるか確認を。
呼び出し先では
Question.not_solved.find_each+question.answers.select/any?でRuby側フィルタを行っており、大量件数でN+1やメモリ負荷になり得ます。また同時起動時に警告コメントの重複生成が理論上は起こり得ます(判定→作成が非原子的)。性能・整合性要件次第ですが、将来的に以下の改善余地があります。includes(:answers)等で事前ロード。with_lockの活用、もしくは何らかの冪等キーでのUpsert。リポジトリ全体での該当箇所を洗い出す補助スクリプトです(実際のリファクタは別PRで十分です):
🏁 Script executed:
Length of output: 974
要対応: QuestionAutoCloser の N+1 と同時実行による重複生成リスク(確認済み))
リポジトリ内検索の結果、QuestionAutoCloser 内で
find_eachと Ruby 側フィルタ(answers.select/answers.any?)が確認されました。大量データで N+1 やメモリ負荷、かつ「判定 → 作成」が非原子的なため同時実行時に警告コメントが重複して生成されるリスクがあります。該当箇所(確認済み)
QuestionAutoCloser.post_warning/QuestionAutoCloser.close_and_select_best_answerfind_eachのループ(lines 16, 27)question.answers.select(line 60)question.answers.any?(line 67)推奨対応(優先順)
answers.where(...).exists?/joinsやサブクエリ/EXISTS を利用)。LIKE/正規表現はDB依存なので注意。includes(:answers)や必要カラムのみを事前ロードする(または join + distinct で絞る)。create_or_find_by/upsert、あるいはwith_lock/トランザクション/アドバイザリロックで原子的に処理する。find_eachのバッチサイズ調整や不要オブジェクト生成の抑制。具体的なリファクタは別PRで問題ありませんが、上記を踏まえて優先的に対応してください。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai
過去の運用実績からすると、未解決の質問(
Question.not_solved)は5件以下、一つの質問に対する回答は平均数件で多くても数十件なので、メモリ負荷はあまり意識しなくて大丈夫です。ただし、指摘の通り現状のコードは非効率なのでN+1対策とロジックのSQL側への移行は別PRで検討します。
app/controllers/scheduler/daily/auto_close_questions_controller.rbのメソッドはdailyで自動的に一度だけ呼び出されるため同時実行リスクはありません。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryufuta, 運用実績に基づく詳細な情報をありがとうございます。
了解しました:
本PRはバグ修正が目的ですので、パフォーマンス最適化は別PRで対応するアプローチが適切だと思います。貴重なコンテキストをご共有いただき、ありがとうございました!
🧠 Learnings used