Tasks
You can register Tasks on the Payload config, and then create Jobs or Workflows that use them. Think of Tasks like tidy, isolated "functions that do one specific thing".
Payload Tasks can be configured to automatically retried if they fail, which makes them valuable for "durable" workflows like AI applications where LLMs can return non-deterministic results, and might need to be retried.
Tasks can either be defined within the jobs.tasks
array in your payload config, or they can be defined inline within a workflow.
Defining tasks in the config
Simply add a task to the jobs.tasks
array in your Payload config. A task consists of the following fields:
Option | Description |
---|---|
slug | Define a slug-based name for this job. This slug needs to be unique among both tasks and workflows. |
handler | The function that should be responsible for running the job. You can either pass a string-based path to the job function file, or the job function itself. If you are using large dependencies within your job, you might prefer to pass the string path because that will avoid bundling large dependencies in your Next.js app. |
inputSchema | Define the input field schema - payload will generate a type for this schema. |
interfaceName | You can use interfaceName to change the name of the interface that is generated for this task. By default, this is "Task" + the capitalized task slug. |
outputSchema | Define the output field schema - payload will generate a type for this schema. |
label | Define a human-friendly label for this task. |
onFail | Function to be executed if the task fails. |
onSuccess | Function to be executed if the task succeeds. |
retries | Specify the number of times that this step should be retried if it fails. |
The logic for the Task is defined in the handler
- which can be defined as a function, or a path to a function. The handler
will run once a worker picks picks up a Job that includes this task.
It should return an object with an output
key, which should contain the output of the task as you've defined.
Example:
In addition to defining handlers as functions directly provided to your Payload config, you can also pass an absolute path to where the handler is defined. If your task has large dependencies, and you are planning on executing your jobs in a separate process that has access to the filesystem, this could be a handy way to make sure that your Payload + Next.js app remains quick to compile and has minimal dependencies.
In general, this is an advanced use case. Here's how this would look:
payload.config.ts:
Then, the createPost
file itself:
src/tasks/createPost.ts: