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:
Option | Description |
---|---|
slug | Define a slug-based name for this workflow. This slug needs to be unique among both tasks and workflows. |
handler | The 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. |
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 workflow. By default, this is "Workflow" + the capitalized workflow slug. |
label | Define a human-friendly label for this workflow. |
queue | Optionally, define the queue name that this workflow should be tied to. Defaults to "default". |
Example:
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: