Previously, our job queue system relied on `payload.*` operations, which ran very frequently: - whenever job execution starts, as all jobs need to be set to `processing: true` - every single time a task completes or fails, as the job log needs to be updated - whenever job execution stops, to mark it as completed and to delete it (if `deleteJobOnComplete` is set) This PR replaces these with direct `payload.db.*` calls, which are significantly faster than payload operations. Given how often the job queue system communicates with the database, this should be a massive performance improvement. ## How it affects running hooks To generate the task status, we previously used an `afterRead` hook. Since direct db adapter calls no longer execute hooks, this PR introduces new `updateJob` and `updateJobs` helpers to handle task status generation outside the normal payload hook lifecycle. Additionally, a new `runHooks` property has been added to the global job configuration. While setting this to `true` can be useful if custom hooks were added to the `payload-jobs` collection config, this will revert the job system to use normal payload operations. This should be avoided as it degrades performance. In most cases, the `onSuccess` or `onFail` properties in the job config will be sufficient and much faster. Furthermore, if the `depth` property is set in the global job configuration, the job queue system will also fall back to the slower, normal payload operations. --------- Co-authored-by: Dan Ribbens <DanRibbens@users.noreply.github.com>
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import type { WorkflowConfig } from 'payload'
|
|
|
|
export const workflowRetries2TasksRetries0Workflow: WorkflowConfig<'workflowRetries2TasksRetries0'> =
|
|
{
|
|
slug: 'workflowRetries2TasksRetries0',
|
|
retries: 2,
|
|
inputSchema: [
|
|
{
|
|
name: 'message',
|
|
type: 'text',
|
|
required: true,
|
|
},
|
|
],
|
|
handler: async ({ job, tasks, req }) => {
|
|
const updatedJob = await req.payload.update({
|
|
collection: 'payload-jobs',
|
|
data: {
|
|
input: {
|
|
...job.input,
|
|
amountRetried:
|
|
// @ts-expect-error amountRetried is new arbitrary data and not in the type
|
|
job.input.amountRetried !== undefined ? job.input.amountRetried + 1 : 0,
|
|
},
|
|
},
|
|
id: job.id,
|
|
})
|
|
job.input = updatedJob.input as any
|
|
|
|
await tasks.CreateSimpleRetries0('1', {
|
|
input: {
|
|
message: job.input.message,
|
|
},
|
|
})
|
|
|
|
// At this point there should always be one post created.
|
|
// job.input.amountRetried will go up to 2 as CreatePost has 2 retries
|
|
await tasks.CreateSimpleRetries0('2', {
|
|
input: {
|
|
message: job.input.message,
|
|
shouldFail: true,
|
|
},
|
|
})
|
|
// This will never be reached
|
|
},
|
|
}
|