# 41-sidecar-child-process / app Sidecar 进程工作台:同一份 `worker.cjs` 分别通过 `utilityProcess`(消息通道) 与 `child_process`(stdin/stdout NDJSON)启动,执行素数计数任务,演示心跳、 崩溃重启、指数退避与停止清理。 ## 运行 ```bash pnpm --filter @tutorial/41-sidecar-child-process-app start pnpm --filter @tutorial/41-sidecar-child-process-app smoke pnpm --filter @tutorial/41-sidecar-child-process-app shots ``` ## smoke 输出 ```json {"evt":"smoke","ok":true,"utilityPid":36760,"childPid":36764,"mainPid":36752, "utilityResult":17984,"childResult":17984,"equal":true, "utilityTransport":"message","childTransport":"stdio","heartbeat":1, "restarts":1,"runningAfterRestart":true,"lastExitCode":3, "finalRunning":false,"logs":9,"events":29} ``` ## 结构 - `sidecar/worker.cjs`:独立程序,兼容 parentPort 与 stdin/stdout - `main/index.ts`:两种启动方式、请求超时、NDJSON 解析、重启策略、停止清理 - `preload/index.ts`:`sidecar` 白名单方法 + 事件 - `src/App.ts`:进程状态、任务、日志、对比、重启策略、打包说明 ## testid | testid | 用途 | | --- | --- | | `status` / `mode-pill` / `transport` | 进程状态 | | `pid` / `main-pid` / `running` | 进程标识 | | `start-utility` / `start-child` / `stop` / `crash` | 生命周期 | | `job-input` / `job-run` / `job-result` | 任务 | | `compare` / `logs` / `events` | 对比与日志 | | `restarts` / `next-restart` / `policy` | 重启策略 | ## 已知限制 - sidecar 是 Node 脚本,不是独立编译的二进制;真实二进制可用同样协议替换。 - 打包产物的 `extraResource` 加载未在本章实打;配置已给出。 - 基准/任务为 CPU 计数,只用于演示协议与生命周期。