fix(task): enforce background task limit on active tasks only - #600
Open
blueberrycongee wants to merge 1 commit into
Open
blueberrycongee wants to merge 1 commit into
blueberrycongee wants to merge 1 commit into
Conversation
Co-authored-by: WUU Agent <305930189+wuu-agent[bot]@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
问题
后台任务原先通过
entries.size判断是否达到默认 32 个任务的上限,但已结束任务也保留在注册表中。因此,累计创建 32 个任务后,即使没有任务正在运行,也无法继续启动新任务。修改
启动任务时,只统计
pending和running状态的任务。已完成、失败和取消的任务不再占用并发名额,历史记录及保留的输出仍可查询。取消请求不会立即释放名额:任务保持
running,直到进程退出后才进入终态,避免提前放入新任务。直接根据任务状态计算并发数,不新增计数器,避免计数与状态不同步。
验证
相关测试 11/11 通过,覆盖:
本地验证使用 Node 24,尚未在项目要求的 Node 22 上复验。
取舍
每次启动需要遍历历史任务,复杂度为 O(n)。历史记录及输出继续保留,内存占用也会随历史增长;历史清理或持久化策略不在本次修改范围内。