fix: run queues via the /payload-jobs/run endpoint without workflows (#9509)
Fixes https://github.com/payloadcms/payload/discussions/9418 (the `/api/payload-jobs/run` endpoint) when the config doesn't have any `workflows` but only `tasks`
This commit is contained in:
@@ -1,13 +1,25 @@
|
|||||||
import type { Endpoint } from '../config/types.js'
|
import type { Endpoint, SanitizedConfig } from '../config/types.js'
|
||||||
|
|
||||||
import { runJobs, type RunJobsArgs } from './operations/runJobs/index.js'
|
import { runJobs, type RunJobsArgs } from './operations/runJobs/index.js'
|
||||||
|
|
||||||
|
const configHasJobs = (config: SanitizedConfig): boolean => {
|
||||||
|
if (!config.jobs) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.jobs.tasks.length > 0) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if (Array.isArray(config.jobs.workflows) && config.jobs.workflows.length > 0) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
export const runJobsEndpoint: Endpoint = {
|
export const runJobsEndpoint: Endpoint = {
|
||||||
handler: async (req) => {
|
handler: async (req) => {
|
||||||
if (
|
if (!configHasJobs(req.payload.config)) {
|
||||||
!Array.isArray(req.payload.config.jobs.workflows) ||
|
|
||||||
!(req.payload.config.jobs?.workflows?.length > 0)
|
|
||||||
) {
|
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{
|
{
|
||||||
message: 'No jobs to run.',
|
message: 'No jobs to run.',
|
||||||
|
|||||||
@@ -423,6 +423,32 @@ describe('Queues', () => {
|
|||||||
expect(allSimples.docs[0].title).toBe('from single task')
|
expect(allSimples.docs[0].title).toBe('from single task')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('can queue and run via the endpoint single tasks without workflows', async () => {
|
||||||
|
const workflowsRef = payload.config.jobs.workflows
|
||||||
|
delete payload.config.jobs.workflows
|
||||||
|
await payload.jobs.queue({
|
||||||
|
task: 'CreateSimple',
|
||||||
|
input: {
|
||||||
|
message: 'from single task',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await restClient.GET('/payload-jobs/run', {
|
||||||
|
headers: {
|
||||||
|
Authorization: `JWT ${token}`,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const allSimples = await payload.find({
|
||||||
|
collection: 'simple',
|
||||||
|
limit: 100,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(allSimples.totalDocs).toBe(1)
|
||||||
|
expect(allSimples.docs[0].title).toBe('from single task')
|
||||||
|
payload.config.jobs.workflows = workflowsRef
|
||||||
|
})
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// 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
|
// 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
|
||||||
it('transaction test against payload-jobs collection', async () => {
|
it('transaction test against payload-jobs collection', async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user