---
title: Crons
description: Crons match the clock and run across servers.
---

A cron defines work on a schedule.

<Diagram caption="The jobs table queues one run per due cron and minute across servers.">
  <Flow>
    <Node title="tick" subtitle="Receives the scheduled minute" />
    <Arrow n={1} />
    <Node title="Crons" subtitle="Match schedules to time" />
    <Arrow n={2} />
    <Node title="Job" subtitle="One per due cron" tone="accent" />
  </Flow>
</Diagram>

```ts twoslash
import { init } from "logic-sdk";

const { cron, query } = init<{ scope: { kind: "system"; job: string; actor: null } }>();
const list = query({ async handler() { return ["Prepare keys"]; } });
declare function emailDigest(tasks: string[]): Promise<void>;
// ---cut---
export const sendDigest = cron({
  schedule: "0 8 * * MON-FRI",
  timezone: "Europe/Madrid",
  scope: { kind: "system", job: "tasks.sendDigest", actor: null },
  async handler(ctx) {
    await emailDigest(await ctx.run(list));
  },
});
```

- **Match (1).** `registry.jobs.tick(at)` selects crons due that minute. It defaults to the registry clock. Missed minutes stay missed.
- **Queue (2).** Export each cron. The tick queues job ID `<cron id>@<minute>`.
- **Scope.** Declare a system scope. Each `ctx.run` checks policy. The cron reads its scope from code. Follow-up jobs use `jobs.scope`.
- **Context.** `ctx.cron` contains the ID, scheduled minute, and attempt. Retries keep the minute. Hooks see `kind: "cron"` and `meta`.
- **Failure.** A retry repeats the handler. Make it idempotent. Put changes that must commit together in one mutation. The `failed` handler receives `ctx.cron`.

## Ticks and duplicate runs

Use [Node](/docs/guides/node) or [Cloudflare](/docs/guides/cloudflare). On other hosts, schedule `tick(at)` each minute and sweep stored jobs.

Migrate `cronRunsTable` alongside `jobsTable`. The table keeps tick records for one day to deduplicate runs. Without it, duplicate ticks queue duplicate runs. Runs from different minutes can overlap.

## Schedule syntax

Use minute, hour, day of month, month, and day of week. Use lists, ranges, steps, and names like `JAN` and `MON`. Sunday is `0` or `7`. If both day fields are restricted, either can match. The parser rejects seconds, aliases, `L`, `W`, and `#`.

Use an IANA `timezone`. The default is UTC. A skipped local minute never matches. A repeated local minute matches twice.

## Manual runs and tests

In production, run a cron now with `registry.jobs.invoke`.

```ts
await registry.jobs.invoke(tasks.sendDigest);
```

- **Manual run.** Pass a registered cron or ID to `invoke` and use `{ at }` to pick a minute. The normal tick stays in place. Handoff errors reach the caller.

In tests, tick the clock with `t.jobs.tick`.

```ts
await t.jobs.tick("2026-12-01T07:00:00Z");
```

- **Tick test.** `t.jobs.tick(at)` runs due crons inline and throws on failure. Omit `at` to use the test clock.
- **One cron.** `t.jobs.perform(tasks.sendDigest, { at })` runs it regardless of schedule.

## See also

- [Jobs](/docs/concepts/jobs)
- [Adapters](/docs/reference/adapters)
- [Testing](/docs/guides/testing)
