Repository navigation
fix(jobs): 无 Redis 时用数据库租约保证「一个 tick 只跑一个副本」 - #39
Merged
Merged
Conversation
worable233
force-pushed
the
feat/job-lease
branch
from
October 25, 2026 08:56
1ff4716 to
3ee1cf4
Compare
对标 #12.5 暴露的真缺口:`MemoryJobBackend` 每个副本各有一份 `setInterval`, 两个副本同时到点就是同一批「到期暂停 / 回收站清理 / outbox 转发」各跑两遍。 之前只有一行启动 warn,把「集群安全」写成了运维自觉 —— 而对标对象 (ZJMF `Cron.php` 的 `cron_lock` + 5 分钟陈旧期)同样场景下反而不会重复。 ## 做法 - `kernel_job_leases(name, holder, expiresAtMs)` + 一条语句完成抢占: `INSERT ... ON CONFLICT DO UPDATE ... WHERE expiresAtMs < now RETURNING name`。 命中即执行,空结果集即跳过 —— 并发时由行锁保证只有一个副本能命中。 - 租约按时间自然过期,**不需要死节点检测**:持有者被 kill,最多一个间隔后 就有别的副本接手。 - 租约时长 = 任务间隔(下限 1s / 上限 5 分钟)。取「间隔的若干倍」会把有效频率 压到几倍间隔以下 —— 抢到的副本每轮都成功、其余副本整个窗口全部跳过。 - 抢租约出错按「没抢到」处理:宁可这一轮不跑(下轮补),也不因数据库抖一下 变成多副本重复执行。 - 只给进程内后端用。BullMQ 本身按队列抢占,再套一层会让重试互相挡住。 ## 验证 - `tests/unit/job-lease-memory.test.ts` 4 例:抢到才执行 / 没抢到静默跳过 / 租约抛错时跳过且告警 / 租约时长取一个间隔(含 1s 下限、5min 上限)。 - `tests/integration/job-lease.test.ts` 4 例真实 DB:两副本只有一个抢到 / 过期后接手 / 不同任务互不阻塞 / 同一副本租约内不能自我放行。 - 既有 `tests/unit/jobs.test.ts`、`tests/unit/jobs-contract.test.ts` 全绿(21 例合计)。 - `tsc` 干净。 顺带一句:写测试时踩到「四个用例共用同一个任务名」——第二例被第一例的未过期租约 挡住,看起来像实现错了。用例已按任务名隔离,并在注释里写明原因。
worable233
force-pushed
the
feat/job-lease
branch
from
October 25, 2026 09:03
3ee1cf4 to
1acb164
Compare
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.
对标 #12.5 暴露的真缺口:
MemoryJobBackend每个副本各有一份setInterval,两个副本同时到点就是同一批「到期暂停 / 回收站清理 / outbox 转发」各跑两遍。之前只有一行启动 warn,把「集群安全」写成了运维自觉 —— 而对标对象(ZJMFCron.php的cron_lock+ 5 分钟陈旧期)在同样场景下反而不会重复。做法
kernel_job_leases(name, holder, expiresAtMs)+ 一条语句完成抢占:INSERT ... ON CONFLICT DO UPDATE ... WHERE expiresAtMs < now RETURNING name。命中即执行,空结果集即跳过 —— 并发时由行锁保证只有一个副本能命中验证
tests/unit/job-lease-memory.test.ts4 例:抢到才执行 / 没抢到静默跳过 / 租约抛错时跳过且告警 / 租约时长取一个间隔(含 1s 下限、5min 上限)tests/integration/job-lease.test.ts4 例真实 DB:两副本只有一个抢到 / 过期后接手 / 不同任务互不阻塞 / 同一副本租约内不能自我放行jobs.test.ts、jobs-contract.test.ts全绿(合计 21 例);tsc干净边界
租约只抑制「同一轮多副本并发执行」;间隔短于处理器执行时长时仍可能重叠(与握手前的行为相同,未变差)。真要做到严格串行需要按任务加长租约或改用 BullMQ。