Skip to main content
NetScriptdocs Search docs ⌘K v0.0.9
Browse documentation
View as Markdown

@netscript/cron

Schedule recurring jobs from cron expressions with one CronScheduler interface. Use it for time-based work: the factory picks native Deno.cron at runtime and an in-memory scheduler in tests, with timezones, retries and job-lifecycle events.

Sub-path exports

Export Entrypoint Purpose
@netscript/cron ./mod.ts Scheduler factory, expression utilities, constants and the full port contract.
@netscript/cron/ports ./ports/mod.ts The port contract only: every interface and type alias, without the factory functions.
@netscript/cron/adapters ./adapters/mod.ts The concrete adapters, plus re-exported contract types and utilities.
@netscript/cron/testing ./testing/mod.ts The MemoryCronAdapter for tests.

Scheduler factory

Symbol Kind Signature Description
createScheduler function function createScheduler(options?: CreateSchedulerOptions): CronScheduler Create a cron scheduler, auto-detecting the runtime (native Deno.cron, or in-memory for tests).
getScheduler function function getScheduler(options?: CreateSchedulerOptions): CronScheduler Get the default shared scheduler instance, creating it on first call (options only used on first call).
stopScheduler function async function stopScheduler(): Promise<void> Stop and reset the default scheduler so the next getScheduler() creates a fresh instance.

Cron expression utilities

Symbol Kind Signature Description
isValidCronExpression function function isValidCronExpression(expression: string): boolean Validate a cron expression, returning true if valid.
parseCronExpression function function parseCronExpression(expression: string): ParsedCronExpression | null Parse a cron expression string into its component parts, or null if invalid.

Constants

Symbol Kind Description
CronPresets const Common cron presets (EVERY_MINUTE, EVERY_5_MINUTES, EVERY_HOUR, EVERY_DAY, WEEKDAYS_9AM, WEEKENDS_NOON, and more).
CronProviders const Canonical provider identifiers supported by the built-in factory.

Scheduler contract

The core scheduling interface implemented by every adapter.

Symbol Kind Description
CronScheduler interface Runtime-agnostic scheduling abstraction: schedule, unschedule, list, get, has, enable, disable, trigger, isValid, on/off, and stop, plus provider and isRunning.
CreateSchedulerOptions interface Options for creating a scheduler (provider, tickInterval).
ScheduleOptions interface Options for scheduling a job (timezone, run-on-init, retries).
ScheduledJob interface Scheduled-job metadata.
JobContext interface Job execution context passed to contextual handlers.
JobExecutionResult interface Result of a single job execution.
JobRunEvent interface Event emitted when a job runs.
JobLifecycleEvent interface Event emitted when a job is scheduled or unscheduled.
SchedulerEventMap interface Typed scheduler event-payload map.
ParsedCronExpression interface A cron expression parsed into its component parts.

Types

Symbol Kind Description
CronExpression type alias Cron expression string.
JobHandler type alias Job handler function signature.
ContextualJobHandler type alias Job handler that receives a JobContext.
JobEventListener type alias Event listener for scheduler events.
SchedulerEvent type alias Scheduler event-name union.
CronProvider type alias Cron provider identifier (built-in providers runtime-validated by createScheduler()).
KnownCronProvider type alias Union of the known built-in provider identifiers.
CronProviderRegistry type alias Canonical provider-identifier registry shape.
BackoffStrategy type alias Backoff strategy for retry logic.

Adapters

Concrete implementations of CronScheduler, exported from @netscript/cron/adapters.

Symbol Kind Description
DenoCronAdapter class Uses the native Deno.cron runtime; works on Deno Deploy and local Deno.
MemoryCronAdapter class setInterval-based in-memory scheduler with test helpers (triggerAll, waitForExecutions, getExecutionCount, setTickInterval). Use it in tests and development.

Examples

Test a schedule with fake time

Pair MemoryCronAdapter with the FakeTime clock from @std/testing/time to advance time in a test without waiting on the wall clock.

TypeScript
import { assertEquals } from "@std/assert";
import { FakeTime } from "jsr:@std/testing/time";
import { MemoryCronAdapter } from "@netscript/cron/adapters";

Deno.test("MemoryCronAdapter deterministic execution with FakeTime", async () => {
  const time = new FakeTime();
  try {
    const scheduler = new MemoryCronAdapter();
    let executionCount = 0;

    // Schedule a job to run every 5 minutes
    await scheduler.schedule("test-job", "*/5 * * * *", () => {
      executionCount++;
    });

    assertEquals(executionCount, 0);

    // Advance clock by 5 minutes (300,000 ms)
    await time.tickAsync(300000);
    assertEquals(executionCount, 1);

    // Advance clock by another 10 minutes (600,000 ms)
    await time.tickAsync(600000);
    assertEquals(executionCount, 3);

    // Clean up
    await scheduler.stop();
  } finally {
    time.restore();
  }
});
Search all documentation

Type a term to search the documentation.