Skip to main content
The workflow() function defines long-running tasks, background jobs, and scheduled tasks. Built on OpenWorkflow, it provides durable execution, retries, and monitoring.

Basic Usage

Configuration (sonamu.config.ts)

Workflows require PostgreSQL configuration.

Options

name

Specifies the workflow name. Default: Function name converted to snake_case

version

Specifies the workflow version. Default: null
You can run workflows with the same name but different versions in parallel.

schema

Defines a Zod schema for input data.

schedules

Defines schedules using Cron expressions.
Cron Expression Examples:

Workflow Function

The workflow function receives 4 parameters:

input

Input data passed when executing the workflow.

step

The Step API defines each stage of the workflow. Each step can be retried and recovered independently.
Step Advantages:
  • Each step runs only once (idempotent)
  • On failure, only that step is retried
  • Already completed steps are skipped

logger

LogTape Logger instance.

version

Current workflow version.

Workflow Execution

Manual Execution

Scheduled Execution

Step API Details

step.define().run()

Wraps a function as a step and executes it.
Usage Example:

step.get().run()

Executes a Model method as a step.
Usage Example:

step.sleep()

Waits for a specified duration.
Time Format: "10s", "5m", "1h", "1d" Usage Example:

Retry Policy

Step retries are automatically handled by the OpenWorkflow backend. No separate retry options need to be configured.

Database Access

You can freely use Sonamu Models within workflows.

Error Handling

Automatic Retry

Steps are automatically retried on failure.

Manual Error Handling

Curried Style (Options First)

workflow() also provides an overload that takes the options first and the execution function afterwards. Use it when you want to read the option definition separately from the execution logic.
workflow is a function, not a TypeScript decorator. Applying @workflow(...) above a function declaration is not supported: decorators can only be applied to classes and class members.

Cautions

1. Idempotence

Steps must be idempotent. Running multiple times with the same input should guarantee the same result.

2. Split Long Tasks into Steps

3. Step Name Uniqueness

Step names must be unique within a workflow.

Monitoring

Check Execution Status

View Logs

Workflows automatically log through LogTape:

Example Collection

Next Steps

@api

Create API endpoints

@transactional

Use transactions

Scheduling

Task scheduling guide

OpenWorkflow

OpenWorkflow documentation