Skip to main content
Workflow is a system for executing background tasks. Use it for email sending, data processing, scheduled jobs, and more.

Tasks Configuration

Options:
  • enableWorker: Whether to enable Worker (default: true only when NODE_TYPE=daemon)
  • workerOptions.concurrency: Number of concurrent tasks (default: CPU core count - 1)
  • workerOptions.usePubSub: Use PostgreSQL LISTEN/NOTIFY (default: true)
  • workerOptions.listenDelay: Wait time after receiving Pub/Sub message (default: 500ms)
  • contextProvider: Function to create Context used within Workflows

WorkflowManager

Sonamu automatically creates and manages the WorkflowManager. Access it via Sonamu.workflows.

Worker Modes

1. Daemon Mode (Auto-enabled)

There is no separate daemon command. When tasks.enableWorker is not set, Sonamu enables the Worker only for processes started with NODE_TYPE=daemon. The Worker is created during Sonamu.init() and started after the server begins listening.
Set enableWorker explicitly to override this detection on any node.

2. Separate Worker Process

startWorker() starts only the Worker that Sonamu.init() created. Set tasks.enableWorker: true or run this process with NODE_TYPE=daemon; otherwise it initializes the task backend without a Worker.

3. API Server + Worker

Distributed Workers

You can run Workers on multiple servers.
PostgreSQL acts as the task queue, and Workers communicate via Pub/Sub.

Context Provider

Customize the Context used in Workflows.
Declare custom fields such as systemUserId in ContextExtend so they are typed. Access via Sonamu.getContext() within a Workflow function:

Table Structure

The task backend runs its own migrations and creates the sonamu_tasks schema in the main database with two tables:

Monitoring

Check Running Workflows

Logging

Next Steps

@workflow Decorator

Define Workflows

Step

Divide tasks with Steps