Basic Structure
enableWorker
Determines whether to enable the Worker process. Type:boolean (optional)
Default: false
- Processes tasks in a separate process
- No impact on main API server
- Can run multiple Workers for distributed processing
Control via Environment Variable
workerOptions
Configures how the Worker operates. Type:WorkflowOptions (optional)
concurrency
The number of tasks the Worker will process simultaneously. Type:number
Default: 1
1- Simple tasks, sequential processing required2-5- General background tasks10+- Lightweight I/O-bound tasks
Higher
concurrency processes more tasks simultaneously but increases CPU and memory usage.usePubSub
Uses Pub/Sub for distributing work among multiple Workers. Type:boolean
Default: true
false: Each Worker polls independentlytrue: New task notifications propagated instantly via PostgreSQL LISTEN/NOTIFY
listenDelay
The interval (in milliseconds) for checking tasks. Type:number (ms)
Default: 500 (0.5 seconds)
100-500ms- Tasks requiring fast response500-1000ms- General background tasks1000-5000ms- Non-urgent tasks
contextProvider
Creates the Context for tasks running in the Worker. Type:(defaultContext) => Context | Promise<Context>
reply- Fastify reply object (null in worker)request- Fastify request object (null in worker)headers- Request headerscreateSSE- SSE stream creatornaiteStore- Naite logging store
Custom Context
Basic Examples
Single Worker (Development)
Multiple Workers (Production)
Practical Examples
Environment-based Configuration
High-Load Workflow
Custom Context
Using Workflows
After tasks configuration, define background jobs with theworkflow() function.
Running Workers
Development Environment
In development, keepenableWorker: false and process in main:
Production Environment
Run a dedicated Worker process:Multiple Workers
Run multiple Workers to increase throughput:Pub/Sub Behavior
WhenusePubSub: true (the default), the Worker receives new task notifications using PostgreSQLβs built-in LISTEN/NOTIFY mechanism. No separate message broker (e.g., Redis) is required β it leverages the existing database connection.
Cautions
1. Worker Context Limitations
2. enableWorker Setting
3. Excessive concurrency
Next Steps
After completing Task configuration:- workflows - Defining Workflows
- steps - Separating work into Steps
- error-handling - Error handling and retries