Skip to content

Overview

A Virtufin worker is a unit of code the WorkManager loads from a package and runs on a set of pub/sub topics. For every CloudEvent on those topics it returns zero or more CloudEvents, which are published for it. This repo holds the org's general workers, the ones that belong to no domain:

Worker Language MIME type Package
WebSocketFeed C# application/x-dotnet-dll Virtufin.Worker.WebSocketFeed (NuGet)
HttpGet C# application/x-dotnet-dll (or application/x-native-dll) Virtufin.Worker.HttpGet (NuGet)
EventStore C# application/x-dotnet-dll Virtufin.Worker.EventStore (NuGet)
Hello Python application/x-python virtufin-worker-hello (wheel)
GetTime Python application/x-python virtufin-worker-gettime (wheel)
ScriptRunner Python application/x-python virtufin-worker-scriptrunner (wheel)

Key features

Feature Description
Devkit base classes C# workers derive from WorkerBase<,> (Worker DevKit); Python workers subclass WorkerBase/CommandWorker/ApiWorker from virtufin-worker-devkit. The base names the worker, builds its envelopes and turns exceptions into error responses.
Shared packaging target A C# worker's nupkg comes from Worker.DevKit's VirtufinPackageWorker MSBuild target: the worker DLL plus a nuspec of its dependencies, which the Runtime resolves at load time.
Managed, native or both HttpGet ships a managed and a NativeAOT build of the same worker class; virtufin-worker build --native puts both layouts in one nupkg. See Packaging Modes.
Thin Python wheels A Python worker's wheel holds only worker.py and its Requires-Dist lines; the engine installs the dependencies when it loads the worker.
CI publishing A push to master that changes a worker's directory tests it and publishes its LIBRARY_VERSION to the org feed.

Where a worker runs

Every worker runs in the WorkManager Runtime, a service separate from the WorkManager. The WorkManager keeps the catalog and serves the gRPC API; each worker is a Runtime actor that owns one engine instance. The MIME type passed to CreateWorker picks the actor type and so the engine:

  • application/x-dotnet-dll: DotNetDllEngine loads lib/<tfm>/<AssemblyName>.dll from the nupkg into its own AssemblyLoadContext in the Runtime process (an embedded CoreCLR, since the Runtime is NativeAOT).
  • application/x-native-dll: NativeDllEngine loads runtimes/<rid>/native/lib<name>.so into the Runtime process and calls its C ABI (Process/FreeResult, ABI v2: a protobuf CloudEvent in, a protobuf CloudEventBatch out).
  • application/x-python: PythonEngine installs the wheel and its dependencies at load time and runs worker.py in a child python3 process.

The Engines page says what each means for the workers here; the WorkManager's Engine System is the full reference.

Base classes

A worker is written against a devkit, never against an engine:

  • C#: derive from WorkerBase<TInput, TOutput> (Virtufin.Worker.DevKit) or, for a Virtufin.Core behavior, from BehaviorWorkerBase (Virtufin.Behavior.DevKit). The worker type passed to the base constructor names the worker's source, response and error types and topics. See Base classes. The same class runs under both .NET engines; the native build adds two [UnmanagedCallersOnly] exports that delegate to AotNative<T> (Virtufin.Worker.Host).
  • Python: subclass WorkerBase, CommandWorker or ApiWorker from virtufin-worker-devkit and expose a module-level Process(cloud_event).

A worker that calls the platform gets its API from its host: context.Api (the IWorkerContext of every C# call), ApiWorker's self.api in Python. On the cluster that is the gateway the Runtime is configured with, with its API key; under a local host an in-memory API. HttpGet and GetTime make plain outbound HTTP calls and need no gateway client.

Lifecycle and failures

CreateWorker returns at once; the Runtime fetches and loads the code in the background. The WorkManager publishes the worker's transitions on sys.workmanager.worker.lifecycle: registered, loaded (or load-failed), starting, online (or failed), stopped, deleted, and updated when its tags or code change. See Worker Lifecycle.

The devkit base classes catch an exception in the worker's handler and publish an error response instead (com.virtufin.<type>.error on wrk.<type>.error, or the caller's replytopic). A message the worker still fails on (it throws past the base class, or a native worker returns an error status) is not retried: the Runtime publishes it once to dlq.<input topic>, with dlqworkerid, dlqerrortype and dlqerrormessage extensions, and goes on with the next message.

Crash isolation

The .NET and native engines run worker code inside the Runtime process. A crash there (a native segfault, a stack overflow) takes down that Runtime pod and every worker it hosts; the WorkManager's API keeps running and the workers come back when their actors reactivate. A native worker must therefore come from a fully trusted source. Python workers run in a child process and cannot take the pod down.

See also