Cron schedules recurring background tasks with 5-field cron expressions, IANA timezones, overlap guards, and single-leader execution when a DistributedLock is present.
Use Cron for periodic jobs such as nightly billing, report generation, cleanup sweeps, or token reconciliation. When you deploy to Cloudflare Workers with @chimp-labs/keel-alchemy, Keel generates the matching Cloudflare Cron Trigger.
Declaring a Cron
Plain TypeScript SDK (@chimp-labs/keel/sdk)
import { k } from "@chimp-labs/keel/sdk"
export const BillingCron = k.cron("billing.nightly", {
expression: "0 2 * * *",
timezone: "UTC",
overlap: "skip",
lockTtl: "10 minutes",
})
Configuration options
| Option | Type | Default | Description |
|---|---|---|---|
expression |
string |
Required | Standard 5-field cron expression (minute hour day month weekday). |
timezone |
string |
"UTC" |
IANA timezone name for schedule evaluation. |
overlap |
"skip" | "allow" |
"skip" |
Whether to skip a tick if the previous run is still active or locked. |
lockTtl |
Duration.Input |
"5 minutes" |
Lease duration when acquiring a distributed leader lock across replicas. |
Effect v4 (@chimp-labs/keel/cron)
import { Cron } from "@chimp-labs/keel/cron"
export class BillingCron extends Cron("billing.nightly", {
expression: "0 2 * * *",
timezone: "UTC",
overlap: "skip",
}) {}
Attaching to a service
Declaring a Cron in resources makes Alchemy wire the Cloudflare Worker crons trigger or container schedule only for the service unit that owns it:
export const BillingService = k.service("billing", {
resources: [InvoicesTable, BillingCron],
operations: { /* ... */ },
})
Operations reference
Running and scheduling
// Compute the next scheduled execution timestamp
const next = await keel.billingCron.nextRun()
// Execute one tick deterministically (ideal for tests or Cloudflare scheduled() handlers)
await keel.billingCron.runOnce(async (scheduledAt) => {
await reconcileInvoices(scheduledAt)
})
// Start a supervised recurring daemon (single-leader when DistributedLock is configured)
await using cronDaemon = await keel.billingCron.start(
async (scheduledAt) => {
await reconcileInvoices(scheduledAt)
},
{
onError: (report) => console.error("Cron tick failed:", report.message),
}
)
SDK and Effect API mapping
SDK method (keel.<cron>.*) |
Effect function (@chimp-labs/keel/cron) |
Description |
|---|---|---|
nextRun(from?) |
nextCronRun(cron, from?) |
Calculates the next Date matching the expression. |
runOnce(handler, scheduledAt?) |
runCronOnce(cron, task, scheduledAt?) |
Runs one tick with an optional distributed leader lock. |
start(handler, opts?) |
startCron(cron, task, opts?) |
Starts a scoped recurring scheduler with supervision. |
| Pure helper | parseCronExpression(expr) |
Parses and validates a 5-field cron string. |
Errors and failure modes
| Error tag | Category | HTTP status | Retryable | When it occurs |
|---|---|---|---|---|
CronParseError |
invalid |
400 |
No | Invalid 5-field cron expression syntax. |
CronLockError |
internal |
500 |
Yes | Failure communicating with the underlying DistributedLock. |
Backends and infrastructure
- On Memory, Node.js, and Docker with PostgreSQL 17, SQLite, or SurrealDB 4,
Cronruns as a scoped Effect fiber daemon using Effect’sClockand an optionalDistributedLockso multiple replicas do not duplicate a tick. It works alongsidebackend: "postgres"(KeelPostgresLive),surreal,sqlite, andmemory. - On Cloudflare Workers with
@chimp-labs/keel-alchemy, Keel infers and registers native Cloudflare Cron Triggers from everyCroninservice.resourcesand dispatches each trigger torunOnce.
Testing in memory
await using keel = await createKeel({
backend: "memory",
resources: { billingCron: BillingCron },
})
const runs: Date[] = []
await keel.billingCron.runOnce(async (ts) => {
runs.push(ts)
}, new Date("2026-01-01T02:00:00Z"))
// runs.length === 1