Repository navigation
Commit 183a89e
fix(service-job): kernel shutdown waits for a scheduled run in flight, or settles its row as interrupted (#34331)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} -->
_Requested by **Steedos** · [project
thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbTGhxJSYAmQXU7efVdNqrpw)_
Fixes #34046
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 改了什么:内核关停时,定时任务适配器会先等正在跑的那一次定时执行结束(默认最多 10 秒);超时仍未结束的,在驱动释放前把它的
`sys_job_run` 行记为 `failed`,错误文案写明「被关停中断」。
- 为什么改:云端驱逐内核是常态,以前关停不等在跑的任务,任务随后撞上已释放的驱动,运行行永远停在 `running`,还打两条驱动失败告警。
- 风险与回滚:关停最多多等 10 秒(在内核 60 秒关停预算内);只动 `service-job` 一个包,不新增状态值或元数据键;回滚即
revert 本 PR。
- 先看:`packages/services/service-job/src/db-job-adapter.ts` 的 `destroy()`
与 `wrap()` 中 `onAttemptStart` 两处关停判断;其次 `scheduled-fire.ts` 的
`drainScheduledFires`。
- 两分钟验证:`pnpm --filter @objectstack/service-job exec vitest run
src/shutdown-drain-34046.test.ts`,预期 8 条全绿。
- 需要你做的:无,席位落地。
## What changes
The kernel destroys plugins in reverse order (`performShutdown()` in
`packages/core/src/kernel.ts`), so the job plugin is destroyed before
the driver it writes through. `IntervalJobAdapter.destroy()` and
`CronJobAdapter.destroy()` cleared their timers and returned at once. A
scheduled run still executing then hit the released driver: its row
stayed `running` and `finishRun` / `bumpJob` logged driver failures.
- `scheduled-fire.ts`: `ScheduledFire` now holds the fire's `settled`
promise next to `startedAt`. New helpers: `openScheduledFire()`,
`drainScheduledFires(inFlight, boundMs)` (waits for every held fire, at
most `boundMs`, and returns the names still running),
`drainExpiredMessage()`, and `DEFAULT_SHUTDOWN_DRAIN_MS = 10_000`.
- `IntervalJobAdapter` and `CronJobAdapter`: new constructor option
`shutdownDrainMs` (default 10000). `destroy()` stops the timers, clears
the registrations, then waits for the fires in `scheduledInFlight`, up
to the bound. If the bound passes, one `warn` names each job left
running. `IntervalJobAdapter.runScheduled()` now skips a tick whose
registration is gone (`CronJobAdapter` already did), so a tick racing
`destroy()` starts nothing.
- `DbJobAdapter`: new `DbJobAdapterOptions.shutdownDrainMs`, forwarded
to its own `inner` timer adapter (the no-cron fallback). A cron adapter
you pass in uses its own option. Each scheduled invocation of a wrapped
handler is tracked from its first attempt until it settles. `destroy()`
sets `shuttingDown`, destroys `inner`, then the cron adapter (both
drain), then settles every scheduled run still open: `status: 'failed'`,
`error` = "interrupted by shutdown: …". `sys_job` mirrors it like any
failed run. This happens while the driver is still up. The existing
one-terminal-write latch makes the abandoned handler's own settle a
no-op when it finishes later. Once `shuttingDown` is set, a scheduled
run starts no retry attempt. If shutdown starts while an attempt's row
insert is in flight, that row is settled as interrupted and the attempt
never starts.
- Status vocabulary: no new value. `failed` is reused because the run
did not finish, and that is what `failed` already means. Spec
`JobExecutionStatus`, `sys_job_run.status` and `sys_job.last_status` are
untouched.
- Manual `trigger()` / `replay()` are unchanged. They are not waited
for, not settled and not gated, because their callers await them.
- Bound: 10 s by default, inside the kernel's default 60 s
`shutdownTimeout`, which every plugin's teardown shares. `packages/core`
is not touched.
## Tests
New pin file
`packages/services/service-job/src/shutdown-drain-34046.test.ts` (8
tests). The harness models a kernel teardown: `await db.destroy()`, then
the engine is "released". After that, every engine write (`insert` /
`update`) is recorded and throws `No driver available for object …`.
`find` stays the plain double, because every path that settles a run
ends in a write. Legs: `DbJobAdapter` with no cron adapter
(`IntervalJobAdapter` owns the timer, interval schedule), and
`DbJobAdapter` with a real `CronJobAdapter` on a croner expression `* *
* * * *`.
1. A run that ends inside the bound: `destroy()` stays pending until the
run ends. The row ends `success` and no new fire starts. Zero engine
calls after release, and no driver-failure warn.
2. A run past the bound: `destroy()` resolves at exactly the bound. The
row is `failed` with the "interrupted by shutdown" error, and `sys_job`
shows `last_status: failed, failure_count: 1`. When the handler finishes
later, there are zero engine calls after release and no driver-failure
warn.
3. A retry due after shutdown starts: the handler is not called again,
and there are zero engine calls after release.
4. Standalone: `IntervalJobAdapter.destroy()` waits for its in-flight
fire. `CronJobAdapter.destroy()` gives up at its bound and names the
job.
<details><summary>Before reading (main 021abd4)</summary>
The adapters' main sources were copied next to a variant of the pin file
that only measures. That harness also guarded `find`, which is why `find
sys_job` appears. On the run-past-the-bound case, both legs read the
same:
```
interval leg: {"rows":["running"],"afterRelease":["update sys_job_run","find sys_job"],
"warns":[…,"DbJobAdapter: failed to update sys_job_run","DbJobAdapter: bumpJob failed"]}
cron leg: {"rows":["running"],"afterRelease":["update sys_job_run","find sys_job"],
"warns":["DbJobAdapter: failed to update sys_job_run","DbJobAdapter: bumpJob failed"]}
```
This is the card's trace: the row is left `running`, and both WARNs fire
after the driver is gone. The pin file run against main: `Tests 6 failed
(6)` (the six tests that existed then). Each failure was `destroy()`
resolving before the in-flight run settled (`expected true to be
false`).
</details>
<details><summary>Ablations (each negative pin; mutate, run the pin
file, restore; `scripts/ablation-replace.mjs`)</summary>
Run at `2ad7c43191`. The only later change is in the test double: `find`
is no longer guarded. Subjects are imported from `src` by relative path,
so no build was involved. Each restore was proven: blob == HEAD and `git
diff HEAD` empty.
- A1: `IntervalJobAdapter.destroy()` drain replaced by an empty list
(old non-awaiting destroy). Result: `Tests 3 failed | 5 passed (8)`.
Interval leg (1), (2) and the standalone interval test fail with
`expected true to be false` (destroy resolved early).
- A2: the same in `CronJobAdapter.destroy()`. Result: `Tests 3 failed |
5 passed (8)`. Cron leg (1), (2) and the standalone cron test fail.
- A3: `await this.settleInterruptedRuns()` removed from
`DbJobAdapter.destroy()`. Result: `Tests 2 failed | 6 passed (8)`. Both
legs of (2) fail with `expected 'running' to be 'failed'`.
- A4: the `shuttingDown` check before an attempt removed. Result: `Tests
2 failed | 6 passed (8)`. Both legs of (3) fail with `afterRelease` =
`["insert sys_job_run", …]`.
</details>
## Acceptance notes
- After the bound passes, an abandoned `CronJobAdapter` fire still
releases its cluster lock in its own `finally`, after the kernel is
gone. The cluster plugin is destroyed after the job plugin, so this can
log the existing "cluster lock could not release" warn once. The lease
also expires on its TTL. Not changed here: the card's two WARNs are
about `sys_job_run` / `sys_job`.
- Manual `trigger()` / `replay()` runs in flight at shutdown are out of
scope by order ("manual trigger semantics unchanged"). They are neither
waited for nor settled.
- `JobServicePlugin` is untouched. Its `kernel:ready` placeholder
hand-over calls `IntervalJobAdapter.destroy()`, which now also waits for
a placeholder fire in flight (bounded), instead of leaving it running
beside the upgraded adapter.
## Verification
Local runs: head `9993f237ea` (origin/main merged at `2ad7c43191`). The
dependents and the derived gate sweep ran at `2ad7c43191`. The commit
after it changes only the pin file's double, and the package suite plus
every derived gate that reads test files were re-run at `9993f237ea`.
Dependency closures were rebuilt first: `--filter
'@objectstack/runtime^...' --filter
'@objectstack/service-automation^...' --filter '@objectstack/cli^...'
build`, exit 0.
- `@objectstack/service-job` at `9993f237ea`: `vitest run`: `Test Files
13 passed (13) · Tests 163 passed (163)`. `typecheck`: exit 0. `eslint
--no-inline-config` on the five touched files: exit 0.
- Direct dependents, every test file that reaches service-job:
- `@objectstack/runtime`, the 7 job files: `Tests 142 passed (142)`.
- `@objectstack/service-automation`, `wait-node*.test.ts`: `Tests 40
passed (40)`.
- `@objectstack/cli`, `src/utils/schema-migration-plugins.test.ts`
(integration project): `Tests 53 passed (53)`.
- `@objectstack/service-cluster-redis`,
`redis-outage.real-redis.test.ts`: `Tests 2 passed (2)`.
- Derived gates: `node scripts/pm/dispatch-gates.mjs --commands` gives
71 families. All 71 were run at `2ad7c43191`. `--ran` reconciliation: 71
derived, 70 run, 1 NOT-MEASURED, 0 UNRUN.
- 68 exit 0.
- `check:dual-build-cjs-loads` exit 3 (PREREQUISITE NOT MET: the whole
workspace is not built here). NOT MEASURED; CI runs it.
- `check:objectql-double-limit` exit 1 at `2ad7c43191`. The probe threw
on the double's `find` guard. Fixed in `9993f237ea`, and the re-run
there exits 0.
- **`check:engine-double-contract`**: the new pin file's double routes
`update` through `assertEngineUpdateDispatch`, as required. Its ledger
row (`scripts/engine-double-contract.pinned.json`: file = this test,
verb `update`, pinned 1) was added in 312b779 by `node
scripts/check-engine-double-contract.mjs --write` (area grant, 0 lost);
at head 7e9abb6 the gate is judged green by CI (`Lint · CI, release &
engine gates` success). It was red at `9993f237ea`, before the row.
- Re-run at `9993f237ea`, exit 0: `objectql-double-limit`,
`cross-package-test-inputs`, `test-source-alias`, `nul-bytes`,
`where-matcher`, `rest-log-spy-declared`, `registry-log-declared`,
`swallow-census-controls`, `tier-file-adoption`, `issue-citations`,
`doc-authoring`, `refd-timer-probe`, `logger-receiver-detach`,
`keyed-text-bounds`, `query-options-erasure`, `optional-error-sink`.
- `git merge-tree` against origin/main (10 commits past the merge):
clean. None of those commits touches `packages/services/service-job/**`
or the ledger.
Session: https://claude.ai/code/session_01QsR2jTEivi2wogPpeo3xd5
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- signed: repo:objectstack#1 · os-project-manager ·
session_01FEkQ5kgF3mo9f7LXuv5hac -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 6cc3f97 commit 183a89e
7 files changed
Lines changed: 573 additions & 38 deletions
File tree
- .changeset
- packages/services/service-job/src
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
14 | 21 | | |
15 | 22 | | |
16 | 23 | | |
| |||
77 | 84 | | |
78 | 85 | | |
79 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
80 | 93 | | |
81 | 94 | | |
82 | 95 | | |
| |||
112 | 125 | | |
113 | 126 | | |
114 | 127 | | |
| 128 | + | |
115 | 129 | | |
116 | 130 | | |
117 | 131 | | |
| |||
132 | 146 | | |
133 | 147 | | |
134 | 148 | | |
| 149 | + | |
135 | 150 | | |
136 | 151 | | |
137 | 152 | | |
| |||
236 | 251 | | |
237 | 252 | | |
238 | 253 | | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
239 | 263 | | |
240 | 264 | | |
241 | 265 | | |
242 | 266 | | |
243 | 267 | | |
244 | 268 | | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
245 | 273 | | |
246 | 274 | | |
247 | 275 | | |
| |||
281 | 309 | | |
282 | 310 | | |
283 | 311 | | |
284 | | - | |
| 312 | + | |
285 | 313 | | |
286 | 314 | | |
287 | 315 | | |
288 | 316 | | |
289 | 317 | | |
| 318 | + | |
290 | 319 | | |
291 | 320 | | |
292 | 321 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
86 | 93 | | |
87 | 94 | | |
88 | 95 | | |
89 | 96 | | |
90 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
91 | 132 | | |
92 | 133 | | |
93 | 134 | | |
| |||
249 | 290 | | |
250 | 291 | | |
251 | 292 | | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
252 | 297 | | |
253 | 298 | | |
254 | 299 | | |
| |||
261 | 306 | | |
262 | 307 | | |
263 | 308 | | |
264 | | - | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
265 | 314 | | |
266 | 315 | | |
267 | 316 | | |
| |||
541 | 590 | | |
542 | 591 | | |
543 | 592 | | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
544 | 606 | | |
545 | 607 | | |
| 608 | + | |
546 | 609 | | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
547 | 625 | | |
548 | 626 | | |
549 | 627 | | |
| |||
640 | 718 | | |
641 | 719 | | |
642 | 720 | | |
| 721 | + | |
643 | 722 | | |
644 | 723 | | |
645 | | - | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
646 | 737 | | |
647 | 738 | | |
648 | | - | |
649 | | - | |
650 | | - | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
651 | 744 | | |
652 | 745 | | |
653 | | - | |
654 | | - | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
655 | 750 | | |
656 | 751 | | |
657 | | - | |
658 | | - | |
659 | | - | |
660 | | - | |
661 | | - | |
662 | | - | |
663 | | - | |
664 | | - | |
665 | | - | |
666 | | - | |
667 | | - | |
668 | | - | |
669 | | - | |
670 | | - | |
671 | | - | |
672 | | - | |
673 | | - | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
674 | 768 | | |
675 | | - | |
676 | | - | |
677 | | - | |
678 | | - | |
679 | | - | |
680 | | - | |
681 | | - | |
682 | | - | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
683 | 793 | | |
684 | 794 | | |
685 | 795 | | |
| |||
0 commit comments