Skip to content

Move recalculate analyze/upsert off the main thread#205

Merged
coji merged 1 commit intomainfrom
improve/worker-thread-analyze
Mar 17, 2026
Merged

Move recalculate analyze/upsert off the main thread#205
coji merged 1 commit intomainfrom
improve/worker-thread-analyze

Conversation

@coji
Copy link
Owner

@coji coji commented Mar 17, 2026

Summary

  • run recalculate analyze and upsert phases in worker threads so the web process is not blocked by synchronous better-sqlite3 work
  • add a worker import resolver for tsx/Node worker execution so existing project aliases and extensionless local imports still load
  • batch reviewer replacement writes to reduce per-PR transaction overhead during upsert

Validation

  • ran node --import tsx batch/cli.ts recalculate aScaf_RvvFiOJWD3m8X32
  • confirmed TechTalk recalculate completed successfully with 338 PRs updated

Summary by CodeRabbit

リリースノート

  • New Features

    • 分析処理とアップサート処理を独立したワーカーで実行できるようになりました。
    • モジュール解決のエイリアス対応を追加しました。
    • SQLiteのロック発生時に再試行と通知を行う仕組みを導入しました。
  • Refactor

    • 分析〜アップサートの処理フローをワーカー中心に切り替え、処理のタイミング計測を追加しました。
    • レビュー担当者の更新をバッチ置換に変更しました。
  • Chores

    • ワーカーのビルドをビルドプロセスに統合しました。
  • Tests

    • ワーカー実行設定に関する単体テストを追加しました。

@coderabbitai
Copy link

coderabbitai bot commented Mar 17, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 25dbeccf-d6c9-4aa4-b250-747d3786e286

📥 Commits

Reviewing files that changed from the base of the PR and between a552c93 and 18b5a93.

📒 Files selected for processing (11)
  • app/services/db.server.ts
  • app/services/jobs/analyze-worker.ts
  • app/services/jobs/crawl.server.ts
  • app/services/jobs/path-alias-hooks.mjs
  • app/services/jobs/run-in-worker.test.ts
  • app/services/jobs/run-in-worker.ts
  • app/services/jobs/shared-steps.server.ts
  • app/services/jobs/upsert-worker.ts
  • app/services/tenant-db.server.ts
  • batch/db/mutations.ts
  • package.json

📝 Walkthrough

Walkthrough

このPRはPR分析とDBアップサートをワーカースレッドへ分離する変更を導入し、パスエイリアス解決フック、ワーカー実行ユーティリティ、ワーカー本体(analyze/upsert)、ビルドスクリプト、及びレビュアーのバッチ置換を追加します。

Changes

Cohort / File(s) Summary
Workers (implementations)
app/services/jobs/analyze-worker.ts, app/services/jobs/upsert-worker.ts
新規ワーカースクリプト。analyze-workerはPR分析をワーカースレッドで実行して結果をpostMessageで返却、upsert-workerはDB向けのupsert処理をワーカースレッドで実行。
Worker runtime & hooks
app/services/jobs/run-in-worker.ts, app/services/jobs/path-alias-hooks.mjs, app/services/jobs/run-in-worker.test.ts
ワーカー起動/再試行/SQLite-BUSYハンドリングを実装するランタイムAPI(runAnalyzeInWorker/runUpsertInWorker含む)と、~/等のモジュールパスエイリアス解決フックを追加。getWorkerRuntimeのユニットテストを追加。
Workflow integration
app/services/jobs/shared-steps.server.ts, app/services/jobs/crawl.server.ts
既存のshared-stepsをワーカーベースに置換(分析→アップサートをワーカー経由に)。最終化でのWAL checkpointがTRUNCATE→PASSIVEに変更。処理時間計測ラッパーを導入。
DB mutations & tenant DB configs
batch/db/mutations.ts, app/services/db.server.ts, app/services/tenant-db.server.ts
batchReplacePullRequestReviewersを追加してレビュアーをチャンク単位で置換するように変更。SQLite接続にbusy_timeout(5000ms)を設定。
Build & tests
package.json, app/services/jobs/run-in-worker.test.ts
build:workersスクリプトを追加しメインbuildに組み込み。ワーカーRuntimeテストを追加。

Sequence Diagram

sequenceDiagram
    participant Main as Main Thread
    participant AnalyzeW as Analyze Worker
    participant PRBuilder as Store/PR Builder
    participant UpsertW as Upsert Worker
    participant DB as Database

    Main->>AnalyzeW: runAnalyzeInWorker({ organizationId, repositoryId, ... })
    activate AnalyzeW
    AnalyzeW->>PRBuilder: initialize store (org/repo)
    PRBuilder-->>AnalyzeW: store ready
    AnalyzeW->>PRBuilder: buildPullRequests(input, filterPrNumbers?)
    PRBuilder-->>AnalyzeW: { pulls, reviews, reviewers, reviewResponses }
    AnalyzeW-->>Main: postMessage(AnalyzeResult)
    deactivate AnalyzeW

    Main->>UpsertW: runUpsertInWorker({ organizationId, pulls, reviews, reviewers })
    activate UpsertW
    UpsertW->>DB: upsertAnalyzedData(orgId, data)
    DB->>DB: batchReplacePullRequestReviewers(rows)
    DB-->>UpsertW: { ok: true }
    UpsertW-->>Main: postMessage({ ok: true })
    deactivate UpsertW
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐰 ワーカーに飛び乗り、コードの野へ、
タイルダの道をすいすい渡り、
分析は踊り、データは整う、
バッチで整えて、静かに返す、
さあビルドで出発、春風のよう!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 27.27% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: moving analyze/upsert operations to worker threads instead of running them on the main thread.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch improve/worker-thread-analyze
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coji coji force-pushed the improve/worker-thread-analyze branch 2 times, most recently from a552c93 to 7a30be2 Compare March 17, 2026 09:46
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@app/services/jobs/run-in-worker.ts`:
- Around line 61-65: The exit handler currently ignores exit code 1
(worker.on('exit', (code) => { if (code !== 0 && code !== 1) ... })), so a
worker that exits with code 1 won't cause rejection; update the check in the
worker.on('exit') callback used in run-in-worker.ts to treat any non-zero code
as an error (e.g., use code !== 0), and ensure the reject call includes the exit
code in the Error message to preserve context.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bdb4ffd8-dcc7-4eed-81d4-96c0d18c5ed0

📥 Commits

Reviewing files that changed from the base of the PR and between e7dcc01 and a552c93.

📒 Files selected for processing (7)
  • app/services/jobs/analyze-worker.ts
  • app/services/jobs/path-alias-hooks.mjs
  • app/services/jobs/run-in-worker.ts
  • app/services/jobs/shared-steps.server.ts
  • app/services/jobs/upsert-worker.ts
  • batch/db/mutations.ts
  • package.json

@coji coji force-pushed the improve/worker-thread-analyze branch 2 times, most recently from 6242d1d to 34b3d3a Compare March 17, 2026 09:57
@coji coji force-pushed the improve/worker-thread-analyze branch from 34b3d3a to 18b5a93 Compare March 17, 2026 10:04
@coji coji merged commit ce1ed2f into main Mar 17, 2026
6 checks passed
@coji coji deleted the improve/worker-thread-analyze branch March 17, 2026 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant