Skip to content

Commit b96475b

Browse files
authored
fix: run queues via the /payload-jobs/run endpoint without workflows (#9509)
Fixes #9418 (the `/api/payload-jobs/run` endpoint) when the config doesn't have any `workflows` but only `tasks`
1 parent cae300e commit b96475b

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

packages/payload/src/queues/restEndpointRun.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
import type { Endpoint } from '../config/types.js'
1+
import type { Endpoint, SanitizedConfig } from '../config/types.js'
22

33
import { runJobs, type RunJobsArgs } from './operations/runJobs/index.js'
44

5+
const configHasJobs = (config: SanitizedConfig): boolean => {
6+
if (!config.jobs) {
7+
return false
8+
}
9+
10+
if (config.jobs.tasks.length > 0) {
11+
return true
12+
}
13+
if (Array.isArray(config.jobs.workflows) && config.jobs.workflows.length > 0) {
14+
return true
15+
}
16+
17+
return false
18+
}
19+
520
export const runJobsEndpoint: Endpoint = {
621
handler: async (req) => {
7-
if (
8-
!Array.isArray(req.payload.config.jobs.workflows) ||
9-
!(req.payload.config.jobs?.workflows?.length > 0)
10-
) {
22+
if (!configHasJobs(req.payload.config)) {
1123
return Response.json(
1224
{
1325
message: 'No jobs to run.',

test/queues/int.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,32 @@ describe('Queues', () => {
423423
expect(allSimples.docs[0].title).toBe('from single task')
424424
})
425425

426+
it('can queue and run via the endpoint single tasks without workflows', async () => {
427+
const workflowsRef = payload.config.jobs.workflows
428+
delete payload.config.jobs.workflows
429+
await payload.jobs.queue({
430+
task: 'CreateSimple',
431+
input: {
432+
message: 'from single task',
433+
},
434+
})
435+
436+
await restClient.GET('/payload-jobs/run', {
437+
headers: {
438+
Authorization: `JWT ${token}`,
439+
},
440+
})
441+
442+
const allSimples = await payload.find({
443+
collection: 'simple',
444+
limit: 100,
445+
})
446+
447+
expect(allSimples.totalDocs).toBe(1)
448+
expect(allSimples.docs[0].title).toBe('from single task')
449+
payload.config.jobs.workflows = workflowsRef
450+
})
451+
426452
/*
427453
// Task rollbacks are not supported in the current version of Payload. This test will be re-enabled when task rollbacks are supported once we figure out the transaction issues
428454
it('transaction test against payload-jobs collection', async () => {

0 commit comments

Comments
 (0)