Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Scheduler API

Canonical reference: docs/api-reference.md (generated endpoint directory) — this page is a task-oriented summary.

Manage scheduled jobs for automated task execution. Supports cron, every (recurring interval), and at (one-time timestamp) schedules, plus natural-language scheduling and missed-job catch-up on restart.

The base path is singular: /api/v1/schedule (not /schedules). All scheduler endpoints share this prefix. Job creation is POST /api/v1/schedule/spawn (not POST /api/v1/schedule).

Overview

The scheduler runs as a gen_server with an ETS-backed job store. Jobs are attributed to the creating user (user_uuid) and dispatch tasks to workers at the scheduled time. Most mutating endpoints require admin (the "*" permission).

Schedule TypeFieldDescriptionUse Case
croncron5-field cron expressionPeriodic tasks on a calendar schedule
everyeveryInterval in milliseconds (must be > 0)Recurring tasks at fixed intervals
atatOne-time timestamp in msDelayed single execution

Endpoints

List Scheduled Jobs

GET /api/v1/schedule

Returns all scheduled jobs. Admin-only — non-admin callers receive an empty paginated list (not an error). Paginated via the standard envelope.

Response (200):

{
  "items": [
    {
      "id": 1,
      "name": "Daily health check",
      "schedule_type": "cron",
      "cron_expr": "0 9 * * *",
      "interval_ms": null,
      "at_ms": null,
      "next_run_ms": 1716192000000,
      "last_run_ms": 1716105600000,
      "status": "active",
      "prompt": "Check system health and report any issues",
      "model": "glm-5.1",
      "opts": {},
      "delete_after_run": false,
      "consecutive_errors": 0,
      "enabled": true,
      "currently_running": false,
      "tz": "UTC"
    }
  ],
  "total": 1,
  "limit": 50,
  "offset": 0,
  "page": 1,
  "total_pages": 1,
  "has_more": false
}

Create a Scheduled Job

POST /api/v1/schedule/spawn

Create a new scheduled job. Admin-only (enforced by the {admin, require} middleware scope, ).

Cron example:

{
  "name": "Daily health check",
  "prompt": "Check system health and report any issues",
  "cron": "0 9 * * *",
  "model": "glm-5.1",
  "opts": {}
}

Every example:

{
  "name": "Monitor queue depth",
  "prompt": "Check ZMQ queue depth and alert if > 100",
  "every": 300000
}

One-time example:

{
  "name": "Deploy notification",
  "prompt": "Announce deployment completion",
  "at": 1716192000000,
  "delete_after_run": true
}

Natural-language example (resolved server-side via sol_nl_schedule):

{
  "name": "Weekly report",
  "prompt": "Generate weekly system report",
  "schedule": "every monday at 9am"
}

Request Fields

FieldRequiredDescription
promptYesTask prompt to send when the job fires. Empty → 400.
nameNoHuman-readable job name.
cronOne of cron/every/at (or schedule)5-field cron expression.
everyOne of cron/every/at (or schedule)Interval in ms (positive integer).
atOne of cron/every/at (or schedule)Unix timestamp in ms (integer). Past timestamps are accepted but logged.
scheduleAlternative to cron/every/atNatural-language string parsed by sol_nl_schedule.
modelNoModel to use for the task.
optsNoAdditional task options. Identity-bearing keys (user_uuid, permissions, listener, owner, task_id, container_id, plugin) are stripped from user input.
delete_after_runNoAuto-delete after execution. at jobs only — rejected for cron/every.

Response (200)

{
  "status": "ok",
  "id": 42,
  "next_run": 1716192000000
}

Error Responses

All error bodies speak the Zerg envelope — {"ok":false,"error":{"code":"…","message":"…"}} — where the table shows <code> inside error.code.

CodeBody (error.code)Condition
400missing promptEmpty prompt.
400must specify at, every, or cronNone of the schedule keys provided.
400at must be a positive integer (ms timestamp)at not an integer.
400every must be a positive integer (ms)every not a positive integer.
400invalid cron expressioncron failed sol_cron:parse/1.
400delete_after_run only supported for 'at' jobsdelete_after_run: true on non-at schedule.
400<create error>sol_scheduler:create_job/1 returned an error.
403insufficient_permissionsNon-admin caller (middleware, {admin, require} scope).
429internal_error (see note)Global cap max_scheduled_jobs (1000) or per-user quota max_scheduled_jobs_per_user (100) reached.

429 note: the quota arms correctly answer 429 (capacity is never a 500), but the handler currently routes the quota reason through the generic error mapper, so the body carries the internal_error code instead of a dedicated quota code. Dispatch on the status, not the code, on these arms. (Flagged for canonical-side repair.)

Get Job Detail

GET /api/v1/schedule/:id

Returns details for a single scheduled job. Admin-only — the job’s prompt and opts are not disclosed to non-admin callers (403).

Response (200):

{
  "id": 1,
  "name": "Daily health check",
  "schedule_type": "cron",
  "cron_expr": "0 9 * * *",
  "interval_ms": null,
  "at_ms": null,
  "next_run_ms": 1716192000000,
  "last_run_ms": 1716105600000,
  "status": "active",
  "prompt": "Check system health and report any issues",
  "model": "glm-5.1",
  "opts": {},
  "delete_after_run": false,
  "consecutive_errors": 0,
  "enabled": true,
  "currently_running": false,
  "tz": "UTC",
  "anchor_ms": 1716000000000
}

anchor_ms is included only when set (used to anchor drift-corrected intervals). tz is always "UTC" — all cron/at/every scheduling is naive UTC. All time fields use the _ms suffix (milliseconds).

Error Responses:

CodeBody (error.code)Condition
400invalid id:id not parseable as an integer.
403forbiddenNon-admin caller.
404job not foundUnknown id.

Delete a Scheduled Job

DELETE /api/v1/schedule/:id

Cancel and remove a scheduled job. Admin-only.

Response (200):

{ "status": "deleted" }

Error Responses:

CodeBody (error.code)Condition
400invalid id:id not an integer.
403forbiddenNon-admin.
404job not foundUnknown id.
409job currently runningJob is mid-execution.

Pause a Job

POST /api/v1/schedule/:id/pause

Pause a scheduled job without deleting it. The job will not fire until /unpause is called. Admin-only.

Response (200):

{ "status": "paused" }

Error Responses:

CodeBody (error.code)Condition
400invalid id:id not an integer.
403forbiddenNon-admin.
404job not foundUnknown id.

Unpause a Job

POST /api/v1/schedule/:id/unpause

Resume a paused job. Admin-only.

Response (200):

{ "status": "active" }

Error Responses: same shape as pause (400 / 403 / 404), plus one extra 400 arm: an unreachable schedule (no reachable fire time, e.g. an impossible cron that survived an older create) is rejected at unpause time and the job stays disabled.

Trigger a Job Immediately

POST /api/v1/schedule/:id/run

Trigger immediate execution of a scheduled job, regardless of its schedule. Admin-only.

Response (200):

{ "status": "dispatched" }

Error Responses:

CodeBody (error.code)Condition
400invalid id:id not an integer.
400job is disabled, unpause firstJob is currently paused.
403forbiddenNon-admin.
404job not foundUnknown id.
409job already runningJob is mid-execution.

Job Object Shape

Every job endpoint that returns a single job or a list of jobs uses the same shape produced by sol_http_json:schedule_job_to_json/1:

FieldTypeDescription
idintegerJob id.
namestringHuman-readable name.
schedule_typestringcron | every | at (atom stringified).
cron_exprstring|nullCron expression (only for cron).
interval_msinteger|nullInterval (only for every).
at_msinteger|nullTimestamp (only for at).
next_run_msinteger|nullNext scheduled run, ms.
last_run_msinteger|nullLast run, ms.
statusstringactive | paused (atom stringified).
promptstringThe prompt dispatched at fire time.
modelstring|nullModel override.
optsobjectTask options.
delete_after_runbooleanAuto-delete after run (at only).
consecutive_errorsintegerError streak (auto-pause at 10).
enabledbooleanWhether the job is enabled.
currently_runningbooleanWhether the job is mid-execution.
tzstringAlways "UTC" — scheduling is naive UTC.
anchor_msinteger|omittedDrift anchor (only when set).

Missed-Job Detection

The scheduler detects jobs that missed their scheduled time during downtime. On startup:

  1. All stored jobs are loaded from ETS.
  2. Jobs with next_run_ms in the past are identified.
  3. Up to 5 missed jobs are dispatched with a 5-second stagger.
  4. Jobs with 10+ consecutive errors are automatically paused.

This ensures tasks are not permanently lost after server restarts.

Limits

ParameterDefaultDescription
max_scheduled_jobs1000Global maximum active jobs (429 on exceed)
max_scheduled_jobs_per_user100Per-user quota of active jobs
max_consecutive_errors10Auto-pause threshold
max_catchup_runs5Missed jobs dispatched on startup
catchup_stagger_ms5000Delay between catch-up dispatches
timer_clamp_ms60000Maximum timer precision
max_dispatch_per_tick5Jobs dispatched per scheduler tick

Cron Expression Format

5-field cron expressions:

┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, 0=Sunday)
│ │ │ │ │
* * * * *
ExpressionMeaning
0 9 * * *Every day at 09:00
*/5 * * * *Every 5 minutes
0 */2 * * *Every 2 hours
0 9 * * 1Every Monday at 09:00
0 0 1 * *First of every month at midnight

User Attribution

Each job records the user_uuid of the creating user (captured at creation time from sol_identity:get_uuid/0). When the job fires, the task is dispatched with this user_uuid, ensuring proper ownership of results. Permissions are also captured at creation time and replayed at fire time.