Worker Routing
Worker routing lets one orchestrator choose among multiple workers by task capability, active job count, and health.
Use it when:
- multiple workers can run the same task type
- workers expose different task types
- stale or unhealthy workers should be skipped
- cancellation must be routed to the worker currently running a job
Concepts
WorkerDirectorytracks worker ids, task types, health, heartbeats, and active jobs.WorkerRouterselects a capable healthy worker. The default router chooses the least-active worker and keeps registration order as a tie-break.RoutingWorkerGatewayimplementsWorkerGateway, so it can be passed directly tocreateOrchestrator.
In-memory workers
The in-memory transport runs handlers in the same process. Use it for trusted dev/test flows, not sandboxing.
Worker thread pools
Use createPooledWorkerThreadGateway when workers run behind worker thread ports. The pooled gateway listens for worker.ready and worker.heartbeat messages, updates the directory, and routes jobs through the same router.
Worker thread workers can send heartbeat messages by setting heartbeatIntervalMs:
Health and cancellation
Registered workers start healthy. A worker becomes stale when now - lastHeartbeatAt > heartbeatTimeoutMs. The least-active router skips stale and unhealthy workers.
During dispatch, the routing gateway records jobId -> workerId. requestCancel(jobId) uses that mapping to send cancellation to the active endpoint.