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
Code
TypeScript
Code
Python
Code
| Situation | Answer |
|---|---|
| No live unit has this key | 201 {created: true} and the unit starts (unless "start": false) |
| A live unit has this key and is running, input equal or omitted | 200 {created: false} with the current unit |
It is stopped and start is true | It 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_data | 409 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
Code
TypeScript
Code
Python
Code
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
Code
TypeScript
Code
Python
Code
- stop drains the unit (it receives a draining notice and stops accepting new work), then stops it. Key, labels and
user_dataare kept, the slot is released and nothing is billed while it is stopped. Stopping a stopped unit is a no-op200. - start wakes a stopped unit. Starting a running unit is a no-op
200. The body may carrylabelsanduser_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
404and may be reused. Success is204.
curl
Code
TypeScript
Code
Python
Code
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:
Code
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:
Code
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:
Code
start_within_secondsis 1 to 604800 (seven days) and only works withstart: false. Anything else is400 invalid-start-within.- A unit not started by then is destroyed:
unit.destroyedwith reasonstart_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_beforesays when the platform destroys it unless it is started first: the earlier of its own deadline anddestroy_stopped_after_daysafter it stopped. It isnullwhile the unit runs, and outsidecodemode. - There is no default deadline. Without
start_within_seconds, a stopped unit waits untildestroy_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.
