WebAssembly workers (Rust, Python preview)
The worker contract of the WorkManager's WebAssembly engine
(application/wasm) is the WIT package virtufin:worker@1.0.0, kept here
in wit/virtufin-worker/world.wit: the worker contract with no domain code,
like Virtufin.Worker.Host's C ABI header. A worker is one WebAssembly
component that exports virtufin:worker/guest (start, process, stop) and
may import virtufin:worker/host (publish, report-status, gateway-call,
log). What the engine offers besides, its limits and how it runs a component:
the WorkManager's WebAssembly workers
reference.
What this repo ships
| Path | What |
|---|---|
wit/virtufin-worker/world.wit |
the WIT package (the contract) |
src/rust/virtufin-worker |
the Rust crate virtufin-worker (cargo registry virtufin): wit-bindgen bindings, the Worker trait, export! |
src/rust/templates/worker |
a cargo generate template (an echo worker) |
src/rust/templates/python-componentize |
a componentize-py template (preview) |
src/rust/test-components |
the components the WorkManager's tests run: the conformance vectors' worker, every host call, a looping, a trapping and a memory-hungry worker, a filesystem import, a WASI 0.3 worker, and two it must refuse; published as the generic package worker-devkit-wasm-test-components |
The Rust toolchain is pinned in src/rust/rust-toolchain.toml (with the
wasm32-wasip2 target). Build and lint:
cd src/rust
cargo clippy --workspace --all-targets -- -D warnings
./scripts/build-test-components.sh # needs wasm-tools; output in dist/test-components
The Rust crate
use virtufin_worker::{export, host, AttributeValue, CloudEvent, Error, LogLevel, StartContext, Worker};
struct Counter {
seen: i32,
}
impl Worker for Counter {
fn start(ctx: StartContext) -> Result<Self, Error> {
host::log(LogLevel::Info, &format!("{} starts", ctx.worker_id));
Ok(Counter { seen: 0 })
}
fn process(&mut self, event: CloudEvent) -> Result<Vec<CloudEvent>, Error> {
self.seen += 1;
let mut out = CloudEvent::new("com.example.counted");
out.set_extension("count", AttributeValue::Integer(self.seen));
if let Some(id) = event.extension_str("correlationid") {
out.set_extension("correlationid", AttributeValue::String(id.to_string()));
}
Ok(vec![out])
}
}
export!(Counter);
Worker::startbuilds the worker from its context (ctx.config(name),ctx.secret(name),ctx.variable(name));processtakes&mut self;stop(self)consumes it. The value lives fromstarttostop; after a trap or a call past its deadline the engine starts a fresh instance (and callsstartagain), so nothing in memory survives a failed event.host::publish,host::report_status,host::gateway_call,host::logare the host imports.gateway_callis the only way to the network.CloudEvent::new(type),with_data,extension,extension_str,set_extension,remove_extensioncover the record.
The CloudEvent record and its attribute mapping
cloud-event field |
CloudEvents attribute |
|---|---|
id, source, type |
id, source, type (on an output, empty is unset) |
subject, time (RFC 3339), data-content-type |
subject, time, datacontenttype |
data: option<list<u8>> |
the data as bytes (text data as UTF-8) |
extensions: list<tuple<string, attribute-value>> |
every other attribute |
attribute-value |
CloudEvents type |
|---|---|
boolean |
Boolean |
integer (s32) |
Integer |
string |
String |
binary |
Binary |
uri |
URI |
uri-ref |
URI-reference |
timestamp (RFC 3339 text) |
Timestamp |
The engine converts both ways without loss: an echoed attribute keeps its
kind, and data goes back as text when its content type is text (absent means
JSON, text/*, JSON, XML) and it is UTF-8. The config, secrets and variables
the Runtime stamps on inputs are string extensions. wit-bindgen names the
Rust field type_ (type is a keyword).
Versions
A change of the WIT package is a new package version (virtufin:worker@1.1.0
for an addition, @2.0.0 for a break); the WorkManager's host lists the
versions it runs and refuses others at load, naming them. The crate's version
is LIBRARY_VERSION; the crate workflow refuses a mismatch.