Base classes
WorkerBase<,> is the typed worker the WorkManager host calls through
IWorker. It decodes the input payload with an IPayloadCodec<,> and builds
the response envelope.
public sealed class MyWorker() : WorkerBase<JsonNode, JsonObject>("my", new StrictJsonCodec())
{
protected override Task<JsonObject?> ProcessAsync(CloudEvent input, JsonNode payload)
=> Task.FromResult<JsonObject?>(new JsonObject { ["echo"] = payload.DeepClone() });
}
The worker type ("my") names the worker through the naming library
(WorkerTypeNames):
| Name | |
|---|---|
| source | urn:com.virtufin:worker:my |
| response | com.virtufin.my.response on wrk.my.response |
| error | com.virtufin.my.error on wrk.my.error, data worker-devkit's WorkerError message (command, message) |
| metrics | com.virtufin.my.metrics on wrk.my.metrics, subject worker/<id> (Worker metrics) |
An incoming replytopic changes the topic of both, never the ce-type. A
worker is always named by its worker type.
| Base | Use when |
|---|---|
WorkerBase<TInput, TOutput> |
one input payload, zero or one output |
WorkerCore<TInput, TOutput> |
several outputs per input, without a behavior: implement ProcessAsync(CloudEvent, CancellationToken) and build each output with BuildResponse/Error |
BehaviorWorkerBase (Virtufin.Behavior.DevKit) |
a Virtufin.Core behavior, zero or more outputs; see Behavior workers |
The codec decides how the payload is read. StrictJsonCodec is the default: a
payload that is not JSON is an error. LenientJsonCodec
(Virtufin.Behavior.DevKit) decodes such a payload to an empty object, for a
worker that reads the CloudEvent itself. A custom codec implements
IPayloadCodec<TInput, TOutput>. IPayloadCodec only frames a worker's request
and response; a domain payload's wire forms belong to the ICodec registry
(Codecs).
The lifecycle, the context and the Virtufin API
Every host starts a worker once before its first event and stops it after
its last (IWorker.StartAsync/StopAsync, worker lifecycle).
WorkerCore stores the activation's IWorkerContext at start, so a pure
worker overrides nothing; Context is there for every later call:
WorkerId, Api, Logger, the Config and Secrets snapshots, State
(the worker's own keys, deleted with the worker), PublishAsync (through the
host's outbox, like a returned output) and ReportStatusAsync (a status
lifecycle event). Stopping is the activation's stop token: canceled when
the host loses ownership (its lease, a stop, a reload, a delete, a drain):
publish nothing more, StopAsync follows.
context.Api is the Virtufin API (IVirtufinApi: state, pubsub, triggers,
gateway) of whatever hosts the worker: the cluster's gateway under the
Runtime, the in-memory LocalVirtufinApi under a LocalWorkManager or a
BehaviorTestHost, with its publishes routed through the host. A worker that
saves state therefore runs in a notebook with no gateway configured and no
VIRTUFIN_API_* environment:
protected override async Task<JsonObject?> ProcessAsync(CloudEvent input, JsonNode payload)
{
var saved = await Context.State.GetAsync("cursor", Stopping);
await Context.State.SaveAsync("cursor", Next(saved?.Value), saved?.Etag, Stopping);
return null;
}
A worker that owns something overrides the lifecycle and calls the base:
public override async Task StartAsync(IWorkerContext context, CancellationToken stop)
{
await base.StartAsync(context, stop); // stores the context
_loop = RunAsync(stop); // returns at once; the loop publishes through Context.PublishAsync
await Context.ReportStatusAsync("connected");
}
public override async Task StopAsync(CancellationToken deadline)
{
await _loop.WaitAsync(deadline); // stop fired before this; the host waits until the deadline
await base.StopAsync(deadline);
}
Inputs can arrive twice
Input topics are at least once since WorkManager 0.38.0: after a hard crash
the events of the last cursor window (≤250 ms) come again
(Inputs across a move).
BehaviorWorkerBase drops a repeated input and derives its output ids from
the input; a WorkerCore or WorkerBase worker of your own does it with
SeenEvents and DerivedEventId, see
Behavior workers: inputs can arrive twice.
Dependencies are topics
A worker's input topics (CreateWorker topics) are its whole dependency: it
subscribes to them and never names the worker that fills them. Who produces a
topic is the orchestrator's business (a notebook, a script, the TUI): for
venue data, market-data-devkit's FeedResolver turns the topics the workers
listen to into the WebSocketFeed workers that serve them and
FeedOrchestrator opens them (see
venue feeds). Workers
and feeds start in any order. The requires=<type>/<name> hook of 0.37 to
0.39 (attach/detach commands to a resource worker) is gone since 0.40.0: a
config key of that name is ordinary config now.
The resource worker (a WebSocketFeed, workmanager#103 phase 3) consumes the
commands; a resource worker is created with library_reload off
(LIBRARY_RELOAD_OFF, a typed CreateWorker field, never a config key), so a
library release never reloads it in place.
WorkerApiEndpoint.CreateClient(endpoint) remains for a worker whose config
points it at an explicit other gateway (host[:port], http://…,
https://… for TLS): the host's key (VIRTUFIN_API_KEY) is attached only when
that target is the host's own configured gateway on its configured transport.