Long-running operations
Some work cannot finish inside a request: provisioning a site builds a database, buckets,
routing and seeded content; a search reindex walks everything the site has. Rather than
answer 202 and leave you guessing, those calls name an Operation — a resource you
can poll, and the only honest answer to "is it done yet".
Code
Three habits make polling one of these correct:
donemeans finished, not succeeded. Exactly one ofresponseanderroris present once it flips, and the error is the ordinary Status shape.- Read
metadata.step, not a percentage. The step is the named hop the work is on; where nothing counts anything, no number is reported rather than a made-up one. - Cancelling is a request, not an event.
:cancelrecords it and returns the operation still running; the work stops at its next checkpoint, if it has one left, and you learn the outcome fromdone— aCANCELLEDerror means it stopped, success means it finished anyway.
17. Long-running operations (AIP-151)
Anything that returns before the work is done MUST return an Operation, not
a bare 202 and a hope.
Code
When done is true, exactly one of response (the resulting resource) or
error (a Status body, §13) is present.
GET {operation_name}polls.POST {operation_name}:cancelrequests cancellation; the operation is not cancelled untildoneistruewith aCANCELLEDerror.- The initiating call returns
202 Acceptedwith the Operation and aLocationheader pointing at it. - Operations are retained at least 24 h after completion.
An Operation is server-minted. It has Get and :cancel and nothing else: a
client cannot create one, and a List of every operation a resource has ever run
is an audit log, which is a different resource. It carries no etag either —
§14 wants one where there are concurrent editors, and this resource has no
editors: :cancel raises a flag, it does not submit a version.
Cancellation is a request, honoured at a checkpoint. :cancel records it
and returns the operation, still running. A worker consults the flag where it
can still stop; past its last checkpoint the work finishes and the operation
reports success — which the done/CANCELLED rule above makes visible rather
than letting the caller assume the cancel took. :cancel on an already finished
operation is FAILED_PRECONDITION: finished work cannot be un-finished.
metadata is service-specific and honest. Emit step — the named hop the
work is on — rather than a progress_percent nothing measures. progress_percent
belongs in the example above and in a service that genuinely counts something.
A kick-off that also creates an addressable resource keeps its 201. §17
covers calls whose whole result is asynchronous. Where the resource exists by the
time the response is written — a website whose provisioning_status is PENDING,
a user whose site is still being built — the answer stays 201 + Location +
the resource, plus an operation field naming what to poll. Downgrading that to
202 would take the resource's name away from the caller and hand back only a
receipt for work.
And there is no operation field when the resource is what you poll. A
resource created to be one run of work — ai_executions is the platform's one
— already carries the whole lifecycle an Operation would: a state that ends,
an error when it ends badly, a :cancel that raises the same flag honoured at
the same kind of checkpoint, and, unlike an Operation, a List that is its history.
Minting an Operation beside it would publish two names for one run and two places
to read whether it finished.
Streamed responses (D9)
A resource whose state changes while someone watches it — a run whose result is written a piece at a time — MAY offer a stream of those changes. Polling stays correct; the stream only makes it immediate.
Code
- The method. A custom method on the resource (§10),
GETbecause it is a read. It is answered200withContent-Type: text/event-streamandCache-Control: no-cache, no-transform, in the server-sent events format. The sameAuthorizationas every other call; a client reads it withfetch, since a browser'sEventSourcecannot send that header. - An event is an
event:line with its name and onedata:line with a JSON object, followed by a blank line. The object issnake_caseand follows §11. Noid:line is sent: the position is the object's own field. - Names are lower-case
snake_casewords joined by dots —text.delta,step.completed,turn.completed. Two are the same on every stream:snapshotanderror. The others, and what each carries, are documented with the resource that streams them; a client ignores a name it does not know, as it tolerates an unknown enum value (§11). sequence. Every event'sdatacarriessequence, an integer that grows with every change of the resource and never repeats. The resource carries the same field: thesequenceof the last change it holds.snapshotcomes first, on every connection — the resource whole, as a Get would answer it, under the field named after the resource (turn), beside thesequenceof the last change it holds. A client replaces what it has with it, so it never has to have kept anything; the events that follow carry greater sequences, and a client applies each event once, in order. What changed before the connection is in the snapshot and is not sent again as an event: work that starts at once — an assistant turn the Create has just queued — has usually begun its first step before a client that opens the stream right after the Create connects, and that client finds the step in the snapshot.- Resuming.
after_sequence(OPTIONAL, an integer ≥ 0) is the lastsequencethe client applied. After the snapshot, no event at or below it is sent; a negative value isINVALID_ARGUMENT. A connection that drops before the last event is reopened with it. - A refusal before the first byte — an unknown or invisible resource, a missing
right, a malformed
after_sequence— is an ordinaryStatusbody with its HTTP code (§13), not a stream. - A failure after the first byte is an
errorevent whosedatais{"sequence": …, "error": {…}}— theStatusof §13, the same object a non-2xx answer's body holds. It is the last event of the stream. - Keep-alive. While nothing changes the server writes a comment line —
: keep-alive— every 15 seconds; a client ignores it. - Closing. The server closes the stream after the resource's terminal event —
the one its documentation names, or
error— and right after thesnapshotwhen that already shows the resource finished. A stream that ends any other way was interrupted and is reopened withafter_sequence. - Closing it changes nothing. A client that disconnects stops reading, not
the work; stopping the work is the resource's
:cancel. - A stream says nothing the resource does not: what a client missed, a Get of the resource reads afterwards.