trigger-scheduler
What it does
The clock for Spine automation. netlify.toml declares it a Netlify scheduled function at * * * * * — Netlify Clockwork invokes it every minute. The file itself is a thin shim: it detects whether the invocation is a genuine scheduled tick (which arrives with no credentials) and runs the shared system-cron handler under a fabricated system context, or falls through to system-cron's normal middleware auth for everything else. All actual work — due schedules, due timers, delivery retries, log cleanup — lives in system-cron.
Route and methods
Two invocation shapes:
- Netlify scheduled invocation — the platform calls
/.netlify/functions/trigger-scheduleronce per minute with a schedule indicator header and a{"next_run": "<ISO timestamp>"}body. - Manual HTTP call — any authenticated request to the same path (useful for forcing a tick in dev). This falls through to
system-cron'screateHandlerauth path.
Authentication
Clockwork detection (isNetlifyScheduledInvocation) requires both:
- a schedule indicator —
x-netlify-event: schedule(production canonical header),x-nf-event: schedule(Netlify CLI emulation), or aNetlify Clockworkuser-agent when neither header is present — and - a JSON body whose
next_runfield is a string.
Requiring the body shape means a bare spoofed header or UA alone cannot authenticate. A detected tick fabricates a RequestContext carrying SYSTEM_PRINCIPAL (machine, machineType: 'internal', *:* scope) and adminDb, then calls the cron handler directly — createHandler short-circuits when requestId + principal are already present, so no second auth resolution happens.
Anything that fails detection goes through system-cron's own checks:
x-internal-secretheader → resolves toSYSTEM_PRINCIPALviaresolvePrincipal, which passes the internal-machine gate.?api_key=or?scheduler_key=query param equal toSCHEDULER_API_KEY.- any other internal machine principal.
- anonymous requests → 401 from
createHandler; authenticated but unauthorized → 403 plus asystem_cron.unauthorized_accessaudit event.
Request contract
- Scheduled tick: schedule indicator header + body
{"next_run": "<ISO timestamp>"}. - Manual call:
?api_key=$SCHEDULER_API_KEY(or?scheduler_key=), or anx-internal-secretheader. No request body is read.
Response contract
Whatever system-cron's handler returns — a JSON summary of the tick (schedules_due, executed, success, failed, skipped, logs_cleaned, deliveries_retried, results[]), or an auth/500 error. See the system-cron reference page for the full shape.
Side effects
None of its own — every effect belongs to system-cron. The only local decision is which auth path the invocation takes.
Errors
- 401 — unauthenticated non-scheduled request (from
createHandler). - 403 — authenticated but not an internal machine principal and no valid scheduler key (from
system-cron, with audit event). - 500 — bubbled from
system-cronfailures (system_cron.erroraudit).
Agent notes
- The function is deliberately a shim: scheduled-invocation plumbing has changed across Netlify environments before (CLI vs production header names), so detection is centralized here rather than inside the worker. If a future Netlify rename breaks ticks, this file is the fix point — the Clockwork user-agent check is already the third fallback indicator.
- A "401 every minute" symptom in production usually means the scheduler hit the non-Clockwork path — check that the invocation carries a recognized schedule header and the
next_runbody. - There is no per-tick idempotency key: two overlapping ticks would both run
system-cron. Due-item claiming relies on the RPCs and row state, not on tick uniqueness.