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

@workkit/cron

v0.2.0

Published

Cron trigger patterns for Cloudflare Workers — declarative scheduled task handlers with distributed locking

Downloads

360

Readme

@workkit/cron

Declarative cron handler with task routing, middleware, and distributed locking

npm bundle size

Install

bun add @workkit/cron

Usage

Before (raw scheduled handler)

export default {
  async scheduled(event, env, ctx) {
    // Giant if/else chain matching cron expressions
    if (event.cron === "0 * * * *") {
      await syncUsers(env)
    } else if (event.cron === "*/5 * * * *") {
      await checkHealth(env)
    }
    // No timeout protection, no retry, no locking
  },
}

After (workkit cron)

import { createCronHandler, withTimeout, withRetry, withLock } from "@workkit/cron"

export default {
  scheduled: createCronHandler({
    middleware: [withTimeout(30_000), withRetry({ maxRetries: 3 })],
    tasks: {
      syncUsers: {
        schedule: "0 * * * *",
        handler: async (event, env, ctx) => {
          await syncUsers(env)
        },
      },
      healthCheck: {
        schedule: "*/5 * * * *",
        handler: withLock(
          { kv: (env) => env.LOCK_KV, key: "health-check" },
          async (event, env, ctx) => {
            await checkHealth(env) // Only one Worker runs this at a time
          },
        ),
      },
    },
  }),
}

API

Handler

  • createCronHandler(options) — Create a scheduled event handler that routes triggers to matching tasks

Matching

  • matchCron(taskSchedule, eventCron) — Check if a cron expression matches

Middleware

  • withTimeout(ms) — Abort tasks that exceed a time limit
  • withRetry(options) — Retry failed tasks with backoff
  • withErrorReporting(reporter) — Report errors to an external service

Distributed Locking

  • withLock(options, handler) — KV-based lock to prevent concurrent execution
  • acquireLock(kv, key, options?) — Manually acquire a distributed lock

Parser

  • parseCron(expression) — Parse a cron expression into fields
  • describeCron(expression) — Human-readable description ("Every 5 minutes")
  • nextRun(expression) — Calculate the next run time
  • isValidCron(expression) — Validate a cron expression

Jitter Middleware

  • withJitter(maxSeconds) — Add random delay before task execution to prevent thundering herd when multiple workers share the same schedule. Delay is uniformly distributed between 0 and maxSeconds.
import { createCronHandler, withJitter } from "@workkit/cron"

export default {
  scheduled: createCronHandler({
    middleware: [withJitter(30)], // random 0-30s delay
    tasks: { syncUsers: { schedule: "0 * * * *", handler: syncUsers } },
  }),
}

Cron Builder

  • cron() — Fluent cron expression builder. Chain .every(n?) or .on() with time units and .build() to produce a valid cron string.
import { cron } from "@workkit/cron"

cron().every(5).minutes().build()          // "*/5 * * * *"
cron().every().day().at(9).build()         // "0 9 * * *"
cron().on().monday().at(14, 30).build()    // "30 14 * * 1"
cron().every().weekday().at(8).build()     // "0 8 * * 1-5"

Task Dependencies

  • after: ['taskName'] — Declare dependencies between tasks. Tasks are topologically sorted and executed in dependency order. Dependent tasks are skipped if a dependency fails. Circular dependencies throw a ValidationError.
import { createCronHandler } from "@workkit/cron"

export default {
  scheduled: createCronHandler({
    tasks: {
      fetchData: { schedule: "0 * * * *", handler: fetchHandler },
      transform: { schedule: "0 * * * *", handler: transformHandler, after: ["fetchData"] },
      publish:   { schedule: "0 * * * *", handler: publishHandler, after: ["transform"] },
    },
  }),
}

License

MIT