Skip to content

Commit c0dc0cc

Browse files
authored
feat: autoRun jobs (payloadcms#10401)
This pull request introduces the ability to configure cron jobs for automatically running tasks in the job queue.
1 parent 7321f9f commit c0dc0cc

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

Diff for: docs/jobs-queue/queues.mdx

+27-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,33 @@ Then, you could configure two different runner strategies:
2525

2626
## Executing jobs
2727

28-
As mentioned above, you can queue jobs, but the jobs won't run unless a worker picks up your jobs and runs them. This can be done in two ways:
28+
As mentioned above, you can queue jobs, but the jobs won't run unless a worker picks up your jobs and runs them. This can be done in four ways:
29+
30+
#### Cron jobs
31+
32+
You can use the jobs.autoRun property to configure cron jobs:
33+
34+
```ts
35+
export default buildConfig({
36+
// Other configurations...
37+
jobs: {
38+
tasks: [
39+
// your tasks here
40+
],
41+
// autoRun can optionally be a function that receives payload as an argument
42+
autoRun: [
43+
{
44+
cron: '0 * * * *', // every hour at minute 0
45+
limit: 100, // limit jobs to process each run
46+
queue: 'hourly', // name of the queue
47+
},
48+
// add as many cron jobs as you want
49+
],
50+
},
51+
})
52+
```
53+
54+
<Banner type="warning">autoRun is intended for use with a dedicated server that is always running, and should not be used on serverless platforms like Vercel.</Banner>
2955

3056
#### Endpoint
3157

Diff for: packages/payload/src/index.ts

+21
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ import type {
6363
} from './types/index.js'
6464
import type { TraverseFieldsCallback } from './utilities/traverseFields.js'
6565
export type { FieldState } from './admin/forms/Form.js'
66+
import { Cron } from 'croner'
67+
6668
import type { TypeWithVersion } from './versions/types.js'
6769

6870
import { decrypt, encrypt } from './auth/crypto.js'
@@ -712,6 +714,25 @@ export class BasePayload {
712714
await this.config.onInit(this)
713715
}
714716
}
717+
if (this.config.jobs.autoRun) {
718+
const DEFAULT_CRON = '* * * * *'
719+
const DEFAULT_LIMIT = 10
720+
721+
const cronJobs =
722+
typeof this.config.jobs.autoRun === 'function'
723+
? await this.config.jobs.autoRun(this)
724+
: this.config.jobs.autoRun
725+
await Promise.all(
726+
cronJobs.map((cronConfig) => {
727+
new Cron(cronConfig.cron ?? DEFAULT_CRON, async () => {
728+
await this.jobs.run({
729+
limit: cronConfig.limit ?? DEFAULT_LIMIT,
730+
queue: cronConfig.queue,
731+
})
732+
})
733+
}),
734+
)
735+
}
715736

716737
return this
717738
}

Diff for: packages/payload/src/queues/config/types/index.ts

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
11
import type { CollectionConfig } from '../../../index.js'
2-
import type { PayloadRequest } from '../../../types/index.js'
2+
import type { Payload, PayloadRequest } from '../../../types/index.js'
33
import type { TaskConfig } from './taskTypes.js'
44
import type { WorkflowConfig } from './workflowTypes.js'
55

6+
export type CronConfig = {
7+
/**
8+
* The cron schedule for the job.
9+
* @default '* * * * *' (every minute).
10+
*
11+
* @example
12+
* ┌───────────── minute (0 - 59)
13+
* │ ┌───────────── hour (0 - 23)
14+
* │ │ ┌───────────── day of the month (1 - 31)
15+
* │ │ │ ┌───────────── month (1 - 12)
16+
* │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday)
17+
* │ │ │ │ │
18+
* │ │ │ │ │
19+
* - '0 * * * *' every hour at minute 0
20+
* - '0 0 * * *' daily at midnight
21+
* - '0 0 * * 0' weekly at midnight on Sundays
22+
* - '0 0 1 * *' monthly at midnight on the 1st day of the month
23+
* - '0/5 * * * *' every 5 minutes
24+
*/
25+
cron?: string
26+
/**
27+
* The limit for the job. This can be overridden by the user. Defaults to 10.
28+
*/
29+
limit?: number
30+
/**
31+
* The queue name for the job.
32+
*/
33+
queue?: string
34+
}
35+
636
export type RunJobAccessArgs = {
737
req: PayloadRequest
838
}
@@ -19,14 +49,18 @@ export type JobsConfig = {
1949
*/
2050
run?: RunJobAccess
2151
}
22-
/**
23-
* Adds information about the parent job to the task log. This is useful for debugging and tracking the flow of tasks.
52+
/** Adds information about the parent job to the task log. This is useful for debugging and tracking the flow of tasks.
2453
*
2554
* In 4.0, this will default to `true`.
2655
*
2756
* @default false
2857
*/
2958
addParentToTaskLog?: boolean
59+
/**
60+
* Queue cron jobs automatically on payload initialization.
61+
* @remark this property should not be used on serverless platforms like Vercel
62+
*/
63+
autoRun?: ((payload: Payload) => CronConfig[] | Promise<CronConfig[]>) | CronConfig[]
3064
/**
3165
* Determine whether or not to delete a job after it has successfully completed.
3266
*/

0 commit comments

Comments
 (0)