Skip to content

Commit e0309a1

Browse files
authored
fix: allow specifying queue (#9151)
Allows user to specify a queue when calling `payload.jobs.queue()`. Closes #9133
1 parent 6bb4067 commit e0309a1

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

packages/payload/src/queues/config/jobsCollection.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@ export const getDefaultJobsCollection: (config: Config) => CollectionConfig | nu
1313
const workflowSlugs: Set<string> = new Set()
1414
const taskSlugs: Set<string> = new Set(['inline'])
1515

16-
const queueNames: Set<string> = new Set(['default'])
17-
1816
config.jobs?.workflows.forEach((workflow) => {
1917
workflowSlugs.add(workflow.slug)
20-
21-
if (workflow.queue) {
22-
queueNames.add(workflow.queue)
23-
}
2418
})
2519

2620
config.jobs.tasks.forEach((task) => {
@@ -168,13 +162,12 @@ export const getDefaultJobsCollection: (config: Config) => CollectionConfig | nu
168162
},
169163
{
170164
name: 'queue',
171-
type: 'select',
165+
type: 'text',
172166
admin: {
173167
position: 'sidebar',
174168
},
175169
defaultValue: 'default',
176170
index: true,
177-
options: [...queueNames],
178171
},
179172
{
180173
name: 'waitUntil',

packages/payload/src/queues/config/types/workflowTypes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ export type WorkflowConfig<TWorkflowSlugOrInput extends keyof TypedJobs['workflo
108108
*/
109109
label?: string
110110
/**
111-
* Optionally, define the queue name that this workflow should be tied to.
111+
* Optionally, define the default queue name that this workflow should be tied to.
112112
* Defaults to "default".
113+
* Can be overridden when queuing jobs via Local API.
113114
*/
114115
queue?: string
115116
/**

packages/payload/src/queues/localAPI.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ export const getJobsLocalAPI = (payload: Payload) => ({
1717
args:
1818
| {
1919
input: TypedJobs['tasks'][TTaskOrWorkflowSlug]['input']
20+
queue?: string
2021
req?: PayloadRequest
2122
// TTaskOrWorkflowlug with keyof TypedJobs['workflows'] removed:
2223
task: TTaskOrWorkflowSlug extends keyof TypedJobs['tasks'] ? TTaskOrWorkflowSlug : never
2324
workflow?: never
2425
}
2526
| {
2627
input: TypedJobs['workflows'][TTaskOrWorkflowSlug]['input']
28+
queue?: string
2729
req?: PayloadRequest
2830
task?: never
2931
workflow: TTaskOrWorkflowSlug extends keyof TypedJobs['workflows']
@@ -35,10 +37,25 @@ export const getJobsLocalAPI = (payload: Payload) => ({
3537
? RunningJob<TTaskOrWorkflowSlug>
3638
: RunningJobFromTask<TTaskOrWorkflowSlug>
3739
> => {
40+
let queue: string
41+
42+
// If user specifies queue, use that
43+
if (args.queue) {
44+
queue = args.queue
45+
} else if (args.workflow) {
46+
// Otherwise, if there is a workflow specified, and it has a default queue to use,
47+
// use that
48+
const workflow = payload.config.jobs?.workflows?.find(({ slug }) => slug === args.workflow)
49+
if (workflow?.queue) {
50+
queue = workflow.queue
51+
}
52+
}
53+
3854
return (await payload.create({
3955
collection: 'payload-jobs',
4056
data: {
4157
input: args.input,
58+
queue,
4259
taskSlug: 'task' in args ? args.task : undefined,
4360
workflowSlug: 'workflow' in args ? args.workflow : undefined,
4461
},

test/queues/int.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ describe('Queues', () => {
153153
it('ensure job retrying works', async () => {
154154
const job = await payload.jobs.queue({
155155
workflow: 'retriesTest',
156+
queue: 'default',
156157
input: {
157158
message: 'hello',
158159
},

0 commit comments

Comments
 (0)