Skip to content

Commit 3acdbf6

Browse files
feat: pass args to task onSuccess and onFail callbacks (#13269)
### What? Passes the same args provided to a task's `shouldRestore` function to both the `onSuccess` & `onFail` callbacks ### Why? Currently `onSuccess` and `onFail` are quite useless without any context, this will allow for a wider range of functionality: - Checking if it's the last failure - Access to the task `input` - Access to `req` to allow logging, email notifications, etc. ### How? 1. Created a new `TaskCallbackArgs` type, which replicates the args of the `ShouldRestoreFn` type. 2. Add a `TaskCallbackFn` type 3. Update the function calls of both `onSuccess` and `onFail`. ### Questions - I wasn't sure about the typing of `input` – I can see `input: input!` being used elsewhere for task errors so I replicated that. - Same for `taskStatus`, I added a type check but I'm not sure if this is the right approach (what would scenario would result in a `null` value?). Should `TaskCallbackArgs['taskStatus']` be typed to allow for `null` values? --------- Co-authored-by: Alessio Gravili <alessio@gravili.de>
1 parent 9a841df commit 3acdbf6

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

packages/payload/src/queues/config/types/taskTypes.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,20 @@ export type RunInlineTaskFunction = <TTaskInput extends object, TTaskOutput exte
127127
},
128128
) => Promise<TTaskOutput>
129129

130-
export type ShouldRestoreFn = (args: {
130+
export type TaskCallbackArgs = {
131131
/**
132132
* Input data passed to the task
133133
*/
134-
input: object
134+
input?: object
135135
job: Job
136136
req: PayloadRequest
137-
taskStatus: SingleTaskStatus<string>
138-
}) => boolean | Promise<boolean>
137+
taskStatus: null | SingleTaskStatus<string>
138+
}
139+
140+
export type ShouldRestoreFn = (
141+
args: { taskStatus: SingleTaskStatus<string> } & Omit<TaskCallbackArgs, 'taskStatus'>,
142+
) => boolean | Promise<boolean>
143+
export type TaskCallbackFn = (args: TaskCallbackArgs) => Promise<void> | void
139144

140145
export type RetryConfig = {
141146
/**
@@ -220,11 +225,11 @@ export type TaskConfig<
220225
/**
221226
* Function to be executed if the task fails.
222227
*/
223-
onFail?: () => Promise<void> | void
228+
onFail?: TaskCallbackFn
224229
/**
225230
* Function to be executed if the task succeeds.
226231
*/
227-
onSuccess?: () => Promise<void> | void
232+
onSuccess?: TaskCallbackFn
228233
/**
229234
* Define the output field schema - payload will generate a type for this schema.
230235
*/

packages/payload/src/queues/errors/handleTaskError.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ export async function handleTaskError({
4747
} = error.args
4848

4949
if (taskConfig?.onFail) {
50-
await taskConfig.onFail()
50+
await taskConfig.onFail({
51+
input,
52+
job,
53+
req,
54+
taskStatus,
55+
})
5156
}
5257

5358
const errorJSON = {

packages/payload/src/queues/operations/runJobs/runJob/getRunTaskFunction.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const getRunTaskFunction = <TIsInline extends boolean>(
9595
shouldRestore = false
9696
} else if (typeof finalRetriesConfig?.shouldRestore === 'function') {
9797
shouldRestore = await finalRetriesConfig.shouldRestore({
98-
input: input!,
98+
input,
9999
job,
100100
req,
101101
taskStatus,
@@ -182,7 +182,12 @@ export const getRunTaskFunction = <TIsInline extends boolean>(
182182
}
183183

184184
if (taskConfig?.onSuccess) {
185-
await taskConfig.onSuccess()
185+
await taskConfig.onSuccess({
186+
input,
187+
job,
188+
req,
189+
taskStatus,
190+
})
186191
}
187192

188193
const newLogItem: JobLog = {

0 commit comments

Comments
 (0)