Skip to content

Commit 06ad171

Browse files
authored
fix: change payload.jobs.run and bin script to only run jobs from default queue by default, adds support for allQueues argument (#12799)
By default, calling `payload.jobs.run()` will incorrectly run all jobs from all queues. The `npx payload jobs:run` bin script behaves the same way. The `payload-jobs/run` endpoint runs jobs from the `default` queue, which is the correct behavior. This PR does the following: - Change `payload.jobs.run()` to only runs jobs from the `default` queue by default - Change `npx payload jobs:run` bin script to only runs jobs from the `default` queue by default - Add new allQueues / --all-queues arguments/queryparams/flags for the local API, rest API and bin script to allow you to run all jobs from all queues - Clarify the docs
1 parent 65309b1 commit 06ad171

File tree

5 files changed

+82
-12
lines changed

5 files changed

+82
-12
lines changed

docs/jobs-queue/queues.mdx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ await fetch('/api/payload-jobs/run?limit=100&queue=nightly', {
8282

8383
This endpoint is automatically mounted for you and is helpful in conjunction with serverless platforms like Vercel, where you might want to use Vercel Cron to invoke a serverless function that executes your jobs.
8484

85+
**Query Parameters:**
86+
87+
- `limit`: The maximum number of jobs to run in this invocation (default: 10).
88+
- `queue`: The name of the queue to run jobs from. If not specified, jobs will be run from the `default` queue.
89+
- `allQueues`: If set to `true`, all jobs from all queues will be run. This will ignore the `queue` parameter.
90+
8591
**Vercel Cron Example**
8692

8793
If you're deploying on Vercel, you can add a `vercel.json` file in the root of your project that configures Vercel Cron to invoke the `run` endpoint on a cron schedule.
@@ -139,11 +145,15 @@ If you want to process jobs programmatically from your server-side code, you can
139145
**Run all jobs:**
140146

141147
```ts
148+
// Run all jobs from the `default` queue - default limit is 10
142149
const results = await payload.jobs.run()
143150

144151
// You can customize the queue name and limit by passing them as arguments:
145152
await payload.jobs.run({ queue: 'nightly', limit: 100 })
146153

154+
// Run all jobs from all queues:
155+
await payload.jobs.run({ allQueues: true })
156+
147157
// You can provide a where clause to filter the jobs that should be run:
148158
await payload.jobs.run({
149159
where: { 'input.message': { equals: 'secret' } },
@@ -160,10 +170,22 @@ const results = await payload.jobs.runByID({
160170

161171
### Bin script
162172

163-
Finally, you can process jobs via the bin script that comes with Payload out of the box.
173+
Finally, you can process jobs via the bin script that comes with Payload out of the box. By default, this script will run jobs from the `default` queue, with a limit of 10 jobs per invocation:
174+
175+
```sh
176+
npx payload jobs:run
177+
```
178+
179+
You can override the default queue and limit by passing the `--queue` and `--limit` flags:
180+
181+
```sh
182+
npx payload jobs:run --queue myQueue --limit 15
183+
```
184+
185+
If you want to run all jobs from all queues, you can pass the `--all-queues` flag:
164186

165187
```sh
166-
npx payload jobs:run --queue default --limit 10
188+
npx payload jobs:run --all-queues
167189
```
168190

169191
In addition, the bin script allows you to pass a `--cron` flag to the `jobs:run` command to run the jobs on a scheduled, cron basis:

packages/payload/src/bin/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,12 @@ export const bin = async () => {
110110
const payload = await getPayload({ config })
111111
const limit = args.limit ? parseInt(args.limit, 10) : undefined
112112
const queue = args.queue ? args.queue : undefined
113+
const allQueues = !!args.allQueues
113114

114115
if (args.cron) {
115116
new Cron(args.cron, async () => {
116117
await payload.jobs.run({
118+
allQueues,
117119
limit,
118120
queue,
119121
})
@@ -124,6 +126,7 @@ export const bin = async () => {
124126
return
125127
} else {
126128
await payload.jobs.run({
129+
allQueues,
127130
limit,
128131
queue,
129132
})

packages/payload/src/queues/localAPI.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ export const getJobsLocalAPI = (payload: Payload) => ({
9898
},
9999

100100
run: async (args?: {
101+
/**
102+
* If you want to run jobs from all queues, set this to true.
103+
* If you set this to true, the `queue` property will be ignored.
104+
*
105+
* @default false
106+
*/
107+
allQueues?: boolean
108+
/**
109+
* The maximum number of jobs to run in this invocation
110+
*
111+
* @default 10
112+
*/
101113
limit?: number
102114
overrideAccess?: boolean
103115
/**
@@ -106,6 +118,11 @@ export const getJobsLocalAPI = (payload: Payload) => ({
106118
* FIFO would equal `createdAt` and LIFO would equal `-createdAt`.
107119
*/
108120
processingOrder?: Sort
121+
/**
122+
* If you want to run jobs from a specific queue, set this to the queue name.
123+
*
124+
* @default jobs from the `default` queue will be executed.
125+
*/
109126
queue?: string
110127
req?: PayloadRequest
111128
/**
@@ -118,6 +135,7 @@ export const getJobsLocalAPI = (payload: Payload) => ({
118135
const newReq: PayloadRequest = args?.req ?? (await createLocalReq({}, payload))
119136

120137
return await runJobs({
138+
allQueues: args?.allQueues,
121139
limit: args?.limit,
122140
overrideAccess: args?.overrideAccess !== false,
123141
processingOrder: args?.processingOrder,

packages/payload/src/queues/operations/runJobs/index.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,22 @@ import { runJob } from './runJob/index.js'
1818
import { runJSONJob } from './runJSONJob/index.js'
1919

2020
export type RunJobsArgs = {
21+
/**
22+
* If you want to run jobs from all queues, set this to true.
23+
* If you set this to true, the `queue` property will be ignored.
24+
*
25+
* @default false
26+
*/
27+
allQueues?: boolean
2128
/**
2229
* ID of the job to run
2330
*/
2431
id?: number | string
32+
/**
33+
* The maximum number of jobs to run in this invocation
34+
*
35+
* @default 10
36+
*/
2537
limit?: number
2638
overrideAccess?: boolean
2739
/**
@@ -32,6 +44,11 @@ export type RunJobsArgs = {
3244
* @default all jobs for all queues will be executed in FIFO order.
3345
*/
3446
processingOrder?: Sort
47+
/**
48+
* If you want to run jobs from a specific queue, set this to the queue name.
49+
*
50+
* @default jobs from the `default` queue will be executed.
51+
*/
3552
queue?: string
3653
req: PayloadRequest
3754
/**
@@ -57,10 +74,11 @@ export type RunJobsResult = {
5774
export const runJobs = async (args: RunJobsArgs): Promise<RunJobsResult> => {
5875
const {
5976
id,
77+
allQueues = false,
6078
limit = 10,
6179
overrideAccess,
6280
processingOrder,
63-
queue,
81+
queue = 'default',
6482
req,
6583
sequential,
6684
where: whereFromProps,
@@ -106,10 +124,10 @@ export const runJobs = async (args: RunJobsArgs): Promise<RunJobsResult> => {
106124
],
107125
}
108126

109-
if (queue) {
127+
if (allQueues !== true) {
110128
where.and?.push({
111129
queue: {
112-
equals: queue,
130+
equals: queue ?? 'default',
113131
},
114132
})
115133
}
@@ -146,7 +164,12 @@ export const runJobs = async (args: RunJobsArgs): Promise<RunJobsResult> => {
146164
if (typeof processingOrderConfig === 'function') {
147165
defaultProcessingOrder = await processingOrderConfig(args)
148166
} else if (typeof processingOrderConfig === 'object' && !Array.isArray(processingOrderConfig)) {
149-
if (queue && processingOrderConfig.queues && processingOrderConfig.queues[queue]) {
167+
if (
168+
!allQueues &&
169+
queue &&
170+
processingOrderConfig.queues &&
171+
processingOrderConfig.queues[queue]
172+
) {
150173
defaultProcessingOrder = processingOrderConfig.queues[queue]
151174
} else if (processingOrderConfig.default) {
152175
defaultProcessingOrder = processingOrderConfig.default

packages/payload/src/queues/restEndpointRun.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,27 @@ export const runJobsEndpoint: Endpoint = {
3939
)
4040
}
4141

42-
const { limit, queue } = req.query
42+
const { allQueues, limit, queue } = req.query as {
43+
allQueues?: boolean
44+
limit?: number
45+
queue?: string
46+
}
4347

4448
const runJobsArgs: RunJobsArgs = {
45-
queue: 'default',
49+
queue,
4650
req,
4751
// We are checking access above, so we can override it here
4852
overrideAccess: true,
4953
}
5054

51-
if (typeof queue === 'string') {
52-
runJobsArgs.queue = queue
53-
}
54-
5555
if (typeof limit !== 'undefined') {
5656
runJobsArgs.limit = Number(limit)
5757
}
5858

59+
if (allQueues && !(typeof allQueues === 'string' && allQueues === 'false')) {
60+
runJobsArgs.allQueues = true
61+
}
62+
5963
let noJobsRemaining = false
6064
let remainingJobsFromQueried = 0
6165
try {

0 commit comments

Comments
 (0)