Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(payload-cloud): set up cron jobs on init #10106

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/payload-cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"ts-jest": "^29.1.0"
},
"peerDependencies": {
"croner": "9.0.0",
"payload": "workspace:*"
},
"publishConfig": {
Expand Down
58 changes: 57 additions & 1 deletion packages/payload-cloud/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Config } from 'payload'

import { Cron } from 'croner'

import type { PluginOptions } from './types.js'

import { payloadCloudEmail } from './email.js'
Expand Down Expand Up @@ -93,5 +95,59 @@ export const payloadCloudPlugin =
})
}

return config
// If the user has tasks configured, we set up cron jobs on init.
// We also make sure to only run on one instance using a instance identifier stored in a global.
if (config.jobs?.tasks) {
GermanJablo marked this conversation as resolved.
Show resolved Hide resolved
config.globals = [
...(config.globals || []),
{
slug: 'payload-cloud-instance',
admin: {
hidden: true,
},
fields: [
{
name: 'instance',
type: 'text',
required: true,
},
],
},
]
config.onInit = async (payload) => {
GermanJablo marked this conversation as resolved.
Show resolved Hide resolved
if (config.onInit) {
await config.onInit(payload)
}
const instance = generateRandomString()

await payload.updateGlobal({
slug: 'payload-cloud-instance',
data: {
instance,
},
})

const cloudInstance = await payload.findGlobal({
slug: 'payload-cloud-instance',
})

if (cloudInstance.instance === instance) {
pluginOptions?.jobs?.forEach((cronConfig) => {
GermanJablo marked this conversation as resolved.
Show resolved Hide resolved
new Cron(cronConfig.cron ?? '* * * * *', async () => {
await payload.jobs.run({
limit: cronConfig.limit ?? 100,
queue: cronConfig.queue,
})
})
})
}
}

return config
}
}

function generateRandomString(): string {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
return Array.from({ length: 24 }, () => chars[Math.floor(Math.random() * chars.length)]).join('')
}
35 changes: 35 additions & 0 deletions packages/payload-cloud/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ export interface PayloadCloudEmailOptions {
skipVerify?: boolean
}

export type CronConfig = {
/**
* The cron schedule for the job. Defaults to '* * * * *' (every minute).
GermanJablo marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* ┌───────────── minute (0 - 59)
* │ ┌───────────── hour (0 - 23)
* │ │ ┌───────────── day of the month (1 - 31)
* │ │ │ ┌───────────── month (1 - 12)
* │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday)
* │ │ │ │ │ OR sun, mon, tue, wed, thu, fri, sat
* │ │ │ │ │
* │ │ │ │ │
* - '0 * * * *' every hour at minute 0
* - '0 0 * * *' daily at midnight
* - '0 0 * * 0' weekly at midnight on Sundays
* - '0 0 1 * *' monthly at midnight on the 1st day of the month
* - '0/5 * * * *' every 5 minutes
*/
cron?: string
/**
* The limit for the job. This can be overridden by the user. Defaults to 100.
*/
limit?: number
/**
* The queue name for the job.
*/
queue?: string
}

export interface PluginOptions {
/** Payload Cloud Email
* @default true
Expand All @@ -72,6 +102,11 @@ export interface PluginOptions {
*/
endpoint?: string

/**
* Jobs configuration
*/
jobs?: CronConfig[]

/** Payload Cloud Storage
* @default true
*/
Expand Down
58 changes: 16 additions & 42 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading