Skip to content

Commit f39ba53

Browse files
authored
docs: document scheduled jobs interval (medusajs#13350)
1 parent 7b1028e commit f39ba53

File tree

26 files changed

+1470
-23
lines changed

26 files changed

+1470
-23
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { Table } from "docs-ui"
2+
3+
export const metadata = {
4+
title: `${pageNumber} Set Interval for Scheduled Jobs`,
5+
}
6+
7+
# {metadata.title}
8+
9+
In this chapter, you'll learn the difference between using intervals and crons for scheduled jobs, and how to use intervals.
10+
11+
## What is a Scheduled Job Interval?
12+
13+
A scheduled job interval is a specific duration that must pass between the execution of scheduled jobs. The timer starts when the Medusa application starts, and it resets every time a job is executed.
14+
15+
For example, if you have a scheduled job that runs on an interval of 5 minutes, the job will run after 5 minutes from application startup. Then, it will run again 5 minutes after the last execution, and so on.
16+
17+
## How to Set an Interval for a Scheduled Job?
18+
19+
To set an interval for a scheduled job, pass in the scheduled job configurations object the `schedule.interval` property with the desired duration.
20+
21+
For example:
22+
23+
export const highlights = [
24+
["12", "interval", "Set the interval in milliseconds"]
25+
]
26+
27+
```ts title="src/jobs/hello-world.ts" highlights={highlights}
28+
import { MedusaContainer } from "@medusajs/framework/types"
29+
30+
export default async function greetingJob(container: MedusaContainer) {
31+
const logger = container.resolve("logger")
32+
33+
logger.info("Greeting!")
34+
}
35+
36+
export const config = {
37+
name: "greeting-every-minute",
38+
schedule: {
39+
interval: 60000 // 1 minute
40+
}
41+
}
42+
```
43+
44+
The `interval` property accepts a number indicating the duration in milliseconds.
45+
46+
So, the above scheduled job will run every minute, starting one minute after the Medusa application starts.
47+
48+
### Test the Scheduled Job Interval
49+
50+
To test out your scheduled job, start the Medusa application:
51+
52+
```bash npm2yarn
53+
npm run dev
54+
```
55+
56+
After a minute, the following message will be logged to the terminal:
57+
58+
```bash
59+
info: Greeting!
60+
```
61+
62+
---
63+
64+
## Scheduled Job Interval vs. Scheduled Job Cron
65+
66+
In the [Scheduled Jobs](../page.mdx) chapter, you learned about using the `schedule` configuration to set the schedule for a job using a cron expression. For example, `*/5 * * * *` would run a job every 5 minutes.
67+
68+
<Table>
69+
<Table.Header>
70+
<Table.Row>
71+
<Table.HeaderCell>
72+
Comparison Aspect
73+
</Table.HeaderCell>
74+
<Table.HeaderCell>
75+
Cron Expression
76+
</Table.HeaderCell>
77+
<Table.HeaderCell>
78+
Interval
79+
</Table.HeaderCell>
80+
</Table.Row>
81+
</Table.Header>
82+
<Table.Body>
83+
<Table.Row>
84+
<Table.Cell>
85+
Execution Timing
86+
</Table.Cell>
87+
<Table.Cell>
88+
Specific times of the day. For example, at midnight.
89+
</Table.Cell>
90+
<Table.Cell>
91+
After a specific duration between job executions. For example, every 5 minutes.
92+
</Table.Cell>
93+
</Table.Row>
94+
<Table.Row>
95+
<Table.Cell>
96+
Execution Consistency
97+
</Table.Cell>
98+
<Table.Cell>
99+
May not run if the specified time is reached and the system is busy
100+
</Table.Cell>
101+
<Table.Cell>
102+
Will run after the specified interval, even if the system is busy
103+
</Table.Cell>
104+
</Table.Row>
105+
</Table.Body>
106+
</Table>
107+
108+
While using a cron expression is ideal to ensure that the job runs at specific times of the day, the scheduled job will only run when the specified time is reached. So, if the events system is busy when the specified time is reached, the job will not run until the next scheduled time.
109+
110+
For example, if you have a job scheduled with a cron expression to run every 5 minutes, but the system is under heavy load, it may miss the 5-minute mark and not execute the job until the next scheduled time.
111+
112+
In contrast, a scheduled job interval is defined by a specific duration between job executions, regardless of the exact time they are scheduled to run. So, even if the system is busy, the job will still run after the specified interval has passed since the last execution.
113+
114+
### When to Use Intervals for Scheduled Jobs
115+
116+
If your scheduled job must run consistently but not at specific times of the day, use an **interval**. This ensures that the job runs after a defined duration, regardless of the system's state.
117+
118+
If your scheduled job must be executed at specific times of the day, use a cron **expression**. This allows for precise control over when the job is executed, but may result in missed executions if the system is under heavy load.

www/apps/book/generated/edit-dates.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,6 @@ export const generatedEditDates = {
128128
"app/learn/introduction/from-v1-to-v2/page.mdx": "2025-07-30T08:13:48.592Z",
129129
"app/learn/debugging-and-testing/debug-workflows/page.mdx": "2025-07-30T13:45:14.117Z",
130130
"app/learn/fundamentals/data-models/json-properties/page.mdx": "2025-07-31T14:25:01.268Z",
131-
"app/learn/debugging-and-testing/logging/custom-logger/page.mdx": "2025-08-28T15:37:07.328Z"
131+
"app/learn/debugging-and-testing/logging/custom-logger/page.mdx": "2025-08-28T15:37:07.328Z",
132+
"app/learn/fundamentals/scheduled-jobs/interval/page.mdx": "2025-08-29T11:45:01.882Z"
132133
}

www/apps/book/generated/sidebar.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,16 @@ export const generatedSidebars = [
916916
"children": [],
917917
"chapterTitle": "3.9.1. Execution Number",
918918
"number": "3.9.1."
919+
},
920+
{
921+
"loaded": true,
922+
"isPathHref": true,
923+
"type": "link",
924+
"path": "/learn/fundamentals/scheduled-jobs/interval",
925+
"title": "Set Interval",
926+
"children": [],
927+
"chapterTitle": "3.9.2. Set Interval",
928+
"number": "3.9.2."
919929
}
920930
],
921931
"chapterTitle": "3.9. Scheduled Jobs",

0 commit comments

Comments
 (0)