The worker¶
run_spec(run_id) runs one row: it claims it, calls the spec through the kernel's dispatch, and
records how the call ended, in one transaction when the spec says atomic=True. Progress under
atomic=True is invisible until that transaction commits, unless the binding names a
progress_using alias: a report written on the connection holding the transaction cannot be read
from any other, which is the point of the transaction and the cost of writing to the row.
Both backends call this function with one string and read nothing back. It takes no retries and returns no result: whether a run may run again is read from the row.
The steps¶
- Read. A row that is not
PENDINGends the call here. A redelivered message costs one query and no run, whatever the spec declares. - Find. The binding by name and the spec in its registry. Either missing ends the row
SPEC_UNAVAILABLE, fromPENDING, withattemptsunchanged, and{"message": ...}inrefusal_detailnaming what is missing. - Claim. One conditional
UPDATEtoRUNNING, committed alone before anything runs. Losing it ends the call. Two copies of a message cannot both win, and a copy cannot run a row another attempt finished. - Resolve.
actor_pknull means the principal isNone, neverAnonymousUser, and a spec gated on a principal refuses it. Otherwisebinding.resolve_actor(actor_pk). - Run.
dispatch, thenpresent, insidearound_run. Everything enqueue checked is checked again, and the Validator runs for the first time. - Record. A write conditioned on
(pk, state, attempts), thenon_finish, in one transaction.
The call is a task call: call_transport(call_context) reads "task", and whatever carry recorded at
enqueue is at call_context["carried"], where a PoolSeeds resolver can read it.
The transaction¶
| Spec | What wraps what |
|---|---|
ServiceSpec with atomic=True |
one transaction around around_run, dispatch, present, the terminal write and on_finish |
ServiceSpec with atomic=False |
no outer transaction; the terminal write and on_finish take their own |
SelectorSpec |
nothing to roll back, so as atomic=False |
So under atomic=True the service's work and the run's state are one fact. A worker dying before the
commit leaves no row of the service's, and a RUNNING run the sweep answers.
A refusal or an exception rolls that transaction back first, then writes its state and runs on_finish
in a transaction of its own, so the state and what it fires still commit together.
The window that remains. An atomic=False spec's committed work and its terminal write are two
facts. A worker dying between them leaves a RUNNING row the sweep ends ABANDONED, with the work done.
That is what atomic=False means, and it is the right choice for a call one transaction should not span,
such as an agent run.
A lost claim commits nothing. Past its lease a run may be handed to another worker or ended by the
sweep. The terminal write then matches no row: it logs a warning and writes nothing, nothing fires, and
under atomic=True the service's work rolls back with it. Under atomic=False the work stands. So a
binding's lease is sized to its longest run.
How a run ends¶
| Raised | State | Why |
|---|---|---|
NotPermitted, PermissionCheckDenied |
AUTHORIZATION_LAPSED |
authorized at enqueue, refused now: access changed, the code did not break |
PrincipalUnavailable |
AUTHORIZATION_LAPSED |
deleted, malformed or deactivated in the window |
InvalidArguments |
INVALID |
the Validator runs only here |
DispatchError, and any later subclass |
INVALID |
a refusal of the call |
ServiceValidationError |
INVALID |
the service's word for bad input |
ServiceNotFound |
REFUSED |
the target vanished |
ActionUnavailable, ServiceConflict |
CONFLICT |
terminal; ActionUnavailable leaves its code |
AdditionalInputRequired |
INPUT_REQUIRED |
nobody to ask; the remedy is a new enqueue carrying the answer |
ServiceError, and any later subclass |
REFUSED |
the operation refused on its own terms |
run_state_for(error) is this table. Anything else is FAILED, and FAILED is never the answer to a
refusal.
Every row of the table records:
| Column | Holds |
|---|---|
refusal_class |
the class's name, a project's own subclass included |
refusal_code |
its code, where it has one |
refusal_detail |
{"message": ..., "errors": ..., "schema": ...} |
errors is the field errors of an InvalidArguments or a ServiceValidationError, and schema is what
an AdditionalInputRequired asks for; each is null otherwise. A refusal carrying a detail no JSON column
could store keeps its state and its message, with both null.
A FAILED run has traceback and leaves the refusal columns null: a reader is shown refusal_detail and
never traceback, so a fault's words stay where only an operator looks.
The result¶
SUCCEEDED records kind, and output, which is present(spec, result): what a JSON transport would
have sent, stamped with package_version since it is a contract. instance_pk keeps the target's identity, as text with no length bound, so a key of any length is stored whole; actor_pk, binding, spec_name, refusal_class and refusal_code are the same.
kind is "instance", "list" or "not_found". A selector that finds nothing has answered, so it ends
SUCCEEDED with kind="not_found" and no output. A service whose target is gone never ran, so it ends
REFUSED, with kind="not_found" and refusal_class="ServiceNotFound", as a ServiceNotFound the service
raises itself does: a refund of a deleted order must not read as a refund that happened.
The bound. Past max_output_bytes, measured as the column writes it, the run ends OUTPUT_TOO_LARGE
with {"size": ..., "limit": ...} in refusal_detail, and output stays null. Under atomic=True the work
rolls back with it, so the caller reads a clean refusal. Under atomic=False the work stands, and the state
says the result was not stored. Output is never truncated.
The limit is the binding's max_output_bytes, which defaults to 1 MiB.
A file result¶
A spec whose presenter is a FilePresenter (its own, or its output selector's) presents a FileResult, and
the worker saves its bytes with default_storage under django_spec_tasks/<run id>/<filename>. The output is
{"file": {"key": "django_spec_tasks/<run id>/report.pdf", "filename": "report.pdf", "content_type": "application/pdf", "size": 10249}}
where key is the name storage returned, which it may have changed to keep it unique: open the file with
default_storage.open(output["file"]["key"]). max_output_bytes bounds that JSON, never the file. Content
that is a file object is handed to storage as it is and closed after, and its size is what storage reports;
bytes or an iterable of chunks is read whole first. The save runs inside around_run, before a file
parameter's file is closed, so a result reading its own input saves; a slow or failing storage write is
therefore held inside around_run, and inside the run's transaction under atomic=True, where a failure
ends the run FAILED and rolls the work back. A selector finding nothing saves nothing and ends
kind="not_found".
The file is saved before the terminal write, so a run that then loses its claim, or rolls back under
atomic=True, leaves a saved file no row names. Nothing here deletes a saved file:
retention is the project's.
Files a run opened¶
A file parameter's key is opened from default_storage before dispatch, inside around_run, and
closed once the call, present and the saving of a file result have returned or raised. A key storage no
longer holds ends the run FAILED.
Output a JSON column cannot hold, which is a presenter's bug, ends FAILED, as any exception nobody declared
does. That counts NaN, inf, a NUL character and half a surrogate pair, which Postgres refuses and SQLite
mostly stores. A refusal keeps its state however it reads: a message, a traceback and a code lose what no text
column holds, and a detail no column could hold is left out.
Progress¶
A service declaring progress gets a reporter that writes to the row, for as long as this attempt holds it.
With a total, the progress column holds the fraction done; without one, the service's own unit.
message lands in progress_message, and meta is dropped.
| Spec | progress_using |
Read from another connection |
|---|---|---|
atomic=False, or a selector |
unset | at once |
atomic=True |
unset | only after the run commits, and never if it rolls back |
atomic=True |
an alias | at once, on a database that lets a second connection write |
A report that cannot be written is logged and dropped: progress never ends a run. SQLite has one writer, so
under atomic=True an alias's write waits for the service's and fails once the service has written anything.
Progress increases within one dispatch. A claim clears both columns, so an idempotent spec run again starts from nothing rather than from what the attempt before it reported.
The seams¶
around_run(run, principal) is entered before dispatch and left after present, and is handed what the
call raises. It may not swallow it: with no result to record, a run whose seam swallowed its exception ends
FAILED rather than SUCCEEDED with nothing to show.
on_finish(run) runs inside the terminal transaction and reads the row as a reader would. Raising from it is
a bug in the project: the terminal write rolls back with it, and the exception reaches the worker. The row
stays RUNNING, and the sweep's own ending calls the same hook, so a hook that always raises keeps the row
RUNNING and counted in the sweep's errors until it stops raising.
Exceptions the worker does not catch¶
A BaseException is the worker dying: it passes through untouched, rolling back whatever transaction is
open, and leaves the row RUNNING. So does anything raised while recording the ending, since nothing could
record that either.