A Captain worker is a compiled Captain program that answers HTTP requests.
It is one .capb artifact with one handler, fetch request:, served by the
runtime captain-worker. On the platform, workers are deployed on Gump, the
cluster scheduler, which places, starts, supervises and replaces the
process like any other workload; Gump itself never reads Captain.
A worker can do only what its profile, captain.worker/1, gives it: read
the request, call allowed HTTPS origins, write log lines, and return a
response. It has no file system, no processes, no shell, no clock beyond
the request's arrival time, no randomness, and no state between requests.
The library
A worker uses the library module Worker::Http, in
lib/worker/http.capt. It defines the
Request, Response and Header structs, the profile's three effects,
and builders called qualified by Http:::
| Procedure | Makes |
|---|---|
response status:headers:body: |
a Response |
header name:value: |
a Header |
text status:body: |
a text/plain response from text |
json status:body: |
an application/json response from text |
header named:in: |
the first request header with that name, as Option[Text] |
bodyText of: |
the request body as text, invalid UTF-8 replaced |
A Request holds the runtime's request id, the method, the path and query
as received, the headers in order with lower-case names, the body as
Octets, and received: the milliseconds since the Unix epoch when the
request arrived. That field is the only time a handler sees, so a handler's
result depends on the request and on its effects' results, and on nothing
else.
A program
module Hello.
use Worker::Http.
export fetch request: Http::Request -> Http::Response [
((request path) = "/hello") ifTrue: [ ret Http::text status: 200 body: "hello" ].
ret Http::text status: 404 body: "not found".
].This is the example from the specification. The repository's packaged
version is in capsules/worker-hello/.
Build it with the library, and the handler's effects are empty:
$ captain build hello.capt -I lib -o hello.capb
built hello.capb ([..])
$ captain inspect hello.capb
...
exports
fetch request: Worker::Http::Request -> Worker::Http::Response (pure)
...The manifest still lists the library's three effects, because the module declares them. What the runtime checks is what the handler can reach, and this one reaches none.
The effects
The profile's effects are exactly these, declared without bodies in
Worker::Http:
export effect fetch method: Text url: Text headers: List[Header] body: Octets
-> Response.
export effect fetchWithSecret method: Text url: Text headers: List[Header]
body: Octets header: Text secret: Secret[Opaque] -> Response.
export effect log message: Text -> bool.(These three declarations are part of the library module, not a module of their own.)
fetchmakes one outbound HTTPS request. Its host must be on the operator's allow list, given to the runtime with--allow-origin. Redirects are not followed.fetchWithSecretdoes the same, and sets one request header to a secret's value. The program names the secret,Secret named: #ApiToken, and never sees its bytes; the value never appears in a log, an error or a response.logwrites one line to the worker's log.
A failed fetch fails the whole request in this version. A fetch that
may have been sent before it failed is reported as unknown, never as not
having happened.
Admission
captain-worker admits a program before it binds a port, and
captain-worker check prints the decision as captain.worker-admission/1.
A program is admitted only if:
- the artifact verifies, and needs a VM generation the runtime runs;
- it exports
fetch request:with exactly the signature above; Header,RequestandResponsehave exactly their published fields;- every effect the handler can reach is one of the three, or a wrapper the program defines with a body over them;
- every bodiless effect the handler can reach is one of the three, with exactly its declared parameters;
- the handler takes no
Grant; - every secret the manifest names is bound by the operator.
Rules 4 and 5 keep exec, file effects and every other host's effects out
of a worker, by name, at start rather than on some request.
$ captain-worker check hello.capb
{
"admitted": true,
"effects": [],
"format": "captain.worker-admission/1",
"profile": "captain.worker/1",
"program": "sha256 of the artifact",
"refusals": [],
"runtime": "0.1.0",
"secrets": [],
"vmGeneration": 7
}vmGeneration is the VM generation the artifact needs (see
Versions and VM generations).
A worker does not run through the Rust SDK's pure profile, because its handler has effects. The runtime drives the VM itself, and converts its values through the SDK's shared converter.
Each request
Every request runs in a fresh VM, on one of a fixed number of VM threads,
with an instruction budget and a deadline: by default 10,000,000
instructions and 10 seconds, counted from when the request head was read.
Nothing a handler computes survives its request. A handler that runs out of
budget gets a 504; one that fails gets a 500; one whose outbound call
fails gets a 502. The runtime writes its own short body for these, and
puts the detail in the log.