Skip to content

feat: pass args to task onSuccess and onFail callbacks #13269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/payload/src/queues/config/types/taskTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,18 @@ export type RunInlineTaskFunction = <TTaskInput extends object, TTaskOutput exte
},
) => Promise<TTaskOutput>

export type ShouldRestoreFn = (args: {
export type TaskCallbackArgs = {
/**
* Input data passed to the task
*/
input: object
job: Job
req: PayloadRequest
taskStatus: SingleTaskStatus<string>
}) => boolean | Promise<boolean>
}

export type ShouldRestoreFn = (args: TaskCallbackArgs) => boolean | Promise<boolean>
export type TaskCallbackFn = (args: TaskCallbackArgs) => Promise<void> | void

export type RetryConfig = {
/**
Expand Down Expand Up @@ -220,11 +223,11 @@ export type TaskConfig<
/**
* Function to be executed if the task fails.
*/
onFail?: () => Promise<void> | void
onFail?: TaskCallbackFn
/**
* Function to be executed if the task succeeds.
*/
onSuccess?: () => Promise<void> | void
onSuccess?: TaskCallbackFn
/**
* Define the output field schema - payload will generate a type for this schema.
*/
Expand Down
9 changes: 7 additions & 2 deletions packages/payload/src/queues/errors/handleTaskError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ export async function handleTaskError({
workflowConfig,
} = error.args

if (taskConfig?.onFail) {
await taskConfig.onFail()
if (taskConfig?.onFail && taskStatus) {
await taskConfig.onFail({
input: input!,
job,
req,
taskStatus,
})
}

const errorJSON = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,13 @@ export const getRunTaskFunction = <TIsInline extends boolean>(
output = taskHandlerResult.output
}

if (taskConfig?.onSuccess) {
await taskConfig.onSuccess()
if (taskConfig?.onSuccess && taskStatus) {
await taskConfig.onSuccess({
input: input!,
job,
req,
taskStatus,
})
}

;(job.log ??= []).push({
Expand Down
Loading