fix(tasks): stop scheduled tasks on runtime close#4416
Conversation
|
@Prains is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThe change adds a Nitro runtime server plugin that runs the ChangesRuntime shutdown lifecycle
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
pi0
left a comment
There was a problem hiding this comment.
While this is a valid fix for durable servers, if someone makes a custom entry like CLI only app, scheduled tasks should keep running even without server listener ref.
I think better alternative would be to make sure scheduled tasks are destroyed when calling nitro close hook in runtime.
|
@pi0 Updated in |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/runtime/internal/app.ts`:
- Around line 42-50: Make shutdown resilient in nitroRuntimeHooksPlugin at
src/runtime/internal/app.ts lines 42-50 by catching failures from the memoized
close hook and ensuring the original server.close call always proceeds; in
src/runtime/internal/task.ts lines 69-73, wrap each job.stop() invocation in
try/catch so one failing job does not prevent subsequent jobs from stopping or
reject the close hook.
In `@src/runtime/internal/task.ts`:
- Around line 75-89: Update the Cron construction in the scheduledTasks loop to
pass an options object with unref enabled, while preserving the existing
scheduled callback. Update the related unit test expectation to include this
Cron option.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 4cb6a897-b57a-4a6c-85f1-583e8efacb1c
📒 Files selected for processing (8)
src/presets/bun/runtime/bun.tssrc/presets/deno/runtime/deno-server.tssrc/presets/node/runtime/node-cluster.tssrc/presets/node/runtime/node-server.tssrc/runtime/internal/app.tssrc/runtime/internal/task.tstest/unit/runtime-app.test.tstest/unit/task.test.ts
Resolves #4415.
Problem
Scheduled Croner timers must stay referenced for custom CLI-only entries, but referenced timers also need to be released during graceful server shutdown.
Change
closehook.closehook for Bun, Node, Node cluster, and Deno server presets.Verification
pnpm testServer closed successfully.