Zaroz
Units API guide

Create by key, start, stop and destroy

A unit made by your code has a key: its name, chosen by you, that never changes. A hub that opens a private world per player uses plot-<player>. A job runner uses job-<id>. The key is also the unit's private network name: <key>.<workload-slug>.

These routes need the policy in code mode.

Create a unit by key

POST .../units is idempotent on the key, which makes it safe to call on every "player opens a plot".

curl

TerminalCode
curl -sS -X POST "$UNITS" -H "$AUTH" -H "Content-Type: application/json" -d '{ "key": "plot-ana", "labels": {"kind": "plot", "owner": "ana"}, "user_data": {"world": "s3://my-bucket/plots/ana"}, "start": true, "on_exit": "stop" }' # 201 {"created": true, "unit": {...}} a new unit # 200 {"created": false, "unit": {...}} the live unit with that key already existed

TypeScript

TypeScriptCode
import { call, UNITS } from "./zaroz"; const { status, body } = await call("POST", UNITS, { key: "plot-ana", labels: { kind: "plot", owner: "ana" }, user_data: { world: "s3://my-bucket/plots/ana" }, start: true, on_exit: "stop", }); console.log(status, body.created, body.unit.key, body.unit.state);

Python

Code
from zaroz import call, UNITS status, body = call("POST", UNITS, { "key": "plot-ana", "labels": {"kind": "plot", "owner": "ana"}, "user_data": {"world": "s3://my-bucket/plots/ana"}, "start": True, "on_exit": "stop", }) print(status, body["created"], body["unit"]["key"], body["unit"]["state"])
SituationAnswer
No live unit has this key201 {created: true} and the unit starts (unless "start": false)
A live unit has this key and is running, input equal or omitted200 {created: false} with the current unit
It is stopped and start is trueIt starts. labels and user_data, when you send them, replace the stored ones. 200 {created: false}
It is running and you send different labels or user_data409 unit-input-differs. A running unit's input changes only by allocate or PATCH labels

Fields: key (required), labels, user_data, start (default true), on_exit (restart, stop or destroy; default the policy's default_on_exit), resources (the unit's own size, see below).

"start": false creates the unit stopped: it holds no slot and costs nothing until you start it.

on_exit is what happens when the unit's process exits: restart keeps the pod and restarts the process in place, stop and destroy end the unit (and release the slot). Use destroy for a match or a job, stop for a plot you will come back to.

resources is {"cpu": 0.5, "memory_mib": 512}: vCPU from 0.25 in steps of 0.25, memory from 256 MiB in steps of 128, never above the plan or the policy's max_unit_resources. Leave it out for the largest size allowed. The unit keeps the size for life; creating the same live key with another size is 409 unit-size-differs. The rules and the billing are on Limits and Billing.

Read a unit

curl

TerminalCode
curl -sS "$UNITS/plot-ana" -H "$AUTH"

TypeScript

TypeScriptCode
const { body: unit } = await call("GET", `${UNITS}/plot-ana`); console.log(unit.state, unit.labels, unit.user_data, unit.allocation);

Python

Code
_, unit = call("GET", f"{UNITS}/plot-ana") print(unit["state"], unit["labels"], unit["user_data"], unit["allocation"])

The response carries state, desired_state, labels, user_data, origin (policy, code or warm), allocation (null until allocated), private_name, public_endpoints, billing_class (included or extra), resources (cpu in vCPU and memory_mib), own_size (true when the size is the unit's own, false when it is the plan's), the current pod and the restart count. Once a unit has ended it also carries termination, which says who ended it and why: see How a unit ended. It is null while the unit has not ended, and after the ending event is no longer kept (30 days).

Stop, start and destroy

curl

TerminalCode
curl -sS -X POST "$UNITS/plot-ana/stop" -H "$AUTH" # drain, then stop: costs nothing while stopped curl -sS -X POST "$UNITS/plot-ana/start" -H "$AUTH" -H "Content-Type: application/json" -d '{}' curl -sS -X DELETE "$UNITS/plot-ana" -H "$AUTH" # drain, then destroy: final

TypeScript

TypeScriptCode
await call("POST", `${UNITS}/plot-ana/stop`); await call("POST", `${UNITS}/plot-ana/start`, {}); // {} starts it as it is await call("DELETE", `${UNITS}/plot-ana`); // final; the key may be reused afterwards

Python

Code
call("POST", f"{UNITS}/plot-ana/stop") call("POST", f"{UNITS}/plot-ana/start", {}) # {} starts it as it is call("DELETE", f"{UNITS}/plot-ana") # final; the key may be reused afterwards
  • stop drains the unit (it receives a draining notice and stops accepting new work), then stops it. Key, labels and user_data are kept, the slot is released and nothing is billed while it is stopped. Stopping a stopped unit is a no-op 200.
  • start wakes a stopped unit. Starting a running unit is a no-op 200. The body may carry labels and user_data, each replacing the stored value whole. Send {} to start it as it is.
  • destroy drains and then removes the unit. It is final: afterwards the key answers 404 and may be reused. Success is 204.

curl

TerminalCode
curl -sS -X POST "$UNITS/plot-ana/start" -H "$AUTH" -H "Content-Type: application/json" \ -d '{"labels": {"kind": "plot", "owner": "ana", "tier": "vip"}, "user_data": {"world": "s3://my-bucket/plots/ana-v2"}}'

TypeScript

TypeScriptCode
await call("POST", `${UNITS}/plot-ana/start`, { labels: { kind: "plot", owner: "ana", tier: "vip" }, user_data: { world: "s3://my-bucket/plots/ana-v2" }, // replaces the stored value whole });

Python

Code
call("POST", f"{UNITS}/plot-ana/start", { "labels": {"kind": "plot", "owner": "ana", "tier": "vip"}, "user_data": {"world": "s3://my-bucket/plots/ana-v2"}, # replaces the stored value whole })

Every call that starts a pod (create with start, start, a refill, allocate with if_none: create) is checked against your limits and spend cap: see Errors for container-unit-limit, org-unit-limit, start-rate and spend-cap. A refusal is never a partial success, and running units are never touched by a limit.

Units that ended

GET .../units?include_gone=true lists the live units and then the newest 200 that ended. To read all of them, use the ended list, a page at a time:

TerminalCode
curl -sS -G "$UNITS/ended" -H "$AUTH" --data-urlencode "limit=50" # {"units": [...], "next_cursor": "..."} newest first; next_cursor is null on the last page curl -sS -G "$UNITS/ended" -H "$AUTH" --data-urlencode "cursor=<next_cursor>"

GET .../units/ended takes limit (1 to 100, default 50), cursor (the next_cursor of the previous page), and the same filters as the list: key_prefix, origin and repeated label=key:value. Each unit has the shape of a list row, with deleted_at (when it ended), kept_until (when its logs age out) and termination. A bad limit or cursor is a 400 (invalid_limit, invalid_cursor). Ended units are listed for 14 days, the time their logs are kept.

GET .../units/summary counts them without listing:

JSONCode
{"running": 12, "ended_24h": 340, "ended_14d": 4810, "ended_by_reason": {"removed": 4700, "memory_limit": 9, "unknown": 101}}

ended_by_reason splits the 14-day figure by reason. Units with no recorded reason count as unknown.

ended and summary are reserved: you cannot use them as unit keys.

Reservations that never start

You can create a unit stopped (start: false) to hold its key before you know it will run, then start it later. If your program crashes in between, that reservation would sit there until destroy_stopped_after_days. Give it a deadline instead:

TerminalCode
curl -sS -X POST "$UNITS" -H "$AUTH" -H "Content-Type: application/json" \ -d '{"key": "match-4812", "start": false, "start_within_seconds": 120}'
  • start_within_seconds is 1 to 604800 (seven days) and only works with start: false. Anything else is 400 invalid-start-within.
  • A unit not started by then is destroyed: unit.destroyed with reason start_deadline. Its key and its place among the stopped units are free again.
  • The first start clears the deadline. A unit that has run is never destroyed by it, however long it stays stopped afterwards (that is destroy_stopped_after_days, below).
  • A stopped unit's start_before says when the platform destroys it unless it is started first: the earlier of its own deadline and destroy_stopped_after_days after it stopped. It is null while the unit runs, and outside code mode.
  • There is no default deadline. Without start_within_seconds, a stopped unit waits until destroy_stopped_after_days, so a unit you wake later by the same key is not lost.

Stopped units pile up

A Workload may hold 1,000 stopped units (stopped-unit-limit). Stopped units that stay stopped for longer than the policy's destroy_stopped_after_days (default 90) are destroyed, and a unit.destroyed event with reason expired tells you. start_before on a stopped unit shows when.

Last modified on