Skip to content

Commit

Permalink
fix: allow workflows to be empty or undefined (#9039)
Browse files Browse the repository at this point in the history
### What?

- Makes `jobs.workflows` optional
- Dynamically include the `workflowSlugs` select field in the jobs
collection as needed

### Why?

When configuring jobs, it should be possible to define `job` with just
some simple tasks and not be forced to define workflows.

### How?

Workflows type was made optional and optional chaining is added where
needed. The workflowSlugs field is added to the jobs collection if
workflows are defined.

Fixes #

When using postgres, the workflowSlugs being an empty enum cause an
error when drizzle fails to detect the enum already exists. This results
in the error `"enum_payload_jobs_workflow_slug" already exists`. Drizzle
tries to make the enum as: `enum_payload_jobs_workflow_slug as enum();`
and the check for existing enums only works when it has values.
  • Loading branch information
DanRibbens authored Nov 6, 2024
1 parent 0165ab8 commit f0f96e7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
27 changes: 16 additions & 11 deletions packages/payload/src/queues/config/jobsCollection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CollectionConfig } from '../../collections/config/types.js'
import type { Config } from '../../config/types.js'
import type { Field } from '../../fields/config/types.js'

import { runJobsEndpoint } from '../restEndpointRun.js'
import { getJobTaskStatus } from '../utilities/getJobTaskStatus.js'
Expand All @@ -14,7 +15,7 @@ export const getDefaultJobsCollection: (config: Config) => CollectionConfig | nu

const queueNames: Set<string> = new Set(['default'])

config.jobs.workflows.forEach((workflow) => {
config.jobs?.workflows.forEach((workflow) => {
workflowSlugs.add(workflow.slug)

if (workflow.queue) {
Expand Down Expand Up @@ -141,16 +142,20 @@ export const getDefaultJobsCollection: (config: Config) => CollectionConfig | nu
},
],
},
{
name: 'workflowSlug',
type: 'select',
admin: {
position: 'sidebar',
},
index: true,
options: [...workflowSlugs],
required: false,
},
// only include the workflowSlugs field if workflows exist
...((workflowSlugs.size > 0
? [
{
name: 'workflowSlug',
type: 'select',
admin: {
position: 'sidebar',
},
index: true,
options: [...workflowSlugs],
},
]
: []) as Field[]),
{
name: 'taskSlug',
type: 'select',
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/queues/config/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ export type JobsConfig = {
/**
* Define all the workflows here. Workflows orchestrate the flow of multiple tasks.
*/
workflows: WorkflowConfig<any>[]
workflows?: WorkflowConfig<any>[]
}
2 changes: 1 addition & 1 deletion packages/payload/src/queues/operations/runJobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const runJobs = async ({
const jobReq = isolateObjectProperty(req, 'transactionID')

const workflowConfig: WorkflowConfig<WorkflowTypes> = job.workflowSlug
? req.payload.config.jobs.workflows.find(({ slug }) => slug === job.workflowSlug)
? req.payload.config.jobs?.workflows.find(({ slug }) => slug === job.workflowSlug)
: {
slug: 'singleTask',
handler: async ({ job, tasks }) => {
Expand Down

0 comments on commit f0f96e7

Please sign in to comment.