# Task Scheduling

## Introduction

Some work runs on a clock: expired sessions to delete at night, numbers to count again every hour. A module writes its tasks in a
schedule, and a process of its own, `marmeon schedule:work`, runs each task when it is due:

```ts title="modules/system/schedule.ts"
import { defineSchedule } from '@marmeon/scheduler';

export const schedule = defineSchedule((s) => {
  s.command('session:prune').everyFifteenMinutes().withoutOverlapping().onOneServer();
  s.command('cache:prune').hourlyAt(25).withoutOverlapping().onOneServer();
  s.command('queue:prune-failed', ['--hours=168']).daily().at('03:20').timeZone('Europe/Berlin').onOneServer();
});
```

The module lists its schedule in its definition:

```ts title="modules/system/index.ts"
import { defineModule } from '@marmeon/core';
import { schedule } from './schedule.ts';

export default defineModule({
  name: 'system',
  schedule,
});
```

`marmeon dev` starts the scheduler next to the server whenever a module has a schedule. `pnpm marmeon schedule:list` shows every
task, when it runs next and its options.

## Defining tasks

A schedule has three kinds of task. Each one needs a frequency, and its options come after it.

### Commands

`s.command(name, args)` runs a console command in the scheduler's process, with its arguments as a list:

```ts
s.command('notifications:prune', ['--days=30']).daily().at('03:30');
```

`s.command()` also takes the command's class. A command that ends with an exit code other than 0 counts as a failed run. Each run
gets a container scope of its own, as it does on the command line. The [console](console.md#writing-commands) page shows how to
write a command.

### Jobs

`s.job(Job, payload)` queues a job when the task is due, and a worker runs it. The payload is optional when the job's schema takes
an empty object:

```ts title="modules/user-profile/schedule.ts"
import { defineSchedule } from '@marmeon/scheduler';
import { RebuildMemberStatsJob } from './jobs/RebuildMemberStatsJob.ts';

export const schedule = defineSchedule((s) => {
  s.job(RebuildMemberStatsJob).hourly();
});
```

The app must register the job: a module lists it in `jobs`, or a package's provider lists it in `static [providerJobs]`. It is queued as a [unique job](queues.md#unique-jobs) until a worker starts it, so
while the job of the last hour still waits, because the workers are behind or down, the task queues no second one.

A job named `<name>:schedule` meets the unique key of `s.job()` of the job `<name>`: that task fails with a `ScheduleError` until you rename the job `<name>:schedule`.

### Callable task classes

`s.call(Class)` builds a class through the container, in a scope of its own, and calls its `handle()`:

```ts title="modules/user-profile/tasks/PruneDataExports.ts"
import { Clock } from '@marmeon/core';
import { Storage } from '@marmeon/storage';

export class PruneDataExports {
  readonly #storage: Storage;
  readonly #clock: Clock;

  constructor(storage: Storage, clock: Clock) {
    this.#storage = storage;
    this.#clock = clock;
  }

  async handle(): Promise<void> {
    const disk = this.#storage.disk('local');
    const before = this.#clock.now() - 15 * 60_000;
    for (const key of await disk.list('exports')) {
      if ((await disk.lastModified(key)).getTime() <= before) await disk.delete(key);
    }
  }
}
```

```ts
s.call(PruneDataExports).everyTenMinutes().withoutOverlapping().onOneServer().name('user-profile.prune-data-exports');
```

### Names

A task's name appears in `schedule:list`, in `schedule:test`, in the log, in traces and in its locks. Never pass a secret as an argument of a scheduled command: read it in the command. By default it is the command with its
arguments, such as `queue:prune-failed --hours=168`, the job's `static job` or the class name. The arguments belong to the name,
so the same command with other arguments is another task, with locks of its own. `.name()` sets another one. Give a class a
name with its module in front, as above, so it stays unique across modules. `.description('…')` adds a line that `schedule:list` shows under the task, and prints as `description` in its `--json`.

The app does not start when two tasks have one name, when a task has no frequency, or when a cron expression or a time zone is
invalid.

## Frequencies

| Method | When |
| --- | --- |
| `everyMinute()`, `everyTwoMinutes()`, `everyThreeMinutes()`, `everyFiveMinutes()`, `everyTenMinutes()`, `everyFifteenMinutes()`, `everyThirtyMinutes()` | Every so many minutes. |
| `hourly()` | At minute 0 of every hour. |
| `hourlyAt(minute)` | At that minute of every hour. |
| `everyTwoHours()`, `everyThreeHours()`, `everySixHours()` | Every so many hours, at minute 0. |
| `daily()` | At midnight. |
| `dailyAt('03:10')` | Every day at that time. |
| `weekly()` | Sundays at midnight. |
| `weeklyOn('monday', '8:00')` | That day of the week, at that time. |
| `monthly()` | On the 1st at midnight. |
| `monthlyOn(1, '6:00')` | That day of the month, at that time. |
| `yearly()` | On January 1st at midnight. |
| `cron('*/5 9-17 * * mon-fri')` | A cron expression of your own. |

After a frequency, `at('03:10')` sets the time of day, `weekdays()` and `weekends()` limit it to those days, and `days('monday',
'friday')` to the days you name. A time is `HH:MM` from `0:00` to `23:59`, and a literal that is no time is a compile error. An
option before a frequency is a compile error too:

```text
Say how often first — .daily().onOneServer(): a task needs a frequency before its options
```

### Time zones

A task's times are on the clocks of `SCHEDULE_TIMEZONE`, UTC by default, or of the zone that `.timeZone('Europe/Berlin')` names.
Daylight saving time counts. A task at a time the clocks skip, such as 02:30 on the last Sunday of March in Berlin, runs right after
the gap. A task at a time that happens twice, 02:30 in October, runs the first time only. Tasks that run every hour run as the hours
really pass.

## Conditions

| Option | What it does |
| --- | --- |
| `when(filter)` | Runs only when `filter` returns `true`. |
| `skip(filter)` | Does not run when `filter` returns `true`. |
| `environments('production')` | Runs only in these environments, by `APP_ENV`. Without it the name follows `NODE_ENV`: `local`, `testing` or `production`. |

A filter is asked every time the task is due. It gets the run's container scope, so it can ask a service, here a config
definition of the module:

```ts
s.command('reports:send').daily().at('07:00').when((scope) => scope.make(ReportsConfig).enabled);
```

`environments()` compares against the environment's name, `APP_ENV`. A staging server, `NODE_ENV=production APP_ENV=staging`,
runs the tasks of `.environments('staging')` and leaves those of `.environments('production')` out:

```ts title="modules/reports/schedule.ts"
import { defineSchedule } from '@marmeon/scheduler';

export const schedule = defineSchedule((s) => {
  // Mails to customers: production only, never from staging.
  s.command('reports:send').daily().at('07:00').environments('production');
  // A copy of production's data for the testers: staging only.
  s.command('reports:refresh-demo').daily().at('05:00').environments('staging');
});
```

Selecting tasks is all `APP_ENV` does here. It never weakens a protection and never enables a development tool, which follow
`NODE_ENV` alone, as the [configuration](configuration.md#app_env-the-environments-name) page explains.

`schedule:work`, `schedule:run` and `schedule:test` check at each start that every `s.command()` names a command the app has and every `s.job()` a job the app registers, and they refuse to start otherwise. A task limited to other environments is left out of the check of its command, because it never runs here and its package may be a dev dependency: `s.command('devtools:prune').hourlyAt(40).environments('local')`. The check of a job's name covers every environment.

A job's name can belong to another class: the app registers `SyncJob` as `pkg.sync`, and a task queues a class with the same `static job`, for example a subclass that inherits it. The scheduler starts and the other tasks run. The queue of `@marmeon/queue` refuses that class, so the task's runs fail. `schedule:work` prints one warning for each such task that runs in this environment, once at its start. `schedule:run` and `schedule:test` print none: `schedule:run` reports the failed run when such a task is due, `schedule:test` when you run that task, and both then exit with 1. To fix it, schedule the class the app registers, or a job class of your own (not a subclass of it) with another name (not under `marmeon.`), listed in a module's `jobs`.

## Several servers and long runs

### One server only

With several servers that each run a scheduler, every task would run on each of them. `onOneServer()` runs it on one: the first
scheduler to take the task's lock for that minute runs it, and the others skip it. The lock lives in the app's cache and is kept an
hour.

### No overlapping runs

`withoutOverlapping()` skips a run while the previous one still runs, on any server. The lock expires after 24 hours, or after
the [span](time.md#spans-of-time) you pass, `withoutOverlapping({ expiresAfter: { minutes: 30 } })`, so a scheduler that crashed cannot block the task forever. A scheduler that stops
before its runs end frees their locks. `s.job()` takes no `withoutOverlapping()`: queuing a job never overlaps, and the type error
says so. Runs of the job itself that must not overlap use the job's [middleware](queues.md#job-middleware).

### The cache they need

Both options lock in the app's [cache](cache.md). With more than one scheduler, the cache must be one they share: `database` or
`redis`. With `file` the locks hold on one machine, and with `memory` in one process. That is right for a single scheduler, so
`schedule:work`, `schedule:run` and `schedule:list` only warn:

```text
onOneServer()/withoutOverlapping() lock in CACHE_DRIVER=file: locks only hold on this machine. With more than one
scheduler instance (more servers), use CACHE_DRIVER=database (marmeon make:cache-table) or redis. Tasks: session:prune,
cache:prune, queue:prune-failed --hours=168
```

Two schedulers on one machine share a `file` cache, so a task with `onOneServer()` still runs once. With `memory` each process has
locks of its own, and a second scheduler runs everything again.

## Missed runs

A scheduler that was down, during a deploy or on a machine that slept, runs a task it missed **once** when it is back, however
often the task was due meanwhile. It looks back at most 7 days. `catchUp('skip')` waits for the next due time instead.

The scheduler keeps the time of its last tick in the cache: one for the tasks with `onOneServer()`, which any server's tick
updates, and one per host name for the others. So nothing is caught up after `cache:clear`, and a task without `onOneServer()`
catches up nothing in a container that starts with a new host name.

## Running the scheduler

`schedule:work` checks every minute what is due, until it is stopped:

```sh
pnpm marmeon schedule:work
```

Its first line names its tasks and the environment it assumed, such as `Running the schedule every minute: 3 tasks (session:prune,
cache:prune, queue:prune-failed --hours=168) — in production (APP_ENV=production)`. On `SIGTERM` or `SIGINT` it starts no new run, gives
the runs still going `--stop-timeout` seconds, 30 by default and at most 2073600 (24 days), then ends and frees their locks.

| Command | What it does |
| --- | --- |
| `schedule:work [--stop-timeout=30]` | Runs the schedule every minute, until stopped. |
| `schedule:run` | Runs what is due now, waits for the runs and ends: with exit code 1 when a run failed. |
| `schedule:list [--json]` | Lists the tasks, their schedule in their time zone, the next run and their options. |
| `schedule:test <name>` | Runs one task now, whatever its frequency, filters and environments say. Its overlap lock still counts. |

The schedule commands mask what they print with the app's redactor, as the log does with a failed task's error. This covers task names, the error of a failed run, the message for an unknown task name and the lock warning. Where an error quotes a secret, you see `[redacted]`. In the terminal, control characters print escaped (`\n`, `\u001b`), so a task name or an error cannot start a line of its own. `schedule:list --json` prints names masked but unescaped, because JSON escapes them itself.

`schedule:list --json` prints the same as a [machine-readable document](console.md#machine-readable-output): `format` (2), `tasks` and
`warnings`. Each task has `name`, `expression`, `timeZone`, `next` (the next run as a time with its offset, `2026-10-06T03:10+02:00`,
or `null` when none comes within 400 days), `does` (what the task runs, as in the table's Does column: `call Class`, `job name {payload}` or `command name args`, with secrets masked), `description` (the text of `.description()`, or `null`) and `options`: `oneServer` (a boolean), `overlapSeconds` (the lock's span in seconds, or `null`
without `withoutOverlapping()`), `catchUp` (`once` or `skip`), `environments` (a list, or `null` for every environment) and
`filtered` (a boolean: the task has `when()` or `skip()`). The lock warning that the text prints is in `warnings`.

A job's payload and a command's arguments are shown as passed, apart from what the redactor masks (`--password=…`, `"apiKey"`); it does not mask a value that follows its flag after a space, like `--token abc`.

In production, run **one** `schedule:work` per server under a supervisor, next to the web processes and the workers. A task with
`onOneServer()` then runs once across them. `marmeon make:docker` writes the role as a `scheduler` container and as a systemd unit;
the [deployment](deployment.md#roles-and-processes) page covers the roles.

Without a supervisor, a system cron can call `schedule:run` every minute. A crontab line gets no environment of its own, so name
`NODE_ENV` in it:

```text
* * * * * cd /app && NODE_ENV=production marmeon schedule:run
```

A scheduler that is `development` only because of the app's `.env` logs a warning and runs anyway. The
[configuration](configuration.md#where-node_env-comes-from) page explains where `NODE_ENV` comes from. Without `@marmeon/cache`
the scheduler refuses to start, because it keeps its locks and its last tick there.

### What a run is

Every run is a unit of work of its own, like a request or a job: outside any transaction, in a container scope of its own, with
`task` in its log entries. Runs never wait for each other. A run that fails is logged and handed to the app's
[error reporters](errors.md#reporting) (`source` `schedule`, with the task's name), and the others go on.

## Configuration

| Variable | Default | Effect |
| --- | --- | --- |
| `SCHEDULE_TIMEZONE` | `UTC` | Whose clocks the tasks' times are on: an IANA name such as `Europe/Berlin`, checked at the start. |
| `APP_ENV` | `local`, `testing` or `production`, by `NODE_ENV` | What `environments()` compares against. |

## Housekeeping commands

The framework's tables grow until something prunes them. Schedule these commands in an app that uses the tables:

| Command | What it deletes | Read more |
| --- | --- | --- |
| `session:prune` | Expired sessions of the `database` driver. | [Session](session.md#expiry-and-clean-up) |
| `cache:prune` | Expired cache entries and locks. | [Cache](cache.md#expired-entries-and-clearing) |
| `queue:prune-failed --hours=168` | Failed jobs older than a week. | [Queues](queues.md#failed-jobs) |
| `notifications:prune` | Notifications read long ago. | [Notifications](notifications.md#the-notifications-table) |
| `uploads:prune` | Uploads that were never used, or used up. | [File storage](storage.md#the-uploads-table) |
| `auth:prune-tokens` | Expired reset and API tokens, remember-me tokens that expired or are older than `AUTH_REMEMBER_DAYS` (taken off their sign-ins), the sign-ins that are over, and, where the app binds an `EmailChangeStore`, changes of address nobody confirmed in time. It also prunes the reset links and sign-ins of every [guard of a package](authentication.md#operator-commands-for-a-guard-of-a-package). | [Remember me](authentication.md#remember-me), [sign-ins](authentication.md#pruning-sign-ins) |

The starter kit's auth module schedules `auth:prune-tokens` at 03:10, which also clears the changes of address nobody confirmed in
time, since the kit binds an `EmailChangeStore`. Its schedule also holds `auth:prune-unverified`, which deletes accounts whose address
was never confirmed, as a line that is commented out: deleting accounts is your app's decision, so turn it on deliberately. The
[starter kit](starter-kit.md#accounts-nobody-confirmed) page explains it.

## Events

Every run says what became of it through the app's event dispatcher:

| Event | When | Fields besides `task`, `dueAt` and `reason` |
| --- | --- | --- |
| `ScheduledTaskStarting` | The run passed its filters and locks and starts. | none |
| `ScheduledTaskFinished` | The run ended. | `ms` |
| `ScheduledTaskFailed` | The run threw, or its command ended with another exit code than 0. | `error`, `ms` |
| `ScheduledTaskSkipped` | The task was due and did not run. With `job-waiting` it comes after `ScheduledTaskStarting`, because the queue says no only when the task tries to queue its job. | `skipped`: `filter`, `other-server`, `overlapping` or `job-waiting` |

`dueAt` is the time the task was due (epoch ms; for a catch-up, the last time it was missed). `reason` says why it ran: `due`, `catch-up` or `test`. All of them extend `ScheduledTaskEvent`. A listener runs in the scheduler's
process, and its error is logged and changes nothing about the run. This one raises an alert for every failed task:

```ts title="modules/system/listeners/AlertOnFailedTask.ts"
import { Logger, Redactor } from '@marmeon/core';
import type { ScheduledTaskFailed } from '@marmeon/scheduler';

export class AlertOnFailedTask {
  readonly #logger: Logger;
  readonly #redactor: Redactor;

  constructor(logger: Logger, redactor: Redactor) {
    this.#logger = logger.child({ channel: 'alerts' });
    this.#redactor = redactor;
  }

  handle(event: ScheduledTaskFailed): void {
    const message = event.error instanceof Error ? event.error.message : String(event.error);
    this.#logger.error(`The scheduled task ${event.task} failed: ${this.#redactor.text(message)}`, { alert: true, task: event.task });
  }
}
```

The module registers it with `listeners: [listen(ScheduledTaskFailed, AlertOnFailedTask)]`, as the
[events](events.md#registering-listeners) page shows.

## Testing

`app.schedule` runs the schedule on the test's clock:

```ts title="modules/system/schedule.test.ts"
import { createTestApp } from '@marmeon/testing';
import { expect, it } from 'vitest';
import application from '../../bootstrap/app.ts';

it('prunes failed jobs at night, Berlin time', async () => {
  const app = await createTestApp(application, { database: 'refresh' });
  expect(app.schedule.due('2026-10-04T03:20:00+02:00')).toContain('queue:prune-failed --hours=168');
  expect(app.schedule.due('2026-10-04T03:20:00Z')).not.toContain('queue:prune-failed --hours=168');

  const results = await app.schedule.run('2026-10-04T03:20:00+02:00');
  expect(results.find((result) => result.task === 'queue:prune-failed --hours=168')).toMatchObject({ outcome: 'ran' });
});
```

`due(time)` lists the tasks due then, `run(time)` moves the clock there and runs them, `runTask(name)` runs one, and `tasks` lists
every name. The [fakes](fakes.md#the-schedule) page covers the schedule's fake, and the
[time](time.md#the-schedule-and-the-clock) page the clock.
