Scheduled functions

Run functions on a cron schedule in the time zone of your choice, for reports, cleanups, syncs and reminders.

The schedule trigger runs a handler on a cron schedule.

YAML
triggers:
  nightlyReport:
    type: schedule
    active: true
    options:
      frequency: "0 3 * * *"
      timezone: Europe/Istanbul
OptionDescription
frequencyRequired. A cron expression with 5 fields, or 6 fields when the first one is seconds.
timezoneRequired. An IANA time zone such as UTC, Europe/Istanbul or America/New_York.

The handler receives no arguments:

JavaScript
export async function nightlyReport() {
  const report = await buildReport();
  console.info("Report ready", report.id);
}

Cron syntax

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

Each field accepts *, a number or a step such as */15.

ExpressionRuns
* * * * *Every minute.
*/5 * * * *Every 5 minutes.
*/30 * * * * *Every 30 seconds.
0 */3 * * *Every 3 hours, on the hour.
0 9 * * 1Every Monday at 09:00.
30 2 1 * *At 02:30 on the first day of every month.

Good practices

  • Make runs idempotent. A slow run can overlap with the next one, and a failed run leaves partial work behind. Use flags or timestamps in your data to skip work that is already done.
  • Process in batches. Fetch and update documents page by page, and stay well under the function's timeout.
  • Record progress. Store a "last processed" marker in a bucket so the next run continues where the last one stopped.
  • Mind multiple replicas. With several API replicas, turn on --replication so that each scheduled run is claimed by exactly one replica.

See the Scheduled cleanup recipe for a complete example.