Skip to content

Commit f076797

Browse files
authored
fix: job task output data was not fetched and restored, if task failed previously (#9651)
Assume you had the following workflow: ```ts handler: async ({ job, inlineTask, req }) => { const { customerData } = await inlineTask('Fetch Customer Data', { task: ({ req }) => { if (Math.random() < 0.2) { throw new Error('Failed on purpose') } return { output: { customerData: 'test', }, } }, retries: { attempts: 40, }, }) console.log('customer Data', customerData) await inlineTask('Analyze Segments', { // Rest of task... ``` It was possible for the following to happen: Run attempt 1: - Task "Fetch Customer Data" fails - Task is added to job log without output data and state "failed" Run attempt 2: - Task "Fetch Customer Data" succeeds - Task is added to job log with correct output data and state "succeeded" - Task "Analyze Segments" fails - Task is added to job log without output data and state "failed" Run attempt 3: - Task "Fetch Customer Data" has already run successfully => restore from DB - Task "Analyze Segments" fails because input data is undefined. The restoration of the already-succeeded "Fetch Customer Data" task did not fetch and restore the correct output data, as it was taking the output data from the previously failed task that did not save any, even though it should have been taking and restoring the output data of the last-run, successful task run. This PR fixed that
1 parent 963387f commit f076797

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function handleTaskFailed({
7070
taskStatus: null | SingleTaskStatus<string>
7171
updateJob: UpdateJobFunction
7272
}): Promise<never> {
73-
req.payload.logger.error({ err: error, job, msg: 'Error running task', taskSlug })
73+
req.payload.logger.error({ err: error, job, msg: `Error running task ${taskID}`, taskSlug })
7474

7575
if (taskConfig?.onFail) {
7676
await taskConfig.onFail()

packages/payload/src/queues/utilities/getJobTaskStatus.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export const getJobTaskStatus = ({ jobLog }: Args): JobTaskStatus => {
2929

3030
if (loggedJob.state === 'succeeded') {
3131
newTaskStatus.complete = true
32+
// As the task currently saved in taskStatus has likely failed and thus has no
33+
// Output data, we need to update it with the new data from the successful task
34+
newTaskStatus.output = loggedJob.output
35+
newTaskStatus.input = loggedJob.input
36+
newTaskStatus.taskSlug = loggedJob.taskSlug
3237
}
3338
taskStatus[loggedJob.taskSlug][loggedJob.taskID] = newTaskStatus
3439
}

0 commit comments

Comments
 (0)