Starting a Worker
The recommended way to run a worker is via the CLI:- Loads your
openworkflow.config.tsconfiguration - Discovers workflow files in the configured directories
- Starts polling the database for work
- Handles graceful shutdown on SIGINT/SIGTERM
How Workers Execute Workflows
When a worker picks up a workflow:- Claim: The worker atomically claims the workflow run from the database
- Load history: The worker loads all completed step attempts
- Replay: The worker executes the workflow function from the beginning
- Memoization: Completed steps return cached results instantly
- Execute: New steps are executed and their results are stored
- Complete: The workflow status is updated to
completedorfailed
Concurrency
Workers can process multiple workflow runs simultaneously. Configure concurrency in youropenworkflow.config.ts:
Start with
concurrency: 10 and tune based on your workload and database
capacity.Heartbeats and Crash Recovery
Workers maintain their claim on workflow runs through a heartbeat mechanism:- When a worker claims a workflow, it sets an
availableAttimestamp in the future - While executing, the worker periodically extends this timestamp
- If the worker crashes, the heartbeat stops
- The
availableAttimestamp expires - Another worker can claim and resume the workflow
High Availability
For production, run multiple worker instances:- Each workflow run is claimed by exactly one worker at a time
- Workers use atomic database operations to prevent duplicate processing
- If a worker crashes, its workflows become available to other workers
Graceful Shutdown
When a worker receives SIGINT or SIGTERM:- It stops polling for new work
- It waits for active workflow runs to complete their current step
- Active workflows are persisted back to the database
- The worker process exits
Workflow Discovery
The CLI automatically discovers workflows from directories specified in your config:defineWorkflow():
.ts, .js, .mts, .mjs, .cts, and .cjs files.