Skip to main content
Sonamu provides a Workflow system for handling asynchronous background jobs. You can separate long-running tasks to the background to improve API response times and distribute work across Worker processes.

Basic Structure

enableWorker

Determines whether to enable the Worker process. Type: boolean (optional) Default: false
What is a Worker?
  • Processes tasks in a separate process
  • No impact on main API server
  • Can run multiple Workers for distributed processing
In production environments, we recommend setting enableWorker: true and running a dedicated Worker process.

Control via Environment Variable

.env:

workerOptions

Configures how the Worker operates. Type: WorkflowOptions (optional)

concurrency

The number of tasks the Worker will process simultaneously. Type: number Default: 1
Recommended values:
  • 1 - Simple tasks, sequential processing required
  • 2-5 - General background tasks
  • 10+ - 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
Behavior:
  • false: Each Worker polls independently
  • true: New task notifications propagated instantly via PostgreSQL LISTEN/NOTIFY
Pub/Sub uses PostgreSQL’s built-in LISTEN/NOTIFY mechanism, so no additional infrastructure (e.g., Redis) is required. It leverages the existing database connection.

listenDelay

The interval (in milliseconds) for checking tasks. Type: number (ms) Default: 500 (0.5 seconds)
Recommended values:
  • 100-500ms - Tasks requiring fast response
  • 500-1000ms - General background tasks
  • 1000-5000ms - Non-urgent tasks

contextProvider

Creates the Context for tasks running in the Worker. Type: (defaultContext) => Context | Promise<Context>
defaultContext includes:
  • reply - Fastify reply object (null in worker)
  • request - Fastify request object (null in worker)
  • headers - Request headers
  • createSSE - SSE stream creator
  • naiteStore - Naite logging store

Custom Context

In Worker Context, request and reply are null. HTTP-related features cannot be used.

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 the workflow() function.
Calling:
β†’ Workflow Usage

Running Workers

Development Environment

In development, keep enableWorker: false and process in main:

Production Environment

Run a dedicated Worker process:
package.json:

Multiple Workers

Run multiple Workers to increase throughput:
Using PM2 or Docker makes it easy to manage multiple Workers.

Pub/Sub Behavior

When usePubSub: 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: