Skip to content

Fix worker healthcheck cold-start CPU overhead - #59

Open
yusixian wants to merge 3 commits into
mainfrom
harness/62978e64-a0b6-4476-b298-72e213171e90
Open

yusixian wants to merge 3 commits into
mainfrom
harness/62978e64-a0b6-4476-b298-72e213171e90

Conversation

@yusixian

@yusixian yusixian commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replace the default worker healthcheck's full CLI startup with a small PostgreSQL probe. It requires a reachable database, the current instance in running state, and a heartbeat no older than 30 seconds. Failures return a nonzero exit code.

Fixes #58

Changes

  • Point the default compose worker healthcheck at dist/worker-healthcheck.js without changing its schedule.
  • Share the 30-second threshold with the existing runtime repository, and cover healthy, stale, wrong-state, missing-row, invalid-date, and connection-error cases.
  • Extend compose smoke to exercise the new probe for a healthy worker and reject a different instance, while retaining the original CLI checks.
  • Exclude the unavailable MinIO image test from the default integration command. The retained file can still be run explicitly if its pinned image is cached; other S3 backend unit tests remain in the default unit suite. Update deployment and media-cache docs so they no longer claim CI has a real MinIO compatibility baseline.

Verification

  • env -u NO_COLOR sh -c 'pnpm lint && pnpm typecheck && pnpm test' — passed on 6387002 in a clean worktree; server tests 560/560, all other package tests passed. An earlier run on the prior commit hit an unrelated 5-second reconciliation CLI smoke timeout under host load; the focused test and complete rerun passed.
  • env -u NO_COLOR pnpm test:integration — passed on d1c6e4e: 29 integration files and 205 tests passed, with the MinIO image test excluded.
  • env -u NO_COLOR pnpm compose:smoke — passed locally with the default Compose files. This exercised healthy and wrong-instance probes, the original CLI checks, worker restart, and the synthetic Telegram collection/public API flow.
  • env -u NO_COLOR pnpm --dir apps/server exec vitest run test/integration/worker-runtime.test.ts --maxWorkers=1 --testTimeout=15000 --hookTimeout=30000 — passed (2/2).
  • Isolated local comparison with a running synthetic worker limited by cgroup to 2 CPUs / 1 GiB: 10 successful probes each. Full CLI median wall 0.913 s / cgroup CPU 1.251 s; lightweight probe median wall 0.112 s / cgroup CPU 0.059 s. With three probes at 10-second intervals and 200 ms CPU sampling windows, p95 fell from 132.1% to 9.6% and max from 189.6% to 30.5%. The synthetic public API and /readyz passed after the comparison. The Docker Desktop VM had 10 CPUs / 8.2 GB, so this is container-limited evidence, not proof on the specified 2 vCPU / 1 GB host.
  • pnpm --filter @koharu-suite/server build, bash -n scripts/compose-smoke/run.sh, git diff --check, and Compose config validation — passed.
  • Previous CI run attempts 1 and 2 passed lint, typecheck, and unit tests, then stopped when Docker Hub denied the pinned MinIO image (HTTP 404). Validate on 6387002 passed, including the remaining PostgreSQL integration tests, Storybook interactions, CLI smoke, Compose validation, and default Compose smoke. The intermediate run on d1c6e4e was cancelled when the documentation commit was pushed. A true 2 vCPU / 1 GB host comparison and production collector/API behavior remain unverified.

Review

Independent Claude Code opus[1m] review found no blocking issue after a follow-up pass. Its four suggestions were addressed: add a real-SQL wrong-instance smoke case, share the staleness constant, emit fixed failure diagnostics, and align environment edge cases with the CLI. The worker source diff is unchanged since that review. A second bounded review confirmed that the original CLI probe remains packaged and can be restored with a Compose override. The owner accepted the bounded local verification and asked to retain this fallback. An independent review of the later integration-script exclusion found no blocking issue and identified four stale documentation claims; those were corrected in 6387002. Its follow-up review found no remaining actionable finding and confirmed the S3 unit coverage described in the docs. The final diff passed CI and independent review.

Fallback

The original node dist/cli.js health worker command is still available in the worker image and is exercised by Compose smoke. If the lightweight probe misbehaves, an operator can restore the original healthcheck with a temporary Compose override file:

services:
  worker:
    healthcheck:
      test: ["CMD", "node", "dist/cli.js", "health", "worker"]

The existing interval, timeout, retries, and start period remain in compose.yaml.

@yusixian yusixian added the bug Something isn't working label Sep 25, 2026
@yusixian yusixian self-assigned this Sep 25, 2026
@yusixian

Copy link
Copy Markdown
Contributor Author

CI round 1 diagnosis: Validate failed in Run PostgreSQL 18 integration test on both attempts of run 36158951553. The sole failed suite is test/integration/media-cache-s3-minio.test.ts: Docker Hub returns HTTP 404 / pull access denied for the pinned minio/minio:RELEASE.2025-09-07T16-13-09Z image. The run reports 29 other integration suites and 205 tests passed; lint, typecheck, and unit tests passed. The later Compose smoke step was skipped.

This is an external image-pull failure in a media-cache test outside PR #59's worker-healthcheck changes. Per the harness scope, I am leaving unrelated code and workflows unchanged. PR #59 remains draft while Compose smoke and the #57 low-spec CPU/API acceptance are unverified.

@cos-tool-bot-production

Copy link
Copy Markdown

Owner feedback (via Telegram)

继续完成这个任务:按 issue 的要求完成修复并验证,然后提交 PR。如果被阻塞,请用 Question: 说明需要我决定什么。

@yusixian

Copy link
Copy Markdown
Contributor Author

按反馈继续完成了本地验证;代码 diff 没有新增修改,远端仍是 c86efac。

  • 启动本机 Docker 后,默认 pnpm compose:smoke 通过:新探针的健康与错误实例检查、原 CLI 对照、worker 重启、合成 Telegram 采集和公开 API 均通过。
  • 重新运行 pnpm lint && pnpm typecheck && pnpm test(去掉本机冲突的 NO_COLOR)全部通过;聚焦的 PostgreSQL worker 集成测试 2/2 通过。
  • 在独立测试项目中给 worker 限制 2 CPU / 1 GiB,健康探针各运行 10 次:旧 CLI 的 cgroup CPU 中位数 1.251 秒,新探针 0.059 秒;每 10 秒触发并以 200 ms 采样时,CPU 窗口峰值从 189.6% 降到 30.5%。对照结束后,合成公开 API 与 /readyz 仍通过。该实验的宿主是 10 CPU / 8.2 GB Docker VM,不能冒充 ops: worker healthcheck 每 10s 冷启动完整 CLI —— 低配宿主上的周期性 CPU 尖峰 #57 的真实 2 vCPU / 1 GB 宿主验收。

PR 正文已更新为这些结果。CI 两次仍因无关的 MinIO 镜像拉取 HTTP 404 停在 Compose smoke 之前;我保持 PR 为 draft,并会在 #58 用 Question: 请 owner 明确剩余验收的判定方式。

@cos-tool-bot-production

Copy link
Copy Markdown

Owner feedback (via Telegram)

CI 好像失败了,另外 review 并修复这个 PR

@yusixian

Copy link
Copy Markdown
Contributor Author

已复核 PR #59 的完整 diff,并对照原 health worker 路径与真实数据库的 smoke 检查;未发现新的、属于本 PR 的可执行问题。此前 Claude Code opus[1m] 独立 review 的 4 项建议均已在当前提交 c86efac 处理,本轮无需额外改动,分支已再次推送确认与远端一致。

重新运行 env -u NO_COLOR sh -c 'pnpm lint && pnpm typecheck && pnpm test' 全部通过(server 单测 560/560);本地 Compose smoke 和 2 CPU / 1 GiB 容器限额下的新旧探针对比结果见 PR 的 ## Verification。

CI Validate 第 2 次运行的 lint、typecheck、单测及 worker-runtime 集成测试均通过(后者 2/2)。失败发生在本 PR 未修改的 media-cache-s3-minio.test.ts:Docker Hub 拉取其固定的 minio/minio:RELEASE.2025-09-07T16-13-09Z 返回 HTTP 404;随后 CI Compose smoke 被跳过。按本任务范围,我没有改动无关的 MinIO 测试或工作流。PR 保持 draft,等待 #58 上现有 Question: 对低配宿主机实测及这项 CI 阻塞的验收答复。

@cos-tool-bot-production

Copy link
Copy Markdown

Owner feedback (via Telegram)

没问题了,留后路可以了

@yusixian
yusixian marked this pull request as ready for review September 26, 2026 07:41
@yusixian

Copy link
Copy Markdown
Contributor Author

已按“留后路”的反馈更新 PR 描述:原 node dist/cli.js health worker 仍保留在运行镜像中,Compose smoke 继续执行它;## Fallback 给出用额外 Compose 覆盖文件恢复旧 healthcheck 命令的具体配置。独立复核确认该覆盖会保留现有的间隔、超时和重试设置。源码 diff 未变,分支再次推送确认与远端一致。

重新运行 env -u NO_COLOR sh -c 'pnpm lint && pnpm typecheck && pnpm test' 全部通过(server 560/560)。本轮首次运行有一个未改动的 reconciliation CLI smoke 用例在本机高负载下碰到 5 秒超时;聚焦重跑及第二次完整快速验证均通过。本地默认 Compose smoke 与 2 CPU / 1 GiB 容器限额对比结果仍见 ## Verification。

PR 已按验收答复标为 ready for review。GitHub CI 的 MinIO 镜像 HTTP 404 仍是未解决的无关阻塞,已保留在 PR 描述中;没有修改该测试或工作流,也没有合并、发布或部署。

@yusixian
yusixian marked this pull request as draft September 26, 2026 08:58
@yusixian

Copy link
Copy Markdown
Contributor Author

你说得对:#59 当前仍只有一条 Validate,状态为失败;将 PR 标为 ready 是我判断过早,现已改回 draft,并同步修正 PR 描述。

远端仍是 c86efac,GitHub 没有产生新的 CI run。现有 run 的 lint、typecheck、单测及 worker runtime 集成测试通过;失败点仍是未被本 PR 修改的 media-cache-s3-minio.test.ts 拉取固定的 MinIO 镜像返回 HTTP 404,导致后续 Compose smoke 被跳过。本地快速验证与默认 Compose smoke 已通过,但不能使 GitHub 的红色检查转绿。

按本任务此前的 CI 范围要求,无关失败只在 PR 解释,不将 MinIO 测试或工作流改动混入 worker healthcheck PR。我已在 #58 用 Question: 请求 owner 确定这项独立 CI 阻塞的处理方式;在检查解决前保持 draft。

@yusixian
yusixian marked this pull request as ready for review September 26, 2026 09:24
@yusixian

Copy link
Copy Markdown
Contributor Author

按反馈去掉了默认 CI 流程中的 MinIO 容器测试,没有从源码构建,也没有改 .github/workflows/**:apps/server/package.json 的 test:integration 只排除 media-cache-s3-minio.test.ts。测试文件仍保留;S3 后端的模拟单测和其余 29 个数据库集成文件继续运行。四处部署/媒体缓存文档已同步改正,不再声称 CI 有真实 MinIO 兼容性基线。

新提交为 6387002。本地 pnpm lint && pnpm typecheck && pnpm test 全部通过,server 单测 560/560;pnpm test:integration 为 29 个文件、205/205 测试通过。独立 review 的文档发现已修正,复核无剩余阻塞项。最终 Validate 全部通过,包括默认 Compose smoke。PR 描述的 ## Verification、## Review 和 fallback 已更新,PR 现为 ready for review。

This branch has not been deployed

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

Labels

bug Something isn't working harness:revise

Projects

None yet

Development

Successfully merging this pull request may close these issues.

降低默认 worker 健康检查的冷启动 CPU 开销

1 participant