You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: configurable job queue processing order (LIFO/FIFO), allow sequential execution of jobs (#11897)
Previously, jobs were executed in FIFO order on MongoDB, and LIFO on
Postgres, with no way to configure this behavior.
This PR makes FIFO the default on both MongoDB and Postgres and
introduces the following new options to configure the processing order
globally or on a queue-by-queue basis:
- a `processingOrder` property to the jobs config
- a `processingOrder` argument to `payload.jobs.run()` to override
what's set in the jobs config
It also adds a new `sequential` option to `payload.jobs.run()`, which
can be useful for debugging.
Copy file name to clipboardExpand all lines: docs/jobs-queue/queues.mdx
+77-4Lines changed: 77 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ Then, you could configure two different runner strategies:
28
28
29
29
As mentioned above, you can queue jobs, but the jobs won't run unless a worker picks up your jobs and runs them. This can be done in four ways:
30
30
31
-
####Cron jobs
31
+
### Cron jobs
32
32
33
33
You can use the `jobs.autoRun` property to configure cron jobs:
34
34
@@ -63,7 +63,7 @@ export default buildConfig({
63
63
and should not be used on serverless platforms like Vercel.
64
64
</Banner>
65
65
66
-
####Endpoint
66
+
### Endpoint
67
67
68
68
You can execute jobs by making a fetch request to the `/api/payload-jobs/run` endpoint:
69
69
@@ -130,7 +130,7 @@ This works because Vercel automatically makes the `CRON_SECRET` environment vari
130
130
131
131
After the project is deployed to Vercel, the Vercel Cron job will automatically trigger the `/api/payload-jobs/run` endpoint in the specified schedule, running the queued jobs in the background.
132
132
133
-
####Local API
133
+
### Local API
134
134
135
135
If you want to process jobs programmatically from your server-side code, you can use the Local API:
Finally, you can process jobs via the bin script that comes with Payload out of the box.
162
162
@@ -169,3 +169,76 @@ In addition, the bin script allows you to pass a `--cron` flag to the `jobs:run`
169
169
```sh
170
170
npx payload jobs:run --cron "*/5 * * * *"
171
171
```
172
+
173
+
## Processing Order
174
+
175
+
By default, jobs are processed first in, first out (FIFO). This means that the first job added to the queue will be the first one processed. However, you can also configure the order in which jobs are processed.
176
+
177
+
### Jobs Configuration
178
+
179
+
You can configure the order in which jobs are processed in the jobs configuration by passing the `processingOrder` property. This mimics the Payload [sort](../queries/sort) property that's used for functionality such as `payload.find()`.
180
+
181
+
```ts
182
+
exportdefaultbuildConfig({
183
+
// Other configurations...
184
+
jobs: {
185
+
tasks: [
186
+
// your tasks here
187
+
],
188
+
processingOrder: '-createdAt', // Process jobs in reverse order of creation = LIFO
189
+
},
190
+
})
191
+
```
192
+
193
+
You can also set this on a queue-by-queue basis:
194
+
195
+
```ts
196
+
exportdefaultbuildConfig({
197
+
// Other configurations...
198
+
jobs: {
199
+
tasks: [
200
+
// your tasks here
201
+
],
202
+
processingOrder: {
203
+
default: 'createdAt', // FIFO
204
+
queues: {
205
+
nightly: '-createdAt', // LIFO
206
+
myQueue: '-createdAt', // LIFO
207
+
},
208
+
},
209
+
},
210
+
})
211
+
```
212
+
213
+
If you need even more control over the processing order, you can pass a function that returns the processing order - this function will be called every time a queue starts processing jobs.
214
+
215
+
```ts
216
+
exportdefaultbuildConfig({
217
+
// Other configurations...
218
+
jobs: {
219
+
tasks: [
220
+
// your tasks here
221
+
],
222
+
processingOrder: ({ queue }) => {
223
+
if (queue==='myQueue') {
224
+
return'-createdAt'// LIFO
225
+
}
226
+
return'createdAt'// FIFO
227
+
},
228
+
},
229
+
})
230
+
```
231
+
232
+
### Local API
233
+
234
+
You can configure the order in which jobs are processed in the `payload.jobs.queue` method by passing the `processingOrder` property.
235
+
236
+
```ts
237
+
const createdJob =awaitpayload.jobs.queue({
238
+
workflow: 'createPostAndUpdate',
239
+
input: {
240
+
title: 'my title',
241
+
},
242
+
processingOrder: '-createdAt', // Process jobs in reverse order of creation = LIFO
0 commit comments