npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@molecule/api-scheduler

v1.0.2

Published

Task scheduler interface for molecule.dev

Readme

@molecule/api-scheduler

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Task scheduler interface for molecule.dev.

Defines SchedulerProvider and ScheduledTask interfaces for periodic background task execution.

Quick Start

import { schedule, setProvider, start } from '@molecule/api-scheduler'
import { provider } from '@molecule/api-scheduler-default'

setProvider(provider)

schedule({
  name: 'cleanup',
  intervalMs: 60000,
  async handler() {
    await cleanupExpiredRecords()
  },
})

// REQUIRED: tasks do not execute until the scheduler is started.
// (Tasks scheduled after start() begin automatically.)
start()

Type

core

Installation

npm install @molecule/api-scheduler @molecule/api-bond @molecule/api-i18n

API

Interfaces

ScheduledTask

A scheduled task definition.

interface ScheduledTask {
  /** Unique identifier for this task. */
  name: string
  /** Interval in milliseconds between executions. */
  intervalMs: number
  /** Async function to execute on each tick. */
  handler(): Promise<void>
  /** Whether this task is enabled. Defaults to true. */
  enabled?: boolean
}

SchedulerProvider

Scheduler provider interface. All scheduler providers must implement this.

interface SchedulerProvider {
  /**
   * Registers a scheduled task.
   * If a task with the same name already exists, it is replaced.
   *
   * The task does NOT execute until `start()` has been called; a task
   * scheduled while the scheduler is already running begins automatically.
   *
   * @param task - The task to schedule.
   */
  schedule(task: ScheduledTask): void

  /**
   * Stops and removes a scheduled task by name.
   *
   * @param name - The task name to unschedule.
   * @returns true if found and removed, false otherwise.
   */
  unschedule(name: string): boolean

  /**
   * Returns the runtime status of a specific task.
   *
   * @param name - The task name.
   * @returns The task status, or null if not found.
   */
  getStatus(name: string): TaskStatus | null

  /**
   * Returns the runtime status of all scheduled tasks.
   */
  getAllStatuses(): TaskStatus[]

  /**
   * Starts the scheduler. Tasks begin executing according to their intervals.
   */
  start(): void

  /**
   * Stops the scheduler. All tasks stop executing. Can be restarted with start().
   */
  stop(): void
}

TaskStatus

Runtime status of a scheduled task.

interface TaskStatus {
  /** Task name. */
  name: string
  /** ISO 8601 timestamp of the last execution, or null if not yet run. */
  lastRunAt: string | null
  /** ISO 8601 timestamp of the next scheduled execution. */
  nextRunAt: string | null
  /** Whether the task is currently executing. */
  isRunning: boolean
  /** Error message from the last failed execution, if any. */
  lastError: string | null
  /** Duration in milliseconds of the last execution, or null if not yet run. */
  durationMs: number | null
  /** Total number of completed executions (both success and failure). */
  totalRuns: number
  /** Total number of failed executions. */
  totalFailures: number
  /** ISO 8601 timestamp of the last successful execution, or null. */
  lastSuccessAt: string | null
  /** Whether this task is enabled. */
  enabled: boolean
}

Functions

getAllStatuses()

Returns the runtime status of all scheduled tasks.

function getAllStatuses(): TaskStatus[]

Returns: An array of TaskStatus for every registered task.

getOptionalProvider()

Retrieves the bonded scheduler provider, returning null if none is bonded.

function getOptionalProvider(): SchedulerProvider | null

Returns: The bonded scheduler provider, or null.

getProvider()

Retrieves the bonded scheduler provider, throwing if none is configured.

function getProvider(): SchedulerProvider

Returns: The bonded scheduler provider.

getStatus(name)

Returns the runtime status of a specific task through the bonded provider.

function getStatus(name: string): TaskStatus | null
  • name — The task name.

Returns: The task status, or null if no task with that name is registered.

hasProvider()

Checks whether a scheduler provider is currently bonded.

function hasProvider(): boolean

Returns: true if a scheduler provider is bonded.

schedule(task)

Registers and starts a scheduled task through the bonded provider.

function schedule(task: ScheduledTask): void
  • task — The task to schedule.

setProvider(provider)

Registers a scheduler provider as the active singleton.

function setProvider(provider: SchedulerProvider): void
  • provider — The scheduler provider implementation to bond.

start()

Starts the bonded scheduler. Registered tasks do not execute until this is called; tasks scheduled while the scheduler is running begin automatically.

function start(): void

stop()

Stops the bonded scheduler. All tasks stop executing; call start() to resume.

function stop(): void

unschedule(name)

Stops and removes a scheduled task by name.

function unschedule(name: string): boolean
  • name — The task name.

Returns: true if found and removed, false otherwise.

Available Providers

| Provider | Package | | ---------------------------- | ------------------------------------ | | Cloudflare Workers Scheduler | @molecule/api-scheduler-cloudflare | | Default (in-process) | @molecule/api-scheduler-default |

Injection Notes

Requirements

Peer dependencies:

  • @molecule/api-bond ^1.0.1
  • @molecule/api-i18n ^1.0.1

Runtime Dependencies

  • @molecule/api-bond

  • @molecule/api-i18n

  • Nothing runs until start() — call it once at server startup after wiring bonds. Tasks scheduled after start() begin automatically; stop() halts everything.

  • Intervals only — there is NO cron syntax. {@link ScheduledTask} takes intervalMs (milliseconds between runs); a cron expression string is not understood. For calendar-time schedules compute the interval or use a cron-capable package.

  • The scheduler is in-process and unpersisted. Every server instance runs its own copy of every task — N instances execute a task N times unless the handler itself dedupes (e.g. an atomic claim in the database). Schedules and run history are lost on restart; missed runs are not caught up.

  • A tick that arrives while the previous execution is still running is SKIPPED — a handler slower than its intervalMs lowers its own frequency instead of overlapping.

  • Handler errors are caught and recorded on {@link TaskStatus} (lastError, totalFailures) — they don't crash the process, and they're invisible unless you check getStatus()/getAllStatuses() or log inside the handler.

  • schedule() with an existing name REPLACES that task — use stable, unique names.

Translations

Translation strings are provided by @molecule/api-locales-scheduler.