Route Tasks Across Workers
Worker routing lets an orchestrator select from multiple workers based on 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 worker with the fewest active jobs and uses registration order to break ties.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 development and test workflows, not as a security sandbox.
Worker thread pools
Use createPooledWorkerThreadGateway for workers connected through worker-thread ports. The pooled gateway listens for worker.ready and worker.heartbeat messages, updates the directory, and dispatches jobs through the same router.
Worker thread workers can send heartbeat messages by setting heartbeatIntervalMs:
Health and cancellation
Newly registered workers start as 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.