-
-
Notifications
You must be signed in to change notification settings - Fork 181
PR: 修复 Edge Runtime 下 process.once 构建告警(AsyncTaskManager 导入链)
#589
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
ding113
merged 14 commits into
ding113:dev
from
YangQing-Lin:fix/edge-runtime-process-once
Jan 11, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9b54b10
fix: skip async task manager init on edge
YangQing-Lin 56a0125
fix: avoid static async task manager import
YangQing-Lin 1152cda
test: cover edge runtime task scheduling
YangQing-Lin cf63b04
chore: document edge runtime process.once fix
YangQing-Lin f968ac4
chore: record edge runtime warning baseline
YangQing-Lin b26899b
fix: drop NEXT_PHASE and lazy-init async task manager
YangQing-Lin 1aaf8e4
test: isolate NEXT_RUNTIME in cloud price sync tests
YangQing-Lin 214556d
docs: stabilize edge process.once repro baseline
YangQing-Lin 7333669
docs: make rollback instructions hashless
YangQing-Lin d391f8e
docs: add grep checklist for edge warning audit
YangQing-Lin 57ad3d8
chore: run regression gate and align docs
YangQing-Lin 4bc4d15
test: cover edge runtime guard on register
YangQing-Lin bee7e19
Update src/lib/async-task-manager.ts
YangQing-Lin 65ae2aa
chore: format code (fix-edge-runtime-process-once-bee7e19)
github-actions[bot] 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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 |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Fix: Edge Runtime `process.once` build warning | ||
|
|
||
| ## 背景 | ||
|
|
||
| `next build` 过程中出现 Edge Runtime 不支持 Node API 的告警:`process.once`。 | ||
|
|
||
| ### 复现基线(修复前) | ||
|
|
||
| 在修复前的版本(例如 tag `v0.4.1`)上运行 `bun run build` 可看到 Edge Runtime 不支持 Node API 的告警,其 import trace 包含: | ||
|
|
||
| ```text | ||
| A Node.js API is used (process.once) which is not supported in the Edge Runtime. | ||
| Import traces: | ||
| ./src/lib/async-task-manager.ts | ||
| ./src/lib/price-sync/cloud-price-updater.ts | ||
| ./src/instrumentation.ts | ||
| ``` | ||
|
|
||
| 相关导入链路(import trace)包含: | ||
|
|
||
| - `src/lib/async-task-manager.ts` | ||
| - `src/lib/price-sync/cloud-price-updater.ts` | ||
| - `src/instrumentation.ts` | ||
|
|
||
| ## 变更 | ||
|
|
||
| - `AsyncTaskManager`: | ||
| - 在 `process.env.NEXT_RUNTIME === "edge"` 时跳过初始化,避免触发 `process.once` 等 Node-only API。 | ||
| - `cloud-price-updater`: | ||
| - 移除对 `AsyncTaskManager` 的顶层静态 import。 | ||
| - 在 `requestCloudPriceTableSync()` 内部按需动态 import `AsyncTaskManager`,并在 Edge runtime 下直接 no-op。 | ||
|
|
||
| ## 验证 | ||
|
|
||
| - `bun run lint` | ||
| - `bun run typecheck` | ||
| - Targeted coverage(仅统计本次相关文件): | ||
| - `bunx vitest run tests/unit/lib/async-task-manager-edge-runtime.test.ts tests/unit/price-sync/cloud-price-updater.test.ts --coverage --coverage.provider v8 --coverage.reporter text --coverage.include src/lib/async-task-manager.ts --coverage.include src/lib/price-sync/cloud-price-updater.ts` | ||
| - 结果:All files >= 90%(Statements / Branches / Functions / Lines) | ||
| - `bun run build` | ||
| - 结果:不再出现 Edge Runtime `process.once` 相关告警 | ||
|
|
||
| ## 回滚 | ||
|
|
||
| 如需回滚,优先按提交粒度回退(以 commit message 为准): | ||
|
|
||
| - `fix: skip async task manager init on edge` | ||
| - `fix: avoid static async task manager import` | ||
| - `test: cover edge runtime task scheduling` | ||
|
|
||
| 定位对应提交(示例): | ||
|
|
||
| ```bash | ||
| git log --oneline --grep "fix: skip async task manager init on edge" | ||
| git log --oneline --grep "fix: avoid static async task manager import" | ||
| git log --oneline --grep "test: cover edge runtime task scheduling" | ||
| ``` | ||
|
|
||
| ## 备选方案(若回归) | ||
|
|
||
| 如果未来 Next/Turbopack 的静态分析行为变化导致告警回归,可将 Node-only 的 signal hooks 拆分到 `*.node.ts`(例如 `async-task-manager.node.ts`),并仅在 `NEXT_RUNTIME !== "edge"` 的分支里动态引入。 | ||
|
|
||
| ## 快速定位(避免文档漂移) | ||
|
|
||
| ```bash | ||
| rg -n "process\\.once" src/lib/async-task-manager.ts | ||
| rg -n "NEXT_RUNTIME|NEXT_PHASE" src/lib tests | ||
| rg -n "requestCloudPriceTableSync" src/lib/price-sync/cloud-price-updater.ts tests/unit/price-sync/cloud-price-updater.test.ts | ||
| ``` | ||
|
|
||
| ## 备注 | ||
|
|
||
| `.codex/plan/` 与 `.codex/issues/` 属于本地任务落盘目录,不应提交到 Git。 | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| import { AsyncTaskManager } from "@/lib/async-task-manager"; | ||
| import { logger } from "@/lib/logger"; | ||
| import type { PriceUpdateResult } from "@/types/model-price"; | ||
| import { | ||
|
|
@@ -58,47 +57,73 @@ export function requestCloudPriceTableSync(options: { | |
| reason: "missing-model" | "scheduled" | "manual"; | ||
| throttleMs?: number; | ||
| }): void { | ||
| const throttleMs = options.throttleMs ?? DEFAULT_THROTTLE_MS; | ||
| const taskId = "cloud-price-table-sync"; | ||
|
|
||
| // 去重:已有任务在跑则不重复触发 | ||
| const active = AsyncTaskManager.getActiveTasks(); | ||
| if (active.some((t) => t.taskId === taskId)) { | ||
| if (process.env.NEXT_RUNTIME === "edge") { | ||
| return; | ||
| } | ||
|
|
||
| const throttleMs = options.throttleMs ?? DEFAULT_THROTTLE_MS; | ||
| const taskId = "cloud-price-table-sync"; | ||
|
|
||
| // 节流:避免短时间内频繁拉取云端价格表 | ||
| const g = globalThis as unknown as { __CCH_CLOUD_PRICE_SYNC_LAST_AT__?: number }; | ||
| const g = globalThis as unknown as { | ||
| __CCH_CLOUD_PRICE_SYNC_LAST_AT__?: number; | ||
| __CCH_CLOUD_PRICE_SYNC_SCHEDULING__?: boolean; | ||
| }; | ||
|
Comment on lines
+68
to
+71
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 对 例如,在 declare global {
// eslint-disable-next-line no-var
var __CCH_CLOUD_PRICE_SYNC_LAST_AT__: number | undefined;
// eslint-disable-next-line no-var
var __CCH_CLOUD_PRICE_SYNC_SCHEDULING__: boolean | undefined;
}之后在此文件中就可以直接使用 |
||
| const lastAt = g.__CCH_CLOUD_PRICE_SYNC_LAST_AT__ ?? 0; | ||
| const now = Date.now(); | ||
| if (now - lastAt < throttleMs) { | ||
| return; | ||
| } | ||
|
|
||
| AsyncTaskManager.register( | ||
| taskId, | ||
| (async () => { | ||
| try { | ||
| const result = await syncCloudPriceTableToDatabase(); | ||
| if (!result.ok) { | ||
| logger.warn("[PriceSync] Cloud price sync task failed", { | ||
| reason: options.reason, | ||
| error: result.error, | ||
| }); | ||
| return; | ||
| } | ||
| // 避免并发请求在 AsyncTaskManager 加载前重复触发(例如多请求同时命中 missing-model) | ||
| if (g.__CCH_CLOUD_PRICE_SYNC_SCHEDULING__) { | ||
| return; | ||
| } | ||
| g.__CCH_CLOUD_PRICE_SYNC_SCHEDULING__ = true; | ||
|
|
||
| void (async () => { | ||
| try { | ||
| const { AsyncTaskManager } = await import("@/lib/async-task-manager"); | ||
|
|
||
| logger.info("[PriceSync] Cloud price sync task completed", { | ||
| reason: options.reason, | ||
| added: result.data.added.length, | ||
| updated: result.data.updated.length, | ||
| skippedConflicts: result.data.skippedConflicts?.length ?? 0, | ||
| total: result.data.total, | ||
| }); | ||
| } finally { | ||
| g.__CCH_CLOUD_PRICE_SYNC_LAST_AT__ = Date.now(); | ||
| // 去重:已有任务在跑则不重复触发 | ||
| const active = AsyncTaskManager.getActiveTasks(); | ||
| if (active.some((t) => t.taskId === taskId)) { | ||
| return; | ||
| } | ||
| })(), | ||
| "cloud_price_table_sync" | ||
| ); | ||
|
|
||
| AsyncTaskManager.register( | ||
| taskId, | ||
| (async () => { | ||
| try { | ||
| const result = await syncCloudPriceTableToDatabase(); | ||
| if (!result.ok) { | ||
| logger.warn("[PriceSync] Cloud price sync task failed", { | ||
| reason: options.reason, | ||
| error: result.error, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| logger.info("[PriceSync] Cloud price sync task completed", { | ||
| reason: options.reason, | ||
| added: result.data.added.length, | ||
| updated: result.data.updated.length, | ||
| skippedConflicts: result.data.skippedConflicts?.length ?? 0, | ||
| total: result.data.total, | ||
| }); | ||
| } finally { | ||
| g.__CCH_CLOUD_PRICE_SYNC_LAST_AT__ = Date.now(); | ||
| } | ||
| })(), | ||
| "cloud_price_table_sync" | ||
| ); | ||
| } catch (error) { | ||
| logger.warn("[PriceSync] Cloud price sync scheduling failed", { | ||
| reason: options.reason, | ||
| error: error instanceof Error ? error.message : String(error), | ||
| }); | ||
| } finally { | ||
| g.__CCH_CLOUD_PRICE_SYNC_SCHEDULING__ = false; | ||
| } | ||
| })(); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
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.
这条备注很有用。为了从根本上防止这些目录被意外提交,强烈建议将
.codex/添加到项目根目录的.gitignore文件中。这样可以利用版本控制系统自动忽略这些文件,比依赖开发者阅读文档更可靠。