Skip to Content
Config SystemDefining Schedules

Defining Schedules

Schedules trigger operations on a recurring basis using cron expressions. Use schedules for periodic syncs, cleanups, report generation, or any task that needs to run at regular intervals.

Schedule Interface

interface ApplyConfigScheduleInput { key?: string; // Unique key (defaults to operationKey) name?: string; // Display name (defaults to key) description?: string; // What this schedule does operationKey: string; // Key of the operation to trigger cron: string; // Cron expression (e.g., '0 2 * * *') timezone?: string; // IANA timezone (e.g., 'America/New_York') enabled?: boolean; // Whether the schedule is active (default: true) each?: 'organization'; // One run in each organization (default: one run) }

Basic Example

Run an operation every night at 2:00 AM:

import { defineConfig, defineSchedule } from '@foir/cli/configs'; export default defineConfig({ key: 'my-app', name: 'My App', operations: [ { key: 'nightly-sync', name: 'Nightly Sync', endpoint: '/sync/full', }, ], schedules: [ defineSchedule({ operationKey: 'nightly-sync', cron: '0 2 * * *', timezone: 'America/New_York', enabled: true, }), ], });

Cron Expressions

Schedules use standard five-field cron syntax:

┌───────────── minute (0-59) │ ┌───────────── hour (0-23) │ │ ┌───────────── day of month (1-31) │ │ │ ┌───────────── month (1-12) │ │ │ │ ┌───────────── day of week (0-6, Sunday=0) │ │ │ │ │ * * * * *

A cron the scheduler cannot read is refused the same way, naming it: five fields, with days of the week from 0 (Sunday) to 6. 0 9 * * 7 is refused; write Sunday as 0.

Common Patterns

ExpressionMeaning
0 * * * *Every hour, on the hour
*/15 * * * *Every 15 minutes
0 2 * * *Daily at 2:00 AM
0 9 * * 1-5Weekdays at 9:00 AM
0 0 * * 0Every Sunday at midnight
0 0 1 * *First day of every month at midnight
0 6,18 * * *Twice daily at 6:00 AM and 6:00 PM

Timezone Support

Specify an IANA timezone to control when the schedule runs relative to a specific region. If omitted, the schedule runs in UTC.

A timezone the IANA list  does not name is refused when the schedule is pushed, created or changed, and the refusal names it. The names are case-sensitive, so Europe/london is refused. EST is accepted, but as a fixed UTC-05:00 offset with no daylight saving, so use America/New_York for New York time.

schedules: [ { operationKey: 'morning-report', cron: '0 9 * * 1-5', timezone: 'Europe/London', }, ]

Common timezone values:

TimezoneRegion
UTCCoordinated Universal Time
America/New_YorkUS Eastern
America/Los_AngelesUS Pacific
Europe/LondonUK
Europe/BerlinCentral Europe
Asia/TokyoJapan
Australia/SydneyAustralia Eastern

Enabling and Disabling

Set enabled to false to pause a schedule without removing it from the config:

schedules: [ { operationKey: 'experimental-job', cron: '0 3 * * *', enabled: false, // Paused -- will not run }, ]

Push the config again with enabled: true to resume. Pausing and resuming take effect on push, without a release, because a pause is how you stop a schedule in a hurry. The schedule’s cron, timezone and operation are its definition and wait for a release, as does a new schedule.

Running in every organization

A schedule starts one run, at the project’s own level. That run reaches nothing inside your organizations: their records are theirs. For routine work each organization needs, such as topping up next month’s bookable days or expiring stale carts, say so on the schedule:

operations: [ { key: 'extend-sessions', name: 'Extend sessions', endpoint: '/upkeep/extend-sessions', grants: ['records:read:channel', 'records:create:session'], }, ], schedules: [ defineSchedule({ operationKey: 'extend-sessions', cron: '0 3 * * *', each: 'organization', }), ],

Each time it fires, the schedule starts one run of the operation in every active organization of the project, at every depth of the tree. Nothing runs at the project’s own level.

Each run is that organization’s and no other’s. The token it is dispatched with names the organization, and so does the org_id in the X-Foir-Context header. What the run creates sits at that organization’s level, what it lists is that organization’s and the project’s shared content, and the organization beside it is out of reach. Your endpoint is called once per organization and does the work for the one it was called for. It holds what the operation declares, as any run does, and it acts for no user.

An organization that is suspended or scheduled for deletion gets no run. One made after a fire gets its first run at the next.

One organization’s failure is its own. The others’ runs are not held up by it, and it shows on that run and in the dead letters like any failed run. The schedule is switched off after five fires in a row in which no organization’s run succeeded, which is what an endpoint that is down looks like. A fire counts once however many organizations it ran in.

Running it now runs it in each. foir schedules trigger starts the same fire.

Two schedules cannot say each, and are refused when you write them:

  • A schedule made inside an organization. It already runs there.
  • A schedule of an app’s operation. An app’s operation acts where the app is installed whichever level starts it, so there is nothing to run in each. Name an operation of your project’s own.

Taking each out of the config and pushing takes the schedule back to one run. Like the cron and the operation, it is part of the schedule’s definition and waits for a release.

Examples

Daily Content Sync

schedules: [ { operationKey: 'full-sync', cron: '0 2 * * *', timezone: 'UTC', enabled: true, }, ]

Hourly Cache Warm

schedules: [ { operationKey: 'warm-cache', cron: '0 * * * *', }, ]

Weekly Report

schedules: [ { operationKey: 'send-report', cron: '0 9 * * 1', timezone: 'America/New_York', }, ]

Next Steps

Last updated on