Skip to content
Jobs: dispatch, poll, stream The async job lifecycle behind every on-box change — jobs_create / POST /v1/servers/{id}/jobs, waiting for the result in one call (wait), idempotency keys, statuses, polling cadence, the 24-hour offline queue, server_offline, reading job output (jobs_output or the SSE stream) and safe-retry rules.

Jobs: dispatch, poll, stream

Every change on a server, and many live reads, runs as a job: a named, whitelisted action (site.create, stack.deploy, ssl.issue, …) that the control plane sends to the server’s agent over its WebSocket. The agent runs it in its native engine and reports back. You never send a shell command. You pick a job kind, and the control plane builds the exact argv from your fields.

MCPREST
Body schema of a kindjobs_schema {type: "site.create"}GET /v1/api/job-kinds/site.create
Dispatchjobs_create {id: <server UUID>, body: {type: "site.create", …fields}}POST /v1/servers/{id}/jobs with the body
Statusjobs_get {id: <job_id>}GET /v1/jobs/{job_id}
Recent jobs on a serverjobs_list {id: <server UUID>}GET /v1/servers/{id}/jobs?limit=10
Output (persisted lines)jobs_output {id: <job_id>, query: {tail: "200"}}GET /v1/jobs/{job_id}/output?tail=200
Live outputnot availableGET /v1/jobs/{job_id}/stream (server-sent events)

The body is flat: type plus that kind’s fields at the top level. Unknown keys are rejected with 400 bad_request, along with a did-you-mean hint and accepted_keys.

A successful dispatch answers 202:

{ "job_id": "8b1d…", "status": "running", "stream_url": "/v1/jobs/8b1d…/stream" }

Before the job is accepted, the control plane checks the token (level, scope and server allowlist for this kind), the owner’s team role, and the plan (tier_required). It also validates every field. Most errors therefore arrive synchronously on dispatch, before anything reaches the box.

Add wait (1–90 seconds) to hold the answer until the job ends. Over MCP it is an argument of jobs_create. Over REST it is the query parameter ?wait=N:

jobs_create {id: <server UUID>, wait: 60, body: {type: "wp.content", domain: "example.com", method: "GET", path: "/wp/v2/posts"}}
POST /v1/servers/{id}/jobs?wait=60

A job that finishes in time answers 200 with its final state and the tail of its output (the same lines, caps and masking as jobs_output; ?tail= 1–500, default 100):

{ "job_id": "8b1d…", "status": "completed", "exitCode": 0, "errorMessage": null,
"output": { "lines": [ { "stream": "stdout", "seq": 1, "line": "{\"ok\":true}" } ], "truncated": false } }

A failed job with a known cause also carries the failure block. That makes short actions and content reads such as wp.content, php.switch or cron.add a single call instead of dispatch, poll and read output.

  • Still running after N seconds: the usual 202 {job_id, status, stream_url} plus waited: N. The job keeps running; continue with jobs_get as below.
  • Offline server: never waited on. You get the usual 202 with status: "queued" (or 409 server_offline for kinds that are never queued).
  • Cap: 90 s, because the Cloudflare edge ends a request at 100 s. Larger values are treated as 90. Use wait for jobs that usually take seconds, and poll for long ones (provisioning, image builds).
  • Limit: a token holds at most 5 waiting requests at once. The 6th answers 429 wait_limit with Retry-After. A value that isn’t a number answers 400 invalid_wait.
StatusMeaningTerminal?
pendingAccepted, being sent to the agentno
queuedThe agent is offline; the job waits for it to reconnectno
runningThe agent is running itno
completedFinished with exit code 0yes
failedFinished with a non-zero exit code or an error (errorMessage)yes
timeoutRan past its time budget (default 600 s; long kinds such as provisioning get more)yes
cancelledStopped before it finishedyes

jobs_get returns:

{ "job": { "id": "…", "type": "site.create", "status": "completed", "exitCode": 0,
"errorMessage": null, "dispatchedAt": "…", "completedAt": "…", "durationMs": 41250 } }

When a job ends failed or timeout and the end of its log matches a known cause, both jobs_get and jobs_output add a failure block with a stable code, a hint that names the next step (usually a tool), and sometimes a docs link:

"failure": { "code": "apt_locked",
"hint": "Another package operation (usually unattended upgrades) holds the apt/dpkg lock. Wait a few minutes and run the same job again; do not loop. …" }

Codes: apt_locked, acme_rate_limited, dns_not_pointing, acme_unreachable, disk_full, nginx_config_invalid, out_of_memory, image_not_found, registry_auth_failed, registry_rate_limited, port_in_use, restore_snapshot_failed, restore_rolled_back, restore_rollback_failed, cron_not_found, agent_outdated. Follow the hint instead of retrying blindly; a job with no failure block failed for a reason MZPanel does not recognise, so read jobs_output.

It does not return the job’s output; read that with jobs_output (below). To confirm the effect, re-read the state the job changed. Use servers_inventory_refresh {id, kind} or servers_sites_refresh {id}, wait a few seconds, then call servers_inventory {id, kind} / servers_sites {id}. If you need a result inline, prefer a synchronous read: the core tools (ssl_list, db_list, docker_ps, logs_tail, site_get, …) or a cached inventory kind.

Poll jobs_get every 2–5 seconds and stop at a terminal status (or dispatch with wait to skip polling for short jobs, see above). Most jobs finish in seconds. Site creation, stack deploys and image pulls take one to several minutes, and server provisioning can take up to 30 minutes. Every poll counts against the token’s rate limit.

If the server’s agent is disconnected when you dispatch:

  • Durable by default. The job is stored as queued and runs automatically when the agent reconnects, in the order it was created. A queued job that has waited more than 24 hours is failed with errorMessage: "expired_offline" instead of running late.
  • Opt out with ?durable=0. Add it to the dispatch URL (over MCP, pass query: {durable: "0"}) when “run it now or not at all” is what you want. The row then ends expired_offline on the next reconnect rather than running hours later.
  • Never queued: kinds that carry secrets or environment values, such as env writes or app.db-attach, which prints a new password. They answer 409 server_offline straight away. Retry once the server is online.

Check online in servers_list before dispatching anything that must run now.

jobs_output {id: <job_id>} (REST GET /v1/jobs/{job_id}/output) returns the lines the job has logged so far, oldest first, while it runs and after it ends:

{ "jobId": "8b1d…", "status": "failed", "exitCode": 1,
"lines": [ { "stream": "stderr", "seq": 41, "line": "nginx: [emerg] unknown directive" } ],
"totalLines": 42, "truncated": true }
  • tail (query, 1–500, default 100) picks how many of the last lines to return.
  • Lines longer than 4 KB are cut, and the whole result is capped at 64 KB, keeping the newest lines. truncated: true means some lines were cut or left out; compare totalLines with lines.
  • Same visibility as jobs_get: a job you cannot see answers 404 not_found.
  • Kinds that print a credential (app.db-attach, db.user-create, db.user-passwd, pg.db-create, mongo.db-create, site.wp-admin-passwd, pgadmin.creds, db.pma) never store their output, so their lines are always empty. Where the kind takes a pass field, set your own password when you need to know it; over REST you can also read the result from the live stream.
  • Secrets that tools print in plain text (private keys, passwords in URLs, Bearer tokens, PASSWORD=…-style pairs, cloud and git tokens) are stored as [REDACTED]. The live stream shows them unmasked while the job runs.
  • The generated passwords in the result of site.create (database and wp-admin) and site.clone (database) are stored as [REDACTED] as well. Read them from the live stream, or set a new wp-admin password afterwards with site.wp-admin-passwd.
  • Output is kept for 30 days. For an older job, jobs_get still answers but lines is empty.

Read it when a job ends failed or timeout: the last stderr lines usually name the cause.

Terminal window
curl -N -s https://api.mzpanel.com/v1/jobs/$JOB/stream -H "Authorization: Bearer $TOKEN"

The stream first replays the lines already logged, then sends live frames:

event: log
data: {"stream":"stdout","seq":12,"line":"Issuing certificate for example.com"}
event: done
data: {"exit_code":0,"duration_ms":18240}

done may carry "error". Kinds that print a credential stream their output to the caller but never store it, so a later replay shows nothing for them. MCP has no stream tool: poll jobs_get, then read jobs_output.

There is no generic cancel, and without an idempotency key each jobs_create is a new job.

Idempotency keys. Send an Idempotency-Key header (1–128 printable characters; over MCP the idempotencyKey argument of jobs_create) with any API-token POST/PUT/PATCH/DELETE. When you repeat the request with the same key, method, path, query and body within 24 hours, you get the first 2xx answer back, with the header Idempotent-Replayed: true, and nothing is dispatched again. Use a fresh key (a UUID) for each new action, and reuse it only for retries of that action.

  • 422 idempotency_key_reused: the key was already used for a different request.
  • 409 idempotency_in_progress: the first request with this key is still running (for example a wait that has not returned yet). Retry with the same key after Retry-After.
  • Only 2xx answers are kept. A 4xx or 5xx frees the key, so you can fix the request and send it again with the same key.
  • An answer that carries a secret (for example a new token or password) is not stored. The replay is then {"replayed": true, "body_withheld": true, "job_id": "…"}; read the job’s state with jobs_get.
  1. Never re-dispatch a mutation blindly. After a timeout, a network error or a queued answer, call jobs_get (or jobs_list {id}) first, or repeat the call with the same idempotency key. A queued job will run when the agent returns. Sending it again without the key would run it twice.
  2. Retry only after a terminal failed/timeout, and only once you’ve fixed the cause (errorMessage, jobs_output, logs, state).
  3. Read jobs are cheap to repeat. Mutations are not. site.create on an existing domain fails. db.create twice fails. stack.deploy twice redeploys.
  4. Exit code 127 means the agent on that box has no native handler for the kind, which happens with an old agent. Update it with agent_update {id} (operate), wait for it to reconnect, then retry.

jobs_list hides high-volume read kinds (inventory polls, server.*, metrics.*), so the list shows real actions.

Backup runs, snapshot verification and restores use dedicated operations (backup_jobs_run, backups_run_once, backup_snapshots_restore, …), not jobs_create. See Backups and restore.