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
When queuing a job, you can pass additional options:
150
+
151
+
```ts
152
+
awaitpayload.jobs.queue({
153
+
task: 'sendEmail',
154
+
input: { userId: '123' },
155
+
156
+
// Schedule the job to run in the future
157
+
waitUntil: newDate('2024-12-25T00:00:00Z'),
158
+
159
+
// Assign to a specific queue
160
+
queue: 'high-priority',
161
+
162
+
// Add custom metadata for tracking
163
+
log: [
164
+
{
165
+
message: 'Email queued by admin',
166
+
createdAt: newDate().toISOString(),
167
+
},
168
+
],
169
+
})
170
+
```
171
+
172
+
#### Common options
173
+
174
+
-`waitUntil` - Schedule the job to run at a specific date/time in the future
175
+
-`queue` - Assign the job to a specific queue (defaults to `'default'`)
176
+
-`log` - Add custom log entries for debugging or tracking
177
+
-`req` - Pass the request context for access control
178
+
179
+
#### Check Job Status
180
+
181
+
After queuing a job, you can check its status:
182
+
183
+
```ts
184
+
const job =awaitpayload.jobs.queue({
185
+
task: 'processPayment',
186
+
input: { orderId: '123' },
187
+
})
188
+
189
+
// Later, check the job status
190
+
const updatedJob =awaitpayload.findByID({
191
+
collection: 'payload-jobs',
192
+
id: job.id,
193
+
})
194
+
195
+
console.log(updatedJob.completedAt) // When it finished
196
+
console.log(updatedJob.hasError) // If it failed
197
+
console.log(updatedJob.taskStatus) // Details of each task
198
+
```
199
+
200
+
#### Job Status Fields
201
+
202
+
Each job document contains:
203
+
204
+
```ts
205
+
{
206
+
id: 'job_123',
207
+
taskSlug: 'sendEmail', // Or workflowSlug for workflows
208
+
input: { userId: '123' }, // The input you provided
209
+
completedAt: '2024-01-15...', // When job completed (null if pending)
210
+
hasError: false, // True if job failed
211
+
totalTried: 1, // Number of attempts
212
+
processing: false, // True if currently running
213
+
taskStatus: { // Status of each task (for workflows)
214
+
sendEmail: {
215
+
'1': {
216
+
complete: true,
217
+
output: { emailSent: true }
218
+
}
219
+
}
220
+
},
221
+
log: [ // Execution log
222
+
{
223
+
message: 'Job started',
224
+
createdAt: '...'
225
+
}
226
+
]
227
+
}
228
+
```
229
+
50
230
#### Access Control
51
231
52
232
By default, Payload's job operations bypass access control when used from the Local API. You can enable access control by passing `overrideAccess: false` to any job operation.
@@ -92,12 +272,16 @@ await payload.jobs.queue({
92
272
})
93
273
```
94
274
275
+
<Bannertype="warning">
276
+
It is not recommended to modify the `payload-jobs` collection's access control
277
+
directly, as that pattern may be deprecated in future versions. Instead—use
278
+
the `access` property in your Jobs Config to control job operations.
279
+
</Banner>
280
+
95
281
#### Cancelling Jobs
96
282
97
283
Payload allows you to cancel jobs that are either queued or currently running. When cancelling a running job, the current task will finish executing, but no subsequent tasks will run. This happens because the job checks its cancellation status between tasks.
98
284
99
-
##### Cancel a Single Job
100
-
101
285
To cancel a specific job, use the `payload.jobs.cancelByID` method with the job's ID:
Copy file name to clipboardExpand all lines: docs/jobs-queue/overview.mdx
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ Payload's Jobs Queue gives you a simple, yet powerful way to offload large or fu
10
10
11
11
### Example use cases
12
12
13
-
**Non-blocking workloads**
13
+
#### Non-blocking workloads
14
14
15
15
You might need to perform some complex, slow-running logic in a Payload [Hook](/docs/hooks/overview) but you don't want that hook to "block" or slow down the response returned from the Payload API. Instead of running this logic directly in a hook, which would block your API response from returning until the expensive work is completed, you can queue a new Job and let it run at a later date.
16
16
@@ -20,7 +20,7 @@ Examples:
20
20
- Send data to a third-party API on document change
21
21
- Trigger emails based on customer actions
22
22
23
-
**Scheduled actions**
23
+
#### Scheduled actions
24
24
25
25
If you need to schedule an action to be run or processed at a certain date in the future, you can queue a job with the `waitUntil` property set. This will make it so the job is not "picked up" until that `waitUntil` date has passed.
26
26
@@ -40,7 +40,7 @@ Examples:
40
40
- Periodically trigger a rebuild of your frontend at night
41
41
- Sync resources to or from a third-party API during non-peak times
42
42
43
-
**Offloading complex operations**
43
+
#### Offloading complex operations
44
44
45
45
You may run into the need to perform computationally expensive functions which might slow down your main Payload API server(s). The Jobs Queue allows you to offload these tasks to a separate compute resource rather than slowing down the server(s) that run your Payload APIs. With Payload Task definitions, you can even keep large dependencies out of your main Next.js bundle by dynamically importing them only when they are used. This keeps your Next.js + Payload compilation fast and ensures large dependencies do not get bundled into your Payload production build.
0 commit comments