Overview
Edge workers are Python modules that run inside worker containers on edge devices. Each worker declares hooks (callbacks for sensor data) and model loading using thecw client—no imports needed.
Architecture: one edge device → one worker container → one runtime → many worker modules.
Quick Example
Hook Decorators
Register callbacks for sensor streams. Hooks are passive at import time—the runtime activates them.
All callbacks receive
(sample_payload, ctx) where ctx is a HookContext with timestamp, channel, sensor_name, twin_uuid, and metadata.
Passing
sensor=None (or omitting it) subscribes to every sensor of that
type on the twin — the SDK constructs a frames/** (or depth/**, etc.)
wildcard key expression. ctx.sensor_name is populated from the observed
publish key so a single handler can disambiguate multi-sensor twins.
Pin sensor="<name>" (e.g. "color_camera") when you need to target one
specific sensor — the name must match what the twin asset declares.Model Loading
cw.models.load() caches models — safe to call at module level.
Publishing Events
cyberwave/twin/{uuid}/event via MQTT. Payload matches the backend mqtt_consumer.handle_business_event() schema.
Configuration
Runtime Entrypoint
Worker modules never callcw.run_edge_workers() themselves. The container entrypoint does:
Lifecycle
Edge Core starts the worker container only when at least one workflow is active for the connected twins (i.e. the latest sync produced one or morewf_*.py files in {config_dir}/workers/). When no active workflow exists, the cyberwaveos/edge-ml-worker image is not pulled at all.
Workflow activations and deactivations are picked up automatically:
- At startup: after the workflow sync step, the worker container is started if files exist; otherwise startup logs
No active workflows for connected twins; worker container not startedand proceeds. - At runtime: every ~5 minutes the workers directory is re-synced; the worker container is started when files appear and stopped when files disappear. Both transitions are idempotent.
- Immediately on activate: when a
run_on_edgeworkflow is activated, the backend publishes async_workflowsMQTT command on each referenced twin’s command topic; edge core runs an immediate sync and lifecycle reconcile so the newwf_*.pylands within seconds. - Immediately on remove (deactivate / soft-delete /
run_on_edgeflip off): the backend publishes a surgicalremove_workflow_workerMQTT command naming the specific worker filename(s); edge core unlinks the file from{config_dir}/workers/and immediately reconciles the container lifecycle. The same nudge also fires when a workflow is removed as part of a workspace or environment teardown. Removal propagates within seconds instead of waiting up to ~5 minutes for the periodic sync plus the bulk-sync two-strikes cleanup. The periodic reconcile remains the correctness backstop.
cyberwave workflow sync <twin_uuid> from the CLI continues to trigger an immediate sync via MQTT, which reuses the same lifecycle path on the next reconcile.