A runtime-agnostic cron scheduler for NetScript: one CronScheduler contract that drives
scheduled work through native Deno.cron in production and a deterministic in-memory adapter in
tests.
Scheduled jobs are easy to write and miserable to test: the native scheduler only fires on real
wall-clock boundaries, so test suites either mock time or skip the path entirely. @netscript/cron
splits the contract from the clock. createScheduler() picks the native DenoCronAdapter when
Deno.cron is available and the MemoryCronAdapter otherwise — and the memory adapter can trigger
every registered job on demand, wait for executions, and tick on a compressed interval, so the same
handler code is exercised deterministically in milliseconds.
- One port, many backends — every adapter implements the
CronSchedulercontract (schedule,unschedule,trigger,enable/disable,list,stop), so handlers stay identical across runtimes. - Runtime auto-detection —
createScheduler()selects the native adapter whenDeno.cronexists and the in-memory adapter otherwise; force a backend with theprovideroption (CronProviders.DENO/CronProviders.MEMORY;nodeandtemporalids are reserved and not yet implemented). - Deterministic testing — the
MemoryCronAdapterexposestriggerAll,waitForExecutions,getExecutionCount, andsetTickIntervalfor fast, time-independent test runs. - Timezone-aware jobs —
ScheduleOptionscarriestimezone,runOnInit, and retry settings; validate expressions ahead of time withisValidCronExpressionand inspect them withparseCronExpression, or start from aCronPresetsconstant. - Typed lifecycle events — subscribe with
scheduler.on(...)to a typedSchedulerEventMapcovering job runs and schedule/unschedule changes, plus shared-instance helpersgetSchedulerandstopScheduler.
deno add jsr:@netscript/cron@<version>Pin <version> to match your installed CLI; bare jsr:@netscript/* specifiers do not resolve on
the pre-release line.
import { createScheduler, CronPresets } from '@netscript/cron';
declare function generateDailyReport(): Promise<void>;
// Auto-detects the runtime: native Deno.cron in production, in-memory under tests.
const scheduler = createScheduler();
await scheduler.schedule(
'nightly-report',
CronPresets.WEEKDAYS_9AM,
async () => {
await generateDailyReport();
},
{ timezone: 'America/New_York', runOnInit: true },
);
scheduler.on('jobRun', (event) => {
if (!event.result.success) throw event.result.error;
});
await scheduler.trigger('nightly-report');
await scheduler.stop();| Entry | What it gives you |
|---|---|
. |
createScheduler, getScheduler, stopScheduler, CronPresets, expression helpers |
./ports |
The CronScheduler contract, event map, and option types |
./adapters |
DenoCronAdapter and MemoryCronAdapter |
./testing |
Test helpers for cron-driven code |
The always-current symbol list is
deno doc jsr:@netscript/cron@<version> (pin <version> on
the pre-release line, as above).
- Reference — scheduler, adapters, and exports: rickylabs.github.io/netscript/reference/cron/
- Background Processing — where cron fits the background stack: rickylabs.github.io/netscript/background-processing/
- How-to: queue, KV, and cron together: rickylabs.github.io/netscript/how-to/queue-kv-cron/
- API docs on JSR: jsr.io/@netscript/cron/doc
Designed for Deno. The native adapter requires the Deno.cron API, which sits behind Deno's cron
unstable feature (--unstable-cron or "unstable": ["cron"] in deno.json); without it,
auto-detection falls back to the in-memory adapter. The in-memory adapter needs no permissions or
flags.
Apache-2.0 — see LICENSE. Published to JSR with cryptographically verified provenance.