payload icon indicating copy to clipboard operation
payload copied to clipboard

Queue jobs update generates an error when you try to save a task with a failed state: The following field is invalid: _index-2._index-0.log.0.error

Open slongiaru-dmind opened this issue 1 year ago • 2 comments

Describe the Bug

When a task return with state 'failed' the related job goes into an error trying to save the related job.log.

The job collection define the field 'log.error' as a 'json' type. @see: https://github.com/payloadcms/payload/blob/main/packages/payload/src/queues/config/jobsCollection.ts#L126

Related there was 2 issues that set the 'log.error' field as a string causing the updateJob fail. The error should be a JSON format and not a string.

@see: https://github.com/payloadcms/payload/blob/main/packages/payload/src/queues/operations/runJobs/runJob/getRunTaskFunction.ts#L88

 const errorJSON = error
    ? {
        name: error.name,
        message: error.message,
        stack: error.stack,
      }
    : runnerOutput.state

Should be something like:

 const errorJSON = error
    ? {
        name: error.name,
        message: error.message,
        stack: error.stack,
      }
    : {message: runnerOutput.state}    // <-------

@see: https://github.com/payloadcms/payload/blob/main/packages/payload/src/queues/operations/runJobs/runJob/getRunTaskFunction.ts#L233

{
  completedAt: new Date().toISOString(),
  error: errorMessage,
  executedAt: executedAt.toISOString(),
  state: 'failed',
  taskID,
  taskSlug,
},

Should be something like:

{
  completedAt: new Date().toISOString(),
  error: {message: errorMessage},    // <-------
  executedAt: executedAt.toISOString(),
  state: 'failed',
  taskID,
  taskSlug,
},

DETAILED ERROR

ERROR: Error running job sendMessageToDevice undefined id: 6751793f572c12d3dd6e37f6 attempt 0/2
    err: {
      "type": "ValidationError",
      "message": "The following field is invalid: _index-2._index-0.log.0.error",
      "stack":
          ValidationError: The following field is invalid: _index-2._index-0.log.0.error
              at beforeChange (/payload/src/fields/hooks/beforeChange/index.ts:69:11)
              at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
              at async updateByIDOperation (/payload/src/collections/operations/updateByID.ts:329:18)
              at async <anonymous> (/payload/src/queues/operations/runJobs/runJob/getUpdateJobFunction.ts:8:25)
              at async handleTaskFailed (/payload/src/queues/operations/runJobs/runJob/getRunTaskFunction.ts:128:5)

Link to the code that reproduces this issue

https://github.com/payloadcms/payload/blob/main/packages/payload/src/queues/operations/runJobs/runJob/getRunTaskFunction.ts

Reproduction Steps

jobs.tasks.push({
    slug: 'taskForceFail',
    handler: async ({ input, req }) => {
      return {
        output: null,
        state: 'failed',
      }
      // throw new Error(`Or force error.`)
    },
  })
await payload.jobs.queue({
          input: undefined,
          task: 'taskForceFail',
        })
payload.jobs.run()

Which area(s) are affected? (Select all that apply)

area: core

Environment Info

Payload: 3.*

slongiaru-dmind avatar Dec 05 '24 10:12 slongiaru-dmind

Fixed in 3.4.0 already - https://github.com/payloadcms/payload/pull/9644

AlessioGr avatar Dec 05 '24 21:12 AlessioGr

nvm reopening, this might still be an issue if an error is returned and not thrown. Will look into this

AlessioGr avatar Dec 05 '24 21:12 AlessioGr

🚀 This is included in version v3.26.0

github-actions[bot] avatar Mar 04 '25 15:03 github-actions[bot]

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.

github-actions[bot] avatar Mar 12 '25 05:03 github-actions[bot]