Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Workflows

They're most helpful when you have multiple tasks in a row, and you want to configure each task to be able to be retried if they fail.

If a task within a workflow fails, the Workflow will automatically "pick back up" on the task where it failed and not re-execute any prior tasks that have already been executed.

Defining a workflow

The most important aspect of a Workflow is the handler, where you can declare when and how the tasks should run by simply calling the runTask function. If any task within the workflow, fails, the entire handler function will re-run.

However, importantly, tasks that have successfully been completed will simply re-return the cached and saved output without running again. The Workflow will pick back up where it failed and only task from the failure point onward will be re-executed.

To define a JS-based workflow, simply add a workflow to the jobs.wokflows array in your Payload config. A workflow consists of the following fields:

OptionDescription
slugDefine a slug-based name for this workflow. This slug needs to be unique among both tasks and workflows.
handlerThe function that should be responsible for running the workflow. You can either pass a string-based path to the workflow function file, or workflow job function itself. If you are using large dependencies within your workflow, you might prefer to pass the string path because that will avoid bundling large dependencies in your Next.js app. Passing a string path is an advanced feature that may require a sophisticated build pipeline in order to work.
inputSchemaDefine the input field schema - payload will generate a type for this schema.
interfaceNameYou can use interfaceName to change the name of the interface that is generated for this workflow. By default, this is "Workflow" + the capitalized workflow slug.
labelDefine a human-friendly label for this workflow.
queueOptionally, define the queue name that this workflow should be tied to. Defaults to "default".
retriesYou can define retries on the workflow level, which will enforce that the workflow can only fail up to that number of retries. If a task does not have retries specified, it will inherit the retry count as specified on the workflow. You can specify 0 as workflow retries, which will disregard all task retry specifications and fail the entire workflow on any task failure. You can leave workflow retries as undefined, in which case, the workflow will respect what each task dictates as their own retry count. By default this is undefined, meaning workflows retries are defined by their tasks

Example:

1
export default buildConfig({
2
// ...
3
jobs: {
4
tasks: [
5
// ...
6
]
7
workflows: [
8
{
9
slug: 'createPostAndUpdate',
10
11
// The arguments that the workflow will accept
12
inputSchema: [
13
{
14
name: 'title',
15
type: 'text',
16
required: true,
17
},
18
],
19
20
// The handler that defines the "control flow" of the workflow
21
// Notice how it uses the `tasks` argument to execute your predefined tasks.
22
// These are strongly typed!
23
handler: async ({ job, tasks }) => {
24
25
// This workflow first runs a task called `createPost`.
26
27
// You need to define a unique ID for this task invocation
28
// that will always be the same if this workflow fails
29
// and is re-executed in the future. Here, we hard-code it to '1'
30
const output = await tasks.createPost('1', {
31
input: {
32
title: job.input.title,
33
},
34
})
35
36
// Once the prior task completes, it will run a task
37
// called `updatePost`
38
await tasks.updatePost('2', {
39
input: {
40
post: job.taskStatus.createPost['1'].output.postID, // or output.postID
41
title: job.input.title + '2',
42
},
43
})
44
},
45
} as WorkflowConfig<'updatePost'>
46
]
47
}
48
})

Running tasks inline

In the above example, our workflow was executing tasks that we already had defined in our Payload config. But, you can also run tasks without predefining them.

To do this, you can use the inlineTask function.

The drawbacks of this approach are that tasks cannot be re-used across workflows as easily, and the task data stored in the job will not be typed. In the following example, the inline task data will be stored on the job under job.taskStatus.inline['2'] but completely untyped, as types for dynamic tasks like these cannot be generated beforehand.

Example:

1
export default buildConfig({
2
// ...
3
jobs: {
4
tasks: [
5
// ...
6
]
7
workflows: [
8
{
9
slug: 'createPostAndUpdate',
10
inputSchema: [
11
{
12
name: 'title',
13
type: 'text',
14
required: true,
15
},
16
],
17
handler: async ({ job, tasks, inlineTask }) => {
18
// Here, we run a predefined task.
19
// The `createPost` handler arguments and return type
20
// are both strongly typed
21
const output = await tasks.createPost('1', {
22
input: {
23
title: job.input.title,
24
},
25
})
26
27
// Here, this task is not defined in the Payload config
28
// and is "inline". Its output will be stored on the Job in the database
29
// however its arguments will be untyped.
30
const { newPost } = await inlineTask('2', {
31
task: async ({ req }) => {
32
const newPost = await req.payload.update({
33
collection: 'post',
34
id: '2',
35
req,
36
retries: 3,
37
data: {
38
title: 'updated!',
39
},
40
})
41
return {
42
output: {
43
newPost
44
},
45
}
46
},
47
})
48
},
49
} as WorkflowConfig<'updatePost'>
50
]
51
}
52
})
Next

Jobs