fix(plugin-cloud): ensure scheduled publishing works if no custom jobs are defined (#12410)

Previously, plugin-cloud would only set up job auto-running if a job configuration was present in the custom config at initialization time.

However, some jobs - such as the scheduled publish job which is added during sanitization - are added after plugin-cloud has initialized. This means relying solely on the initial state of the job config is insufficient for determining whether to enable auto-running.

This PR removes that check and ensures auto-running is always initialized, allowing later-added jobs to run as expected.

## Weakening type

This PR also weakens to `config.jobs.tasks` type and makes that property optional. It's totally permissible to only have workflows that define inline tasks, and to not have any static tasks defined in `config.jobs.tasks`. Thus it makes no sense to make that property required.
This commit is contained in:
Alessio Gravili
2025-05-14 14:58:25 -07:00
committed by GitHub
parent 93d79b9c62
commit d63c8baea5
7 changed files with 22 additions and 25 deletions

View File

@@ -16,6 +16,14 @@ export const generateRandomString = (): string => {
return Array.from({ length: 24 }, () => chars[Math.floor(Math.random() * chars.length)]).join('')
}
const DEFAULT_CRON = '* * * * *'
const DEFAULT_LIMIT = 10
const DEFAULT_CRON_JOB = {
cron: DEFAULT_CRON,
limit: DEFAULT_LIMIT,
queue: 'default',
}
export const payloadCloudPlugin =
(pluginOptions?: PluginOptions) =>
async (incomingConfig: Config): Promise<Config> => {
@@ -100,15 +108,6 @@ export const payloadCloudPlugin =
}
// We make sure to only run cronjobs on one instance using a instance identifier stored in a global.
const DEFAULT_CRON = '* * * * *'
const DEFAULT_LIMIT = 10
const DEFAULT_CRON_JOB = {
cron: DEFAULT_CRON,
limit: DEFAULT_LIMIT,
queue: 'default',
}
config.globals = [
...(config.globals || []),
{
@@ -126,13 +125,13 @@ export const payloadCloudPlugin =
},
]
if (pluginOptions?.enableAutoRun === false || !config.jobs) {
if (pluginOptions?.enableAutoRun === false) {
return config
}
const oldAutoRunCopy = config.jobs.autoRun ?? []
const oldAutoRunCopy = config.jobs?.autoRun ?? []
const hasExistingAutorun = Boolean(config.jobs.autoRun)
const hasExistingAutorun = Boolean(config.jobs?.autoRun)
const newShouldAutoRun = async (payload: Payload) => {
if (process.env.PAYLOAD_CLOUD_JOBS_INSTANCE) {
@@ -150,8 +149,8 @@ export const payloadCloudPlugin =
return false
}
if (!config.jobs.shouldAutoRun) {
config.jobs.shouldAutoRun = newShouldAutoRun
if (!config.jobs?.shouldAutoRun) {
;(config.jobs ??= {}).shouldAutoRun = newShouldAutoRun
}
const newAutoRun = async (payload: Payload) => {