Skip to content

Schedules

Run tools on a cron schedule or trigger them manually.

Updated

A schedule runs one of an extension’s tools with a fixed JSON input on a recurring cron calendar. Instead of introducing a separate execution model for background jobs, every scheduled run is an ordinary call of one of the extension’s tools.

Schedule declarations in index.ts

Import cron from @buildr/sdk and pass your schedules under schedules in defineExtension:

import { boolean, cron, defineExtension, integer, object, tool } from "@buildr/sdk";

const syncProjects = tool(
  {
    description: "Syncs projects into the repo's Cost centers tag family",
    input: object({
      workspaceId: integer(),
      fullRefresh: boolean().optional(),
    }),
  },
  async (ctx, { workspaceId, fullRefresh = false }) => {
    // Read external data and commit to ctx.repo.id
    return { workspaceId, fullRefresh };
  },
);

export default defineExtension(
  {},
  {
    tools: {
      linear: { syncProjects },
    },
    schedules: {
      nightlySync: cron(
        { expression: "0 2 * * *", timezone: "America/Chicago" },
        syncProjects,
        { workspaceId: 42, fullRefresh: true },
      ),
    },
  },
);

Schedule validation rules

When the compiler inspects your bundle during the declaration stage, it checks every entry in schedules:

  • Schedule name: Each key in schedules must be a valid JavaScript identifier (/^[A-Za-z_][A-Za-z0-9_]*$/) of at most 128 characters.
  • Target tool: The tool passed to cron(calendar, tool, input) must also be exported in the extension’s tools tree. Passing a tool that isn’t there fails the build with DeclarationFailed.
  • Cron calendar: expression must be a valid five-field cron expression (minute hour day-of-month month day-of-week). Forge runs schedules by the minute, so a sixth field for seconds is refused. timezone must be a valid IANA time zone name (such as "America/Chicago" or "America/Los_Angeles"). When timezone is omitted, it defaults to "UTC".
  • Static input: The fixed input passed to cron is validated against the target tool’s input schema at build time. If input does not match the tool’s schema, the build fails with DeclarationFailed.

You can inspect the schedules of the version a branch publishes before releasing it with GET /repos/:repo_id/branches/:branch/extensions/:name/schedules (forge extensions list-extension-schedules).

Schedule settings in an installation

When a repo installs an extension on main, every schedule starts disabled (enabled: false). When an installation updates to a higher version, retained schedules keep their enabled setting and added schedules start disabled.

Schedule settings are versioned in the installing repo’s DAG. Turning a schedule on or off takes manage on the schedule (Actions), which the default Editor policy doesn’t grant. To turn a schedule on or off, update it on a branch, commit, and merge to main. Only schedule settings committed to main’s head commit run:

# List the schedules declared by the installed version and their enabled status
forge extensions list-extension-installation-schedules --repo "$OPS_REPO" --branch main --name integrations.linear

# Enable a schedule on a branch and commit to main
forge extensions upsert-extension-installation-schedule --repo "$OPS_REPO" --branch main \
  --name integrations.linear --schedule nightlySync --data '{"enabled":true}'

forge commits create-commit --repo "$OPS_REPO" --branch main \
  --data '{"message":"Enable nightly Linear sync"}'

Schedule settings in the defining repo

The repo that defines an extension runs its schedules on its own main as if it had installed it, at the version that main’s head commit publishes. Its settings live in the same DAG table as an installation’s and work the same way: each schedule starts disabled, and you turn it on or off on a branch, commit, and merge to main. Only main’s committed settings run.

# List the schedules the branch head's version declares and their enabled status
forge extensions list-extension-schedules --repo "$DIRECTORY_REPO" --branch main --name linear

# Turn one on and commit it
forge extensions upsert-extension-schedule --repo "$DIRECTORY_REPO" --branch main \
  --name linear --schedule nightlySync --data '{"enabled":true}'
forge commits create-commit --repo "$DIRECTORY_REPO" --branch main \
  --data '{"message":"Enable nightly Linear sync"}'

While the head’s version is still publishing, listing and setting answer ExtensionPublicationPending, and on main none of its schedules run. A schedule that a later version drops stays on without running until a version declares it again.

Execution identity and connections

When a schedule runs (either on its cron timing or through a manual trigger), it executes with a specific security context:

  • Who it runs as: A run’s ctx.forge calls act as the extension itself, a timed run and a manual run alike, for an installation and the defining repo alike. A manual run needs run on the schedule from the member who starts it, but that member lends the run none of their access. What it commits names the extension as its author, such as “integrations.linear (extension)”, and holds only what the run wrote: a run writes on a branch of its own and merges into main when it succeeds.
  • Org connections only: Because a scheduled run has no interactive calling user, ctx.org.connections holds the repo’s bound org connections, whereas reading ctx.user.connections.<name> throws an error explaining that schedule runs have no user connections.

Manual runs and run history

A member with run on the schedule (Actions) can trigger a schedule of main’s committed installation manually, whether the schedule is enabled or disabled, and a member with readRuns can inspect past runs:

# Run a schedule immediately and wait for the result
forge extensions create-extension-installation-schedule-run --repo "$OPS_REPO" \
  --name integrations.linear --schedule nightlySync

# List past runs of the schedule (newest first)
forge extensions list-extension-installation-schedule-runs --repo "$OPS_REPO" \
  --name integrations.linear --schedule nightlySync

In the repo that defines an extension, members with run and readRuns on the schedule run and inspect it on main the same way, at the version main’s head publishes. While that version is still publishing, the run answers ExtensionPublicationPending:

forge extensions create-extension-schedule-run --repo "$DIRECTORY_REPO" \
  --name linear --schedule nightlySync

forge extensions list-extension-schedule-runs --repo "$DIRECTORY_REPO" \
  --name linear --schedule nightlySync

Only one run of a given schedule can be active at a time. Triggering a manual run while another run of the same schedule is still in flight fails with ExtensionScheduleRunActive.

Forge dispatches timed runs by these rules:

  • No catch-up: Occurrences that fall due while a schedule’s run is in flight, or while Forge couldn’t run it, merge into one run for the earliest of them. The next occurrence counts from the time that run starts.
  • No retries: Forge never runs a failed run again. Run it by hand, or wait for its next occurrence.
  • Interrupted runs: A run that doesn’t report how it ended within 2 minutes is marked interrupted.
  • Runs in flight: At most 8 schedule runs are in flight in a repo at once, manual runs included. A schedule that falls due past that bound waits until a run finishes.
  • Run history: Each schedule keeps its latest 100 runs.

Each ExtensionScheduleRun record reports:

  • trigger: "schedule" for cron-dispatched runs or "manual" for runs started via POST .../runs.
  • status: "running", "succeeded", "failed", or "interrupted".
  • scheduled_at, started_at, and finished_at: ISO-8601 timestamps.
  • failure: A single-line error description (with source-mapped file, line, and column if the tool threw) when status is "failed", and null otherwise.

Type to search…

↑↓ navigate↵ selectEsc close