system-cron
What it does
The worker behind every trigger-scheduler tick. Each authenticated run performs, in order: a stale-execution sweep, due-schedule execution, due- timer pipelines, webhook-delivery retries, and — on the 00:00 UTC tick — log cleanup. It is also directly invocable over HTTP for manual runs.
Route and methods
/.netlify/functions/system-cron. Normally reached internally via trigger-scheduler (Netlify Clockwork, * * * * * in netlify.toml), which calls this handler either with a fabricated SYSTEM_PRINCIPAL context (scheduled ticks) or through the normal middleware path (manual calls). Any HTTP method reaches the handler — there is no method dispatch.
Authentication
Inside the handler, after createHandler has resolved a non-anonymous principal:
?api_key=or?scheduler_key=equal to theSCHEDULER_API_KEYenvironment variable, or- a machine principal with
machineType === 'internal'— which is what a validx-internal-secretheader resolves to (SYSTEM_PRINCIPAL).
Anything else returns 403 and emits a system_cron.unauthorized_access audit event. When SCHEDULER_API_KEY is unset the key comparison can never match, so only internal machine principals pass — fail closed.
Request contract
- Query:
api_keyorscheduler_key(scheduler key auth path only — still requires a resolved non-anonymous principal, e.g. anyx-api-key). - Headers:
x-internal-secret(internal machine principal path). - No body is read.
Prefer the header path for manual local calls: netlify dev logs request URLs verbatim, so ?api_key=<secret> lands plaintext in dev log files — headers do not.
What runs per invocation
- Stale-execution sweep —
sweepStaleExecutions()markspipeline_executionsrows inrunning/pending/queueduntouched for over 10 minutes asfailedwith astale executionerror message. Runs on every invocation, before anything else. - Due schedules —
get_due_schedulesRPC returns duepublic.schedulesrows. For each:validate_schedule_creatorRPC → load thepublic.actionsrow → load the schedule's machine principal fromapi_keys→ verify scope (action.required_scopes[0]or<handler>:execute, satisfied also by*:*) →executeAction→ insert aschedule_executionsrow →update_schedule_after_runRPC →schedule.executeaudit. - Due timers —
get_due_timersRPC; each due timer runsrunPipeline(timer.pipeline_id, …)under a fabricated machine principaltimer:<timer_id>with scopepipelines:execute, thenupdate_timer_after_runreschedules it. Timers withoutpipeline_idfail that item only. - Log cleanup — only when the tick lands on UTC 00:00:
cleanup_old_logsRPC withp_retention_days: 30. - Delivery retries —
retryPendingDeliveries(ctx)sweeps due outbound webhook deliveries.
executeAction routes action.handler_module:
functions—send_email,generate_report,notify_watchersare console-log stubs returning success-shaped objects;run_pipelineinvokesrunPipelinefor real.integrations— stub returning{ executed: true, handler }.custom— throwsCustom handlers not yet implemented.- anything else — throws
Unknown handler module.
Response contract
- 200 —
{ message, schedules_due, executed, success, failed, skipped, logs_cleaned, deliveries_retried, results }whereresults[]carries{ scheduleId, actionId, status: success|failed|skipped, error?, durationMs }per schedule/timer item. An emptyget_due_schedulesresult does not short-circuit the tick — timers, retries, and cleanup still run. - 403 — invalid scheduler authentication.
- 500 — top-level failure, with a
system_cron.erroraudit event.
Side effects
Writes pipeline_executions (via runPipeline and the sweep), schedule_executions, schedule/timer bookkeeping via RPC, logs audit events (system_cron.unauthorized_access, system_cron.error, schedule.execute), deletes old logs rows at 00:00 UTC, and mutates webhook_deliveries through the retry sweep.
Errors
Per-item failures are contained: a failing schedule/timer records a failed result and a schedule_executions/update_*_after_run failure record rather than aborting the tick. A failure in the outer block — e.g. get_due_schedules itself erroring — short-circuits to 500 with a system_cron.error audit. Timer-loop, cleanup, and sweep failures are caught and logged without failing the response.
Agent notes
send_email,generate_report,notify_watchers, and theintegrationsmodule are stubs — a schedule pointing at them "succeeds" while doing nothing but a console log. Onlyrun_pipelineperforms real work today.SCHEDULER_API_KEYunset does not open the endpoint: the key path simply never matches and only internal machine principals pass.