Skip to content

feat: add unref option for heartbeat timers#588

Merged
merencia merged 6 commits into
mainfrom
feat/unref
Jun 25, 2026
Merged

feat: add unref option for heartbeat timers#588
merencia merged 6 commits into
mainfrom
feat/unref

Conversation

@merencia

Copy link
Copy Markdown
Member

Summary

  • Adds unref?: boolean to TaskOptions and RunnerOptions
  • When set, calls .unref() on the heartbeat and jitter setTimeout handles so the Node.js process can exit naturally when only unref'd timers remain
  • Useful for CLI tools, scripts, and graceful shutdown scenarios

Changes

  • src/scheduler/runner.ts: stores unref flag, calls .unref() on heartBeatTimeout in armHeartBeat() and on jitterTimeout
  • src/tasks/scheduled-task.ts: adds unref?: boolean to TaskOptions
  • src/tasks/inline-scheduled-task.ts: passes unref through to RunnerOptions
  • src/scheduler/runner-unref.test.ts: 4 tests (default ref, unref true, stop/start persistence, jitter unref)

Test plan

  • Default: heartbeat has ref (hasRef() === true)
  • With unref: true: heartbeat is unref'd (hasRef() === false)
  • Setting persists across stop/start cycles
  • Jitter timeout is also unref'd when option is set
  • Full suite passes (612 tests)

merencia added 6 commits June 24, 2026 18:43
When unref: true is passed in RunnerOptions (or TaskOptions), the
heartbeat setTimeout and jitter setTimeout are unref'd so the Node.js
process can exit when only these timers remain. Useful for CLI tools
and short-lived scripts.
Add unref field to RunnerOptions and TaskOptions. When true, the
heartbeat setTimeout and jitter setTimeout handles are unref'd after
each arm so the Node.js process can exit naturally when only cron timers
remain. InlineScheduledTask passes the option through to RunnerOptions.
Allow changing ref/unref at runtime on a running task.
Runner.setUnref() applies immediately to the current heartbeat
and jitter timeouts. Background tasks delegate to child.unref/ref.
@merencia merencia merged commit 35cb140 into main Jun 25, 2026
7 checks passed
@merencia merencia deleted the feat/unref branch June 25, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant